racket

Using scheme/racket to return specific items from a list

[亡魂溺海] 提交于 2020-01-04 02:30:48
问题 What I would like to do is create a function that takes in a list of values and a list of characters and coalesce the corresponding characters("atoms" I think they would technically be called) into a new list. Here is what I have so far; #lang racket (define (find num char) (if (= num 1) (car char) ;Problem here perhaps? (find (- num 1) (cdr char)))) (define (test num char) (if (null? num) '("Done") (list (find (car num) (test (cdr num) char))))) This however gives me an error, which for the

Remove integers from list

匆匆过客 提交于 2020-01-03 04:25:14
问题 I have a strange problem that couple of hours can't implement in Scheme. Let's say we have: (define x '( (Orlando (NY 3)) (Chicago (Montana 5) (Orlando 8)) ...and so on ... ) I want to transform it to '( (Orlando NY) (Chicago Montana Orlando) ...and so on ... ) Any help would be greatly appreciated. 回答1: You could also try (map (lambda (x) (cons (car x) (map car (cdr x)))) x) 来源: https://stackoverflow.com/questions/20749277/remove-integers-from-list

Implement SICP evaluator using Racket

妖精的绣舞 提交于 2020-01-03 03:25:23
问题 I'm working on metacircular evaluator of 4.1.4 Running the Evaluator as a Program , building which with Racket: #lang racket (require (combine-in rnrs/base-6 rnrs/mutable-pairs-6)) (define (evaluate exp) (cond ; ... ((definition? exp) (display exp) (display " is a definition\n")) ; ... (else (display exp) (display " is something else\n")))) (define (definition? exp) (tagged-list? exp 'define)) (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false)) (define (driver-loop)

Why does '(a . b . c) evaluate to (b a c) in PLT-Scheme 372?

拥有回忆 提交于 2020-01-03 03:10:54
问题 I'm trying to understand the relations between pair , cons , dotted tuples and proper list in PLT-Scheme 372. The detailed context of my question is as follows: After reading some textbook and doing trial-and-error, I have got the following understanding and intuitive ideas (I may be wrong...): all lists are pairs, e.g.: (pair? (list 'a 'b 'c)) => #t all conses are pairs, e.g.: (pair? (cons 'a (cons 'b 'c))) => #t some dot-separated tuples are pairs, e.g.: (pair? '(a . b)) => #t (pair? '(a .

remove duplicate of a list in O(nlogn)

点点圈 提交于 2020-01-02 21:19:07
问题 How to remove duplicate of a list? (running time is O(n log n) ) ex: '(4 6 1 1 2 3 3 5 6) => '(4 6 1 2 3 5) (define (re-dup lst) (cond ((empty? lst) empty) (else (define el (first lst)) (define el-free-lst (filter (lambda (x) (not (= el x))) (rest lst))) (cons el (re-dup el-free-lst))))) Is this right? 回答1: Your current solution is O(n^2) , because filter traverses the list once for each of the elements in the original list. It's possible to write an O(n) solution using a helper data

How do I include files in DrScheme?

雨燕双飞 提交于 2020-01-02 08:53:11
问题 I'm using DrScheme to work through SICP, and I've noticed that certain procedures (for example, square ) get used over and over. I'd like to put these in a separate file so that I can include them in other programs without having to rewrite them every time, but I can't seem to figure out how to do this. I've tried: (load filename) (load (filename)) (load ~/path-to-directory/filename) (require filename) (require ~/path-to-directory/filename) (require path-from-root/filename) None of these

Scheme code cond error in Wescheme

。_饼干妹妹 提交于 2020-01-02 08:13:05
问题 Although the following code works perfectly well in DrRacket environment, it generates the following error in WeScheme: Inside a cond branch, I expect to see a question and an answer, but I see more than two things here. at: line 15, column 4, in <definitions> How do I fix this? The actual code is available at http://www.wescheme.org/view?publicId=gutsy-buddy-woken-smoke-wrest (define (insert l n e) (if (= 0 n) (cons e l) (cons (car l) (insert (cdr l) (- n 1) e)))) (define (seq start end) (if

Methods and properties in scheme: is OOP possible in Scheme?

孤街浪徒 提交于 2020-01-02 05:28:32
问题 I will use a simple example to illustrate my question. In Java, C, or any other OOP language, I could create a pie class in a way similar to this: class Apple{ public String flavor; public int pieces; private int tastiness; public goodness(){ return tastiness*pieces; } } What's the best way to do that with Scheme? I suppose I could do with something like this: (define make-pie (lambda (flavor pieces tastiness) (list flavor pieces tastiness))) (define pie-goodness (lambda (pie) (* (list-ref

Using trace to display a procedure in racket

北城以北 提交于 2020-01-02 04:46:06
问题 I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver. It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been

Variable in a function

故事扮演 提交于 2020-01-02 03:30:11
问题 I have see the following code... The first call of (next-num) returns 1 , and the second returns 2 . (define next-num (let ((num 0)) (lambda () (set! num (+ num 1)) num))) (next-num) ; 1 (next-num) ; 2 What I can not understand is... num is created by let inside next-num , it is kind of a local variable... How does scheme know that each time next-num is called, the value of num is not erased by let ((num 0)) ; How does scheme know that it is always the same num that we modify whenever next