racket

Why is foldl defined in a strange way in Racket?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 12:34:37
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 in Racket achieve the same behavior as in Haskell: instead of (foldl - 0 '(1 2 3 4)) we can write:

What exactly is a symbol in lisp/scheme?

谁说我不能喝 提交于 2019-11-27 12:02:52
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. 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 pointer comparison). Symbols and strings are separate data types. One use for symbols is lightweight

How is Racket different from Scheme?

末鹿安然 提交于 2019-11-27 10:04:17
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. Zorf 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 otherwise backwards compatible, of course, the compiler that comes with Racket can also run in R5RS or R6RS

Dr Racket problems with SICP

戏子无情 提交于 2019-11-27 10:00:13
问题 I'm working through SICP. Currently, in the first chapter, I'm having problems getting Racket to let me redefine "primitives". For instance, I was under the impression that I should be able to arbitrarily do (define + 5) and that would be fine, or redefine the sqrt procedure. Instead, I get this: define-values: cannot change constant variable: + I have the language currently set to R5RS, which I was under the impression would take care of the compatibility issues with SICP. 回答1: Even if

Expanded form of fold in Racket

半城伤御伤魂 提交于 2019-11-27 08:47:57
问题 Example from http://www.cse.unsw.edu.au/~en1000/haskell/hof.html : (foldr / 7 (list 34 56 12 4 23)) (foldl / 7 (list 34 56 12 4 23)) Output in Racket: 5 193/196 5 193/196 What would be the full (expanded) form of foldl and foldr in this case? It is not the following: > (/ (/ (/ (/ (/ 7 34) 56) 12) 4) 23) 1/300288 Edit: I have modified above question since implementation of fold in Racket vs Haskell has been explained in another question Why is foldl defined in a strange way in Racket?. Edit:

How do you return the description of a procedure in Scheme?

点点圈 提交于 2019-11-27 08:10:25
问题 Suppose I have something like this: (define pair (cons 1 (lambda (x) (* x x)) If I want to return the front object of the pair I do this: (car pair) And it returns 1. However when the object is a procedure I don't get the exact description of it. In other words: (cdr pair) returns #<procedure> and not (lambda (x) (*x x)) . How do I fix this? 回答1: Although there's no way to do this generally, you can rig up something to do it for procedures that you define. Racket struct s can define a prop

Transpose a matrix in racket (list of lists

百般思念 提交于 2019-11-27 06:15:13
问题 I got a list of lists in racket and have to transpose them. (: transpose ((list-of(list-of %a)) -> (list-of (list-of %a)))) (check-expect (transpose (list (list 1 2 3) (list 4 5 6))) (list (list 1 4) (list 2 5) (list 3 6))) (define transpose (lambda (xs) (cond ((empty? xs)empty) ((pair? xs)(make-pair (make-pair (first(first xs)) (make-pair (first(first(rest xs)))empty)) (transpose (rest(rest xs)))))))) That's my code at the moment. I think the problem is in the recursive call (correct me if I

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

戏子无情 提交于 2019-11-27 05:28:09
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." has been displayed. like below: 100017 100018 100019 * 54 calculating stopped. . . application: not a

Why is one-armed “if” missing from Racket?

久未见 提交于 2019-11-27 03:03:38
问题 In standard Scheme it is possible to write (if (> x 2) (set! x (- x 1))) but this is not possible in Racket -- Racket's if always requires two arms. Why? 回答1: Rationale The one-armed variant of if was removed from Racket to prevent bugs. In functional code one always uses the two-armed variant of if . (if test expr-on-true expr-on-false) Forgetting the second arm expr-on-false would not lead to a syntax-error, but to a runtime error (the expression would return #<void> ). To prevent these

How to do a powerset in DrRacket?

人盡茶涼 提交于 2019-11-27 02:06:19
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. 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 choice , for each set's element, whether to be or not to be included in subset which is a member of a