racket

racket/scheme filtering

喜欢而已 提交于 2020-01-14 19:05:11
问题 How would I filter this to display all vegetables? Thank you in advance. ("Pecan" . (1982 .("nut". "AL"))) ("Blackberry" . (2004 .("fruit". "AL"))) ("Peach" . (2006 .("fruit". "AL"))) ("Rice" . (2007 .("grain". "AR"))) ("Orange" . (2005 .("fruit". "FL"))) ("Huckleberry" . (2000 .("fruit". "ID"))) ("Blackberry" . (2004 .("fruit". "KY"))) ("Strawberry" . (1980 .("fruit". "LA"))) ("WildBlueberry" . (1991 .("fruit". "ME"))) ("BlueCrab" . (2000 .("food". "MD"))) ("HoneycrispApple" . (2006 .("fruit

Replace first occurrence of symbol in (possibly nested) list

心不动则不痛 提交于 2020-01-14 18:47:10
问题 I would like to replace just the first occurrence of a certain symbol (say '- ) with another symbol (say '+ ) inside a list that may contain lists. That is to say, '(((-))) would turn into '(((+))) '((-) - b) into '((+) - b) 回答1: Here's another, different option: using mutable state to find out when the first replace has happened: (define (replace-first) (let ((found #f)) (define (replacer exp old new) (cond ((null? exp) '()) ((not (pair? exp)) (cond ((and (eq? exp old) (not found)) (set!

communcation between racket program and python program

二次信任 提交于 2020-01-14 10:39:05
问题 I want communication between racket program and python program. My Racket code: #lang racket (define-values (sp o i e) (subprocess #f #f #f "hello.exe" )) (display "server" i) (flush-output i) (display (read o)) My python code: input_var = raw_input("Enter something: ") print ("you entered " + input_var) If i am just printing in my python program it works fine. If i am reading input from racket program it hangs. I want to read messages from racket. 回答1: It looks like it's hanging because you

Execute the following call/cc expression

孤人 提交于 2020-01-14 04:34:27
问题 I use racket and I got the result 4 for following simple code: (let/cc done ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3)) and I was going to execute this code step-by-step. First, I changed the first let/cc into the form of call/cc like below: (call/cc (λ (done) ((let/cc esc (done (+ 1 (let/cc k (esc k))))) 3))) Of course, this produces 4 also. Second, since I found the mechanism of call/cc in the internet which says call/cc do following 4 steps: Captures the current continuation.

Mapping multiple functions, in order, over a single list [closed]

与世无争的帅哥 提交于 2020-01-11 12:00:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I would like to map three different functions, in order, over a single list. To demonstrate what I mean, say we want to do the following three mappings: (map foo mylist) (map bar mylist) (map foobar mylist) If we define mylist as '(1 2 3) , and we run the above functions one at a time, we get: (map foo mylist) =

Delete element from List in Scheme

蓝咒 提交于 2020-01-11 10:52:08
问题 I have list in this form ( (1 3) (2 2) (3 1) (4 5) (5 1))) and I want to delete an item let's say (3 1) So the result will be ( (1 3) (2 2) (4 5) (5 1))) I have written something like this and I do not know why it is not running correctly. (define (deleteItem list item) (cond ((equal? item (car list)) (cdr list)) (cons (car list)(deleteItem(cdr list) item)))) 回答1: There's a built-in function for this, it's called remove: (define lst '((1 3) (2 2) (3 1) (4 5) (5 1))) (remove '(3 1) lst) => '(

Improving performance for converting numbers to lists, and base10 to base2

◇◆丶佛笑我妖孽 提交于 2020-01-10 04:26:06
问题 Many Project Euler problems require manipulating integers and their digits, both in base10 and base2. While I have no problem with converting integers in lists of digits, or converting base10 into base2 (or lists of their digits), I often find that performance is poor when doing such conversions repeatedly. Here's an example: First, here are my typical conversions: #lang racket (define (10->bin num) (define (10->bin-help num count) (define sq (expt 2 count)) (cond [(zero? count) (list num)]

Combine two lists of 3 characters into 3 pairs

 ̄綄美尐妖づ 提交于 2020-01-07 07:48:09
问题 I'm having a little trouble with this. Basically, I need a procedure comb that takes two lists (comb '(a b c) '(1 2 3) and returns ('a 1)('b 2)('c 3) . I came up with a part of the cod which returns the first pair (define some-letters '(a b c)) (define some-nums '(1 2 3)) (define x (first (foldr cons empty some-letters))) (define y (first (foldr cons empty some-nums))) (define (comb list1 list2) (cond [(empty? list1) empty] [(empty? list2) empty] [else (list x y)])) Now, I tinkered around a

File I/O In DrScheme

馋奶兔 提交于 2020-01-07 04:52:05
问题 (read) takes in a string from stdin, parses it as an s-expression, and returns that expression. How do I do the exact same thing, except taking input from a file? 回答1: Any of these: (call-with-input-file "foo" read) (with-input-from-file "foo" read) The first will open the file and apply read on the open port to read a value and finally close it. The second is similar, except that it applies the function on no arguments in a dynamic context where the current input is read from the file. There

Longest common sublist

故事扮演 提交于 2020-01-06 02:47:06
问题 While trying to write a solution to the longest common sublist problem in Scheme, I'm having trouble figuring out what is wrong with what I have so far. I think it's the right idea and before worrying about polynomial time I'm just trying to get one that works at all. I haven't written in a functional language before and the syntactic differences can make things a little harder at first. (define (lcs lst1 lst2) (if (or (null? lst1) (null? lst2)) '() (if (not (null? lcs)) lcs (if (equal? (car