racket

How to find the maximum nesting depth of a S-expression in scheme?

拈花ヽ惹草 提交于 2021-02-05 07:33:20
问题 for example (nestFind '(a(b)((c))d e f)) => 3 (nestFind '()) => 0 (nestFind '(a b)) => 1 (nestFind '((a)) )=> 2 (nestFind '(a (((b c d))) (e) ((f)) g)) => 4 this is what i tried so far but its not working properly: (define (nestFind a) (cond ((null? a)0) ((atom? a)0) ((atom? (car a))(+ 1 (nestFind (cdr a)))) (else (+(nestFind (car a))(nestFind (cdr a)))))) 回答1: It's a bit simpler. Give this a try: (define (nestFind lst) (if (not (pair? lst)) 0 (max (add1 (nestFind (car lst))) (nestFind (cdr

why does apply map and to list of expressions returns only one boolean?

笑着哭i 提交于 2021-01-29 05:45:50
问题 (define (cart . lists) (cond ((null? lists) '()) ((= (length lists) 1) (map list (first lists))) (else (append-map (lambda(x) (map (lambda(y) (cons y x)) (first lists))) (apply cart (rest lists)))))) (define (numbers n) (define (reversed-numbers n) (if (= n 0) '() `(,n . ,(reversed-numbers (- n 1))))) (reverse (reversed-numbers n))) (define (gen-truth n vals) (apply cart (map (lambda(x) list vals) (numbers n)))) (gen-truth 2 '(#t #f)) returns: '((#t #t) (#f #t) (#t #f) (#f #f)) (define and-l

How to do define-for-syntax in typed racket?

混江龙づ霸主 提交于 2021-01-28 19:37:58
问题 This works #lang racket (begin-for-syntax (define (foo n) (+ n 3))) So I would also expect this to work #lang typed/racket (: foo : Real -> Real) (define-for-syntax (foo n) (+ n 3)) But if fails with ; :: undefined; ; cannot reference an identifier before its definition After that I tried each of the following in turn in typed/racket (define-for-syntax (foo (n : Real)) : Real (+ n 3)) (begin-for-syntax (: foo (-> Real Real)) (define (foo n) (+ n 3))) (begin-for-syntax (define (foo (n : Real))

Why does this return a list '(5) rather than the number 5?

放肆的年华 提交于 2021-01-28 12:01:02
问题 I am working through SICP, and the exercise I am working on asks for a procedure that returns the last element in a list. I implemented the procedure last-pair to do this, but I'm confused why it's returning a list rather than a number: (define (last-pair alist) (cond ((null? (cdr alist)) (car alist)) ; still happens if this is just "car alist)" (else (last-pair (cdr alist))))) When I invoke it on a list of the integers from 1 to 5, I get the output '(5): > (last-pair (list 1 2 3 4 5)) '(5) I

Why does this not evaluate in Scheme?

元气小坏坏 提交于 2021-01-28 05:40:48
问题 I am using the DrRacket environment to try out the Scheme language. I defined sum+1 as follows: (define sum+1 '(+ x y 1)) I was wondering why the following expression does not evaluate: (let ([x 1] [y 2]) (eval sum+1)) whereas doing this returns the correct value: (define x 1) (define y 2) (eval sum+1) 回答1: eval does not work with lexical variables at all unless the lexical variable was created in the same expression: #!r7rs (import (scheme base) (scheme eval)) (define env (environment '

Why does the Racket interpreter write lists with an apostroph before?

拟墨画扇 提交于 2021-01-28 04:49:02
问题 Why is '(1 2 3) written instead of (1 2 3) ? > (list 1 2 3) '(1 2 3) 回答1: Racket's default printer prints a value as an expression that would evaluate to an equivalent value (when possible). It uses quote (abbreviated ' ) when it can; if a value contains an unquotable data structure, it uses constructor functions instead. For example: > (list 1 2 3) '(1 2 3) > (list 1 2 (set 3)) ;; sets are not quotable (list 1 2 (set 3)) Most Lisps and Schemes print values using the write function instead.

Count digits in list Racket

旧城冷巷雨未停 提交于 2021-01-27 20:04:12
问题 I have a doubt, I'm using Racket, and I wanna count the digits of a list, but I can't. I try with length but it doesn't work as I want because (countDigits '(4 5 6 78)) > 5 the answer has to be 5 but, i don't know how to, i have a code for count digits in a number but I don't knowhow to do it in a list. ¿How could I do it? 回答1: Here's a possible solution: (define (countDigits lst) (apply + (map (compose string-length number->string) lst))) Explanation: For each number in the list, we convert

Macro stepper in DrRacket

拟墨画扇 提交于 2021-01-27 08:00:49
问题 On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper. However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term". So my question is: how i must configure macro stepper to get the second expansion, like in tutorial? 回答1: I'm assuming that your source program looked something like this:

Macro stepper in DrRacket

一笑奈何 提交于 2021-01-27 07:54:50
问题 On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper. However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term". So my question is: how i must configure macro stepper to get the second expansion, like in tutorial? 回答1: I'm assuming that your source program looked something like this:

Macro stepper in DrRacket

倖福魔咒の 提交于 2021-01-27 07:52:50
问题 On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper. However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term". So my question is: how i must configure macro stepper to get the second expansion, like in tutorial? 回答1: I'm assuming that your source program looked something like this: