scheme

What is a 'thunk', as used in Scheme or in general?

瘦欲@ 提交于 2019-12-18 10:14:44
问题 I come across the word 'thunk' at a lot of places in code and documentation related to Scheme, and similar territories. I am guessing that it is a generic name for a procedure, which has a single formal argument. Is that correct? If yes, is there more to it? If no, please? For eg. in SRFI 18, in the 'Procedures' section. 回答1: It is really simple. When you have some computation, like adding 3 to 5, in your program, then creating a thunk of it means not to calculate it directly, but instead

Lazy Evaluation vs Macros

依然范特西╮ 提交于 2019-12-18 10:05:22
问题 I'm used to lazy evaluation from Haskell, and find myself getting irritated with eager-by-default languages now that I've used lazy evaluation properly. This is actually quite damaging, as the other languages I use mainly make lazily evaluating stuff very awkward, normally involving the rolling out of custom iterators and so forth. So just by acquiring some knowledge, I've actually made myself less productive in my original languages. Sigh. But I hear that AST macros offer another clean way

Which Lisp should I learn? [closed]

南楼画角 提交于 2019-12-18 09:56:49
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Which Lisp (dialect) should I learn, and why? The fragmentation between CL and Scheme slows uptake (at least for me!). So, give me the

Counting elements of a list and sublists

旧巷老猫 提交于 2019-12-18 09:29:16
问题 I'm trying to create a function to count all the elements in a list, including the elements of its sublists. initially, to get started, i came up with a basic function myList : (define myLength (lambda (L) (cond ((null? L) 0) (else (+ 1 (myLength (cdr L))))))) However, it doesn't help me account for function calls like: (numAtoms '()) "...should be 0" (numAtoms '(())) "...should be 0" (numAtoms '(1 1)) "...should be 2" (numAtoms '(1 (1 1) 1)) "...should be 4" (numAtoms '(1 (1 (1 1)) 1)) "..

Understanding the environment model of evaluation

笑着哭i 提交于 2019-12-18 09:16:41
问题 Exercise 3.20 in SICP: Draw environment diagrams to illustrate the evaluation of the sequence of expressions (define x (cons 1 2)) (define z (cons x x)) (set-car! (cdr z) 17) (car x) 17 using the procedural implementation of pairs given above. My eyes are destroyed so I cannot draw. I will instead try to imagine as best as I can how the environment model evolves. First, here is the procedural pairs implementation. (define (cons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)

Convert number to list of digits

删除回忆录丶 提交于 2019-12-18 09:01:38
问题 How do I convert a number to a list of digits? I am currently doing: ;; (num->list 12345) -> '(1 2 3 4 5) (define (num->list n) (local ((define (num->list n) (map (lambda (c) (char->num c)) (string->list (number->string n)))) (define (char->num c) (- (char->integer c) 48))) (num->list n))) but would like to know if there's a better way. 回答1: This is the digits function of my Standard Prelude: (define (digits n . args) (let ((b (if (null? args) 10 (car args)))) (let loop ((n n) (d '())) (if

Racket/Scheme Flatten Explanations

混江龙づ霸主 提交于 2019-12-18 08:48:19
问题 Can someone help me to break down exactly the order of execution for the following versions of flatten? I'm using Racket. version 1, is from racket itself, while version two is a more common? implementation. (define (flatten1 list) (let loop ([l list] [acc null]) (printf "l = ~a acc = ~a\n" l acc) (cond [(null? l) acc] [(pair? l) (loop (car l) (loop (cdr l) acc))] [else (cons l acc)]))) (define (flatten2 l) (printf "l = ~a\n" l) (cond [(null? l) null] [(atom? l) (list l)] [else (append

How is set! defined in scheme?

Deadly 提交于 2019-12-18 07:24:29
问题 How would you implement your own set! function in Scheme? A set! function is a destructive procedure that changes a value that is defined taking into account the previous value. 回答1: As noted in the comments, set! is a primitive in Scheme that must be provided by the implementation. Similarly, you can't implement the assignment operator, = , in most programming languages. In Common Lisp, setf can be extended (using setf -expanders) to allow (setf form value) to work on new kinds of forms.

How to delete an element from a list in scheme

↘锁芯ラ 提交于 2019-12-18 06:57:28
问题 how to delete an element from a list ex:- list=[1 2 3 4] I have come up with some code.I think I got wrong somewhere. (define delete item (lambda (list) (cond ((equal?item (car list)) cdr list) (cons(car list)(delete item (cdr list)))))) 回答1: Your code is almost correct. The item also should be a parameter, so the function may begin with like this: (define delete (lambda (item list) ... Also, your code needs paren around the cdr list and else in the last clause. Then, the code may be like

ANTLR resolving non-LL(*) problems and syntactic predicates

99封情书 提交于 2019-12-18 05:27:04
问题 consider following rules in the parser: expression : IDENTIFIER | (...) | procedure_call // e.g. (foo 1 2 3) | macro_use // e.g. (xyz (some datum)) ; procedure_call : '(' expression expression* ')' ; macro_use : '(' IDENTIFIER datum* ')' ; and // Note that any string that parses as an <expression> will also parse as a <datum>. datum : simple_datum | compound_datum ; simple_datum : BOOLEAN | NUMBER | CHARACTER | STRING | IDENTIFIER ; compound_datum : list | vector ; list : '(' (datum+ ( '.'