scheme

Returning from a function inside when statement

巧了我就是萌 提交于 2019-12-10 14:23:24
问题 All I'm trying to do is use a when statement to return a value :( I want the functionality of: if(x) return y And I'm trying to use: (when (x) y) But the when statement is not evaluating in a way that exits the function and return y. It just happily carries on to the next line. Is there a way to do this without making an extremely ugly looking if-else block? mzscheme/racket does not allow 1-armed ifs. 回答1: You tagged this as both Common Lisp and Racket, which are two completely different

How to obtain my function definition in MIT Scheme?

こ雲淡風輕ζ 提交于 2019-12-10 14:09:32
问题 In JavaScript, I can retrieve the "source code" definition of a function, for example: ​function alert_Hi() { alert("Hi"); } alert(alert_Hi); will return exactly what I typed. http://jsfiddle.net/DuCqJ/ How can I do this in MIT Scheme? I remember seeing something that returns #compound-procedure or something, but what I really want is the "source code". 回答1: You might try pp (define (display-hi) (display "Hi")) (pp display-hi) => (named-lambda (display-hi) (display "Hi")) MIT-Scheme debugging

Difference between eq? and = in Scheme?

不羁岁月 提交于 2019-12-10 13:40:35
问题 > (eq? 1 1) #t > (eq? 1.1 1.1) #f > (= 1.1 1.1) #t This is the interaction window in DrScheme. Could somebody please explain the difference between = and eq? in Scheme? 回答1: = compares numbers. eq? tests if the parameters represent the same data object in memory. eqv? should work in the second case as it tests same as eq? but tests primitives specially. More on equlivence predicates in scheme here. 回答2: I would guess that since eq? evaluates to #f unless its parameters represent the same data

Scheme How To Return Multiple Values?

一世执手 提交于 2019-12-10 13:28:08
问题 I notice that almost all scheme functions can only return one list as output. In the following, I would like to return multiple values of all the adjacent nodes of neighbors. (define (neighbors l w) (if (and (= 1 l) (= 1 w)) (list (and (l (+ 1 w))) (and (+ 1 l) w)))) ; how to output 2 or more values? In this case I'm first testing if the node is at corner, if so, return 2 values of the coordinates where (l and w+1), (l+1 and w) basically if I'm at (1,1) return me (1,2) and (2,1) Same applies

What is the definition of “natural recursion”?

我的梦境 提交于 2019-12-10 12:43:24
问题 The Third Commandment of The Little Schemer states: When building a list, describe the first typical element, and then cons it onto the natural recursion. What is the exact definition of "natural recursion"? The reason why I am asking is because I am taking a class on programming language principles by Daniel Friedman and the following code is not considered "naturally recursive": (define (plus x y) (if (zero? y) x (plus (add1 x) (sub1 y)))) However, the following code is considered

Scheme, how do you append a list with a single item?

二次信任 提交于 2019-12-10 12:17:47
问题 I'm trying to make a function to append a list with a single item. What it's doing though is returning a dot pair. (define (append lst x) (cond ((null? lst) x) (else (cons (car lst) (append (cdr lst) x))))) The output I'm getting is > (append '(1 2 3 4 5 6 7) 8) (1 2 3 4 5 6 7 . 8) I'm trying to get (1 2 3 4 5 6 7 8) Thanks. 回答1: Try this: (define (append lst x) (cond ((null? lst) (cons x '())) ; here's the change (else (cons (car lst) (append (cdr lst) x))))) Notice that all proper lists

delete every nth item in scheme

和自甴很熟 提交于 2019-12-10 12:17:08
问题 Trying to delete every nth item in scheme recursively (define x '(1 2 3 4 5 6 7 8 15 10)) (define ndelete (lambda (alist nth) ;@params (list to delete items from) (nth intervals to delete items) (cond [(null? alist) alist] ;if null, return empty list [(if (= nth 1) (ndelete (cdr alist) nth))] [else (list (car alist) (ndelete (cdr alist) (- nth 1)))] ))) when i call: > (ndelete x 5) the output should be: (1 2 3 4 6 7 8 15) but i get blank output: > (ndelete x 5) > 回答1: At the (= nth 1)

ANTLR grammar for scheme R5RS

跟風遠走 提交于 2019-12-10 11:25:17
问题 I'm beginner in ANTLR and I'm learning it by an example. I use C as my target language. The example is a Scheme R5RS grammar file taken from this question, with a little modification(rename the grammar name and add some options with the grammar specification untouched). antlr generated the lexer and parser, and I compile it with a test main() in which I just do some initialization and simply call the parser. When runing the test program with a piece of scheme code, the parser detect some

Little Schemer: why wrap (mk-length mk-length) into a function?

Deadly 提交于 2019-12-10 10:47:55
问题 In The Little Schemer book, in Chapter 9, while building a length function for arbitrary long input, the following is suggested (on pages 170-171), that in the following code snippet (from page 168 itself): ((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) ((lambda (length) (lambda (l) (cond ((null? l) 0) (else (add1 (length (cdr l))))))) (mk-length mk-length)))) the part (mk-length mk-length) , will never return and will be infinitely applying itself to itself: Because we just

applicative-order/call-by-value and normal-order/call-by-name differences

前提是你 提交于 2019-12-10 10:38:54
问题 Background I am learning the sicp according to an online course and got confused by its lecture notes. In the lecture notes, the applicative order seems to equal cbv and normal order to cbn. Confusion But the wiki points out that, beside evaluation orders(left to right, right to left, or simultaneous), there is a difference between the applicative order and cbv: Unlike call-by-value, applicative order evaluation reduces terms within a function body as much as possible before the function is