scheme,racket , scheme/racket: breaking a list into list of lists

scheme/racket: breaking a list into list of lists


Tag: scheme,racket

If i have a list with 3 distinct objects that are separated from each other with an all uppercase string, how do i bundle them into a list of 3 lists?

I've only gotten so far as

#lang racket

(define (test-string4 lst keyword)
  (let ((kw (string-upcase keyword)))
    (cond ((null? lst) '())
          ((string-upper-case? (car lst))

For a list of '("POLYLINE" "2" "3" ... "LINE" "2" "3" ...) it should be broken down into '(("POLYLINE" "2" "3" ...) ("LINE" "2" "3" ...))


Assuming that string-upper-case? has already been defined (say, using andmap):

(define (string-upper-case? str)
  (andmap char-upper-case?
          (string->list str)))

… We can write a generic, simpler, arguably more idiomatic implementation using break from SRFI-1 to split a list into sublists with elements that start with a given condition, in this case being an all-uppercase string:

(require srfi/1)

(define (splitter pred? lst)
  (if (empty? lst)
      (let-values ([(data tail) (break pred? (rest lst))])        
        (cons (cons (first lst) data)
              (splitter pred? tail)))))

It doesn't matter how long each sequence of elements is, we don't even have to pass a list of keywords as long as we respect the convention that keywords are all-uppercase strings. For example:

(splitter string-upper-case?
          '("POLYLINE" "2" "3" "4" "LINE" "2" "3" "TEST" "1"))

=> '(("POLYLINE" "2" "3" "4") ("LINE" "2" "3") ("TEST" "1"))


Racket - How to use foldr to evaluate if any element in a list satisfies an argument?

I have the task of writing a program called any? that requires an input of a list and one-argument procedure and then tells you if any element in that list satisfies the procedure. ex: (any? odd? (list 2 4 6 8)) -> false I need to use foldr in the...

how to write lex file for input like “{\”a\“:1,\”b\“:2}”

I want to implement a json parser, but having problem with parse object like "{\"a\":1,\"b\":2}", currently the parser output somthing like this '(json (object "{" (kvpair "\"a\":1,\"b\"" ":" (json (number "2"))) "}")) but what i actually want is '(json (object "{" (kvpair "\"a\"" ":" (json (number "1"))) "," (kvpair "\"b\""...

abstracting away match in racket

I have a couple of functions that matches for structs that look like such: (define (get-bounding-y struct-lst) (flatten (for/list ([i struct-lst]) (match i [(line _ _ _ _ _ y1 _ y2) (list y1 y2)] [(arc _ _ _ _ _ y radius _ _ _ _ _ _ _...

Using match with user defined types in PL Racket

The following PL code does not work under #lang pl: Edited code according to Alexis Kings answer (define-type BINTREE [Leaf Number] [Node BINTREE BINTREE]) (: retrieve-leaf : BINTREE -> Number) (define (retrieve-leaf btree) (match btree [(Leaf number) number]) What i'd like to achieve is as follows: Receive a BINTREE as...

How to use symbols and lists in scheme to process data?

I am a newbie in scheme, and I am in the process of writing a function that checks pairwise disjointess of rules (for the time being is incomplete), I used symbols and lists in order to represent the rues of the grammar. Uppercase symbol is a non-terminal in the grammar,...

Racket lexer - return list of cons

I'm new to Racket but super excited about it. I've been working on writing a simple lexer for WWW-Authenticate headers. I'm feeling pretty good about the lexing, but now I'd like to change my output. #lang racket (require parser-tools/lex) (require (prefix-in : parser-tools/lex-sre)) (define in (open-input-string "MsRtcOAuth href=\"\",grant_type=\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\", Bearer trusted_issuers=\"\",...

Writing a While Loop in Scheme

I am trying to implement a while loop using recursion with lambda, but I just don't understand how to do it. I am supposed to start with this lambda expression: ((lambda (x) (x x)) (lambda (x) (x x)) My first question is why does this cause 'eternal' recursion? I try...

Is there a way to see the body of a lambda in Racket?

Say I have this code: #lang racket (define a ((λ (x) x) ((λ (y) y) (λ (z) ((λ (w) w) z))))) I know intuitively that this lambda expression is (extensionally) equal to (λ (z) z) My question is if there is a way to print out the body of a...

dr racket code error beginning student language

I am writing a function that consumes two images and produces true if the first is larger than the second here is my code (require 2htdp/image) (check-expect(image1 30 40)false) (check-expect(image1 50 20)true) (define(image1 x y) (begin ( circle x "solid" "blue") (circle y "solid" "blue") (if(> x y) "true" "false")))...

Racket: Graphing a parabola with elements from a list

I have created the following expression which I would like to graph parabolas based on the last two elements in a list. It looks like this: #lang racket (require plot) (define list-sqr-graph (lambda (lst) (cond [(null? lst) (plot (function sqr 0 0))] [(<= (car lst) 0) (list-sqr-graph (cdr lst))] [(not...

“Mapping” Little Schemer to coderbyte challenge: Capitalize

After studying most of The Little Schemer, I've been trying my hand at some recursive solutions to Coderbyte challenges. After some fiddling I threw in cons and thought my upperConsIt would work to look through an array, find all of the instances of a particular letter and capitalize each. Ultimately,...

Racket/Scheme embed list within existing list

I'm attempting to embed a list in to an existing list, the problem is explained below. list1 bla bla bla bla list2 useful stuff is here while my function iterates list1 it picks a random point where it will then insert: useful stuff is here the issue is that list2...

Create lists from lists

I want to write a function which for n arguments will create n lists and each contains n-th element for every argument, for example: (aux '(1 2) '(3 4)) = `((1 3) (2 4)) I wrote such a function: (define (aux . args) (if (null? args) '() (cons (map car...

Implement yield and send in Scheme

I'm trying to port yield and yield from from Python to Scheme. Here is an implementation I've done: (define (coroutine routine) (let ((current routine) (status 'new)) (lambda* (#:optional value) (let ((continuation-and-value (call/cc (lambda (return) (let ((returner (lambda (value) (call/cc (lambda (next) (return (cons next value))))))) (if (equal? status 'new) (begin...

Find empy lines in text file

I've been learning racket for a few days and I'm puzzled with this task, I'm trying to find empty lines in a text file and select a random empty line to INSERT the text "calculation here", this is as far as I have gotten so far. for example: myfile.txt has...

Writing a `define-let` macro, with hygiene

I'm trying to write a define-let macro in racket, which "saves" the header of a (let ((var value) ...) ...) , namely just the (var value) ... part, and allows re-using it later on. The code below works as expected: #lang racket ;; define-let allows saving the header part of...

Append string to existing textfile in IronScheme

We are trying to construct a log file using IronScheme, and we have written a code for it using racket. It works fine in racket, but IronScheme throws an error. This is what we have so far: (define write-to-log (lambda(whatToWrite) (with-output-to-file "robot-log.txt" (lambda () (printf (string-append whatToWrite "\r\n" ))) #:exists...

How to get only specific elements of list in racket

Input: '(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73) ("june 101" 55 55) ("june 104" 55 54) ("aug 201" 220 220)) Desired output: '(("may 001" 75 72) ("may 002" 75 75) ("may 003" 70 73)) How do I achieve this? I only want the may terms....

Racket not closing TCP port

I've written a simple HTTP echo server in Racket. When I run the server from within DrRacket and then click the Stop button, my program terminates, but the port that was being used takes an annoyingly long time to close. If I run lsof -i :<port> in my terminal after...

What's the function to render scribble string with the environment in a hash table?

I'm new to using scribble, but I can't work out how to use its syntax in my own programs, rather than using a scribble language. > (define ht (make-hash '(("Name" . "Simon")))) > (define template "Hello @Name") > (function-i-dont-know ht template) "Hello Simon" What is the function that I'm looking...

Defining cons, car and cdr with lambda - how does it work? [duplicate]

This question already has an answer here: Use of lambda for cons/car/cdr definition in SICP 2 answers I can't seem to explain how this definition of cons, car and cdr works: (define (p-cons x y) (lambda (proc) (proc x y))) (define (p-car proc) (proc (lambda (p q) p))) (define...

Where can the Raco Pkgs library “table-editor” be downloaded?

I'm attempting to install this library for Racket, which depends on a library called table-editor. Helpfully, there is a comment in the source: ;; raco pkg install table-panel Unfortunately, that package doesn't seem to exist. Google is not returning helpful results. Does anybody know where to find it?...

Scheme several actions if an if-statement proves true

The way I understand a scheme if-statement is that the first condition is when the if-statement is true, and the second statement is when it is false. What if I want several conditions for when the statement proves true? An example: (if (= a b) (set! ([a 2])) // This...

Using MIT/GNU Scheme

I have downloaded the MIT/GNU Scheme implementation of LISP in order to understand Structure and Interpretation of Computer Programs. However, I am a bit confused about what I am dealing with when I launch the program. When the program is launched, I get two windows: the "MIT/GNU Scheme" window, and...

Scheme - unpack list's elements into function [duplicate]

This question already has an answer here: How do I pass a list as a list of arguments in racket? 2 answers I have a function which takes an unlimited number of args such as (define (func . args)). Say I have a list '(1 2 3), how do...

Generating n-grams with foldl in Racket

So I'm fiddling with Racket. Recursively generating n-grams from a list of words was pretty straightforward: (define (n-grams-recursive words n) (if (< (length words) n) '() (cons (take words n) (n-grams-recursive (cdr words) n)))) And the iterative version also pretty straightforward: (define (n-grams words n) (define (iter n-grams remaining-words) (if...

Returning an element and then deleting the element from a list in Scheme

I have a list in Scheme which contains numbers. I wrote a function that takes as input one number and a list of objects and checks if the confidence value of the object matches the number. Once I get the object whose confidence values matches the given number, I want...

Accessing call stack depth in Scheme

In order to demonstrate the effectiveness of tail recursion, I would like a way to access the depth of the call stack dynamically in Scheme. Is there a way to do this? If not, is there a way to do this in other major functional languages (OCaml, Haskell, etc.)?...

Any history background about the “yin-yang puzzle” in detail?

There are quite a few questions about the "yin-yang puzzle" already in Stackoverflow: How does the yin yang puzzle work? (I assume this is the first one) Haskell (this is my question) C# 5.0 Typed languages ... (there could be something I didn't find yet) I was wondering when and...

How is it possible to filter a list of directories via “directory-exists?”?

I recently discovered a strange behaviour of racket: Whenever I try to filter a list of directories created via directory-list my REPL returns me an empty list, but when I try the same with an quasiquoted list my REPL returns a correctly filtered list. My questions is now: Why it's...

How to configure the Racket interpreter to support command history?

I've just set up Racket and have been trying out the interpreter. However, I can't seem to go up and down the command history using the arrows (or Ctrlp/Ctrln for that matter) or use Ctrlr to search through it. Is that even supported? Can I build it with that feature...

require/typed contract in racket fails

I've tried looking at the docs but i can't find an example that i can use for my case. I need to import break from srfi/1. Here's my attempt. The example works in #lang racket. #lang typed/racket (require/typed (only-in srfi/1 break) ;[break (All (T) (-> (-> T Boolean) (Listof T)...

Scheme nested lambda function

I am a beginner in Scheme. I found this question in MIT exam 1 for SICP lecture. What's the value and type for – ((lambda (a) (lambda (b) (+ (sqrt a) (sqrt b)))) 5) I am having a hard time understanding how this function works. I am really confused about...

How would I make this Racket code DRYer?

I'm porting a Python script to Racket as a learning experience, and I have this function: (define (check-status) (define git [find-executable-path "git"]) (define-values (ckot out in err) (subprocess #f #f #f git "checkout" "-q" "master")) (define-values (local lout lin lerr) (subprocess #f #f #f git "rev-parse" "@")) (define-values (remote rout...

output variable value to new file

Currently I have the following code: (define (write host code) (with-output-to-file host (lambda () (printf code)))) (let ([myself (find-system-path 'run-file)] [test (substring myself 1 3)]) (printf "~s\n" myself) (write "hello.txt" myself)) I'm trying to write the value of variable "myself". Sorry for being such a noob, I'm not a huge...

racket - algorithm to cut din Ax

I´m developing a mini-game where I need to find out how many lines I can paint in a DIN-A0 paper if I get a DIN-Ax as input . For example: ;car horizontal and cdr vertical lines (define A1 (list 0 1)) (define A2 (list 1 1)) (define A3 (list 1...

Translate Scheme closure-defining function into Haskell

The following procedure, written in Scheme, (define (eat xs) (lambda (x) (if (eq? x 'vomit) (reverse xs) (eat (cons x xs))))) can be called in the following way: > eat #{procedure 9165 eat} > (eat '()) #{procedure 9166 (unnamed in eat)} > ((eat '()) 1) #{procedure 9166 (unnamed in eat)}...

Scheme define-macro and/or define-syntax

I want to create an overloaded Scheme macro for a simple form of polymorphism. That is, a macro smart enough to expand differently when given params of different types, so that (look-up key container) does the "right" thing for different kinds of containers. (define-macro (look-up key container) (cond ((table? container)...

Racket\Scheme compare and delete unwanted items in a list

I have 2 lists: (Define list1 '("xx1" "xx2" xx3" "xx4" "xx5")) (Define list2 '("xx2" "xx4" "xx5")) the items in the list above are just an example, but either way it will be a string item. What I need to do is compare both lists and remove the items in list1...

display executing name of the current running script

I have tried a few methods, one of them being: (define (program) (find-system-path 'pref-file)) I have read from the documentation (after attempting the above code) and have noticed it is not what i'd need to use, obviously :) Any ideas? Would also like to save this information to a variables....

No-argument (and) returns t

Both CL and Scheme define (and) to return t (or #t) with no arguments. I'm trying to understand the rationale for this. My naive assumption is that an empty set is false, and passing in zero arguments feels like passing in nothing that can be true. Edit: clojure follows the...