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


scheme/racket: breaking a list into list of lists

Question:

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))
           (list 

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


Answer:

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)
      empty
      (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"))

Related:


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


racket,packages
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?...

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


scheme,racket
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...

Using match with user defined types in PL Racket


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 write lex file for input like “{\”a\“:1,\”b\“:2}”


json,parsing,racket,tokenize,lex
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\""...

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


scheme,racket,grammar,bnf
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,...

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


racket,scribble
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...

Racket lexer - return list of cons


parsing,racket
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=\"https://foo.com/WebTicket/oauthtoken\",grant_type=\"urn:microsoft.rtc:windows,urn:microsoft.rtc:anonmeeting,password\", Bearer trusted_issuers=\"\",...

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


lambda,scheme,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...

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


lambda,scheme,sicp,r5rs
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...

“Mapping” Little Schemer to coderbyte challenge: Capitalize


javascript,arrays,recursion,scheme,the-little-schemer
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


racket
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...

Generating n-grams with foldl in Racket


scheme,racket,fold
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...

Scheme several actions if an if-statement proves true


if-statement,syntax,scheme
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...

display executing name of the current running script


racket
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....

Writing a `define-let` macro, with hygiene


macros,racket,define-syntax,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...

How to configure the Racket interpreter to support command history?


racket,readline,input-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...

Racket\Scheme compare and delete unwanted items in a list


scheme,racket
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...

Append string to existing textfile in IronScheme


file,scheme,append,r6rs,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...

Racket not closing TCP port


racket
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...

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


scheme
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...

Create lists from lists


scheme,racket
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...

dr racket code error beginning student language


racket
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")))...

abstracting away match in racket


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 _ _ _ _ _ _ _...

Accessing call stack depth in Scheme


functional-programming,scheme,tail-recursion,callstack
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.)?...

Find empy lines in text file


racket
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...

Racket: Graphing a parabola with elements from a list


list,function,plot,racket
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...

output variable value to new file


racket
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...

Translate Scheme closure-defining function into Haskell


haskell,recursion,types,scheme,closures
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)}...

No-argument (and) returns t


clojure,scheme,lisp,common-lisp
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...

require/typed contract in racket fails


scheme,racket,typed-racket
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)...

Using MIT/GNU Scheme


scheme,edwin
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...

How would I make this Racket code DRYer?


scheme,racket
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...

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


scheme,continuations
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...

Scheme define-macro and/or define-syntax


macros,polymorphism,scheme,overloading
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)...

Writing a While Loop in Scheme


loops,recursion,lambda,scheme,r5rs
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...

Implement yield and send in Scheme


scheme,coroutine,continuations,guile,delimited-continuations
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...

Scheme nested lambda function


function,lambda,nested,scheme,sicp
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...

racket - algorithm to cut din Ax


algorithm,racket
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...

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


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...

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


recursion,racket,fold
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 get only specific elements of list in racket


functional-programming,scheme,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....