racket

set-car!, set-cdr! unbound in racket?

守給你的承諾、 提交于 2019-11-26 21:12:57
问题 I am just trying to do very simple code with set-car! and set-cdr! in racket , but I got the error: expand: unbound identifier in module in: set-car! and expand: unbound identifier in module in: set-cdr! Aren't they defined in racket ? Could anyone help? 回答1: You need to import mutable-pairs-6 , like this: (require rnrs/mutable-pairs-6) Those procedures were moved to a different module and renamed to mcons , mcar , mcdr , set-mcar! , set-mcdr! , mlist to emphasize that they operate on mutable

Use of lambda for cons/car/cdr definition in SICP

别说谁变了你拦得住时间么 提交于 2019-11-26 18:54:10
I was just beginning to feel I had a vague understanding of the use of lambda in racket and scheme when I came across the following 'alternate' definitions for cons and car in SICP (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) For the life of me I just cannot parse them. Can anybody explain how to parse or expand these in a way that makes sense for total neophytes? Óscar López This is an interesting way to represent data: as functions. Notice that this definition of cons returns a lambda which closes over the parameters x

a tail-recursion version list appending function

不羁的心 提交于 2019-11-26 17:25:19
问题 i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) 回答1: The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data

Why is foldl defined in a strange way in Racket?

ⅰ亾dé卋堺 提交于 2019-11-26 16:04:55
问题 In Haskell, like in many other functional languages, the function foldl is defined such that, for example, foldl (-) 0 [1,2,3,4] = -10 . This is OK, because foldl (-) 0 [1, 2,3,4] is, by definition, ((((0 - 1) - 2) - 3) - 4) . But, in Racket, (foldl - 0 '(1 2 3 4)) is 2, because Racket "intelligently" calculates like this: (4 - (3 - (2 - (1 - 0)))) , which indeed is 2. Of course, if we define auxiliary function flip, like this: (define (flip bin-fn) (lambda (x y) (bin-fn y x))) then we could

What exactly is a symbol in lisp/scheme?

南楼画角 提交于 2019-11-26 15:45:30
问题 For the love of the almighty I have yet to understand the purpose of the symbol 'iamasymbol . I understand numbers, booleans, strings... variables. But symbols are just too much for my little imperative-thinking mind to take. What exactly do I use them for? How are they supposed to be used in a program? My grasp of this concept is just fail. 回答1: In Scheme and Racket, a symbol is like an immutable string that happens to be interned so that symbols can be compared with eq? (fast, essentially

How is Racket different from Scheme?

荒凉一梦 提交于 2019-11-26 15:00:05
问题 Racket is a descendant of Scheme. How is Racket different than R6RS? What did it add, or take away, or is just different? I understand that Racket is more than a language, it's a platform for languages. But I'm referring to the main Racket dialect. 回答1: Racket is ultimately based on R5RS, and not R6RS and not a strict superset of either. I don't think it can be called 'Scheme' because it's not backwards compatible with any Scheme standard. Most implementations offer extensions, but are

I got “scheme application not a procedure” in the last recursive calling of a function

好久不见. 提交于 2019-11-26 12:47:48
问题 so here is the code: (define (time-prime-test n) (newline) (display n) (start-prime-test n (runtime))) (define (start-prime-test n start-time) (if (prime? n) (report-prime (- (runtime) start-time)))) (define (report-prime elapsed-time) (display \" *** \") (display elapsed-time)) (define (search-for-primes n m) (if (< n m) ((time-prime-test n) (search-for-primes (+ n 1) m)) (display \" calculating stopped. \"))) (search-for-primes 100000 100020) and i got this error after \"calculating stopped

How do I find the index of an element in a list in Racket?

人走茶凉 提交于 2019-11-26 12:45:22
问题 This is trivial implement of course, but I feel there is certainly something built in to Racket that does this. Am I correct in that intuition, and if so, what is function? 回答1: Strangely, there isn't a built-in procedure in Racket for finding the 0-based index of an element in a list (the opposite procedure does exist, it's called list-ref). However, it's not hard to implement efficiently: (define (index-of lst ele) (let loop ((lst lst) (idx 0)) (cond ((empty? lst) #f) ((equal? (first lst)

“application: not a procedure” in binary arithmetic procedures

浪子不回头ぞ 提交于 2019-11-26 11:25:48
I have a simple Racket definition for multiplying binary numbers together. It uses a well-tested "addWithCarry" definition that takes three parameters: two lists and a carry digit and returns the binary sum. The binary numbers are represented as lists in reverse order. I stepped through the test line with the debugger, and it goes through the recursion properly. It performs the multBins each time shrinking the y list as appropriate, then conducts the addWithCarry functions as expected. As it rises back up the stack, it suddenly throws an exception "application: not a procedure, expected a

How to do a powerset in DrRacket?

与世无争的帅哥 提交于 2019-11-26 09:55:06
问题 I\'m using the beginning language with list abbreviations for DrRacket and want to make a powerset recursively but cannot figure out how to do it. I currently have this much (define (powerset aL) (cond [(empty? aL) (list)] any help would be good. 回答1: What's in a powerset? A set's subsets! An empty set is any set's subset, so powerset of empty set's not empty. Its (only) element it is an empty set: (define (powerset aL) (cond [(empty? aL) (list empty)] [else As for non-empty sets, there is a