scheme

Scheme: Iterative process to reconstruct a list in original order?

别等时光非礼了梦想. 提交于 2021-02-11 04:55:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends

Scheme: Iterative process to reconstruct a list in original order?

不问归期 提交于 2021-02-11 04:54:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends

Dr Racket, R5RS and SRFI

只谈情不闲聊 提交于 2021-02-11 01:15:17
问题 Whenever I try to use srfi/1 functions like fold and reduce in drracket r5rs language I get an null-list? error. After some research I found that it is due to the fact that the function requires and immutable list but gets a mutable one. How do I create immutable list in r5rs or is this srfi/1 not designed with r5rs in mind? 回答1: This is basically reiterating what John said, but it definitely does merit a full answer status. (John, I'll be happy to delete this if you copy this answer verbatim

Why does my lazy filtered list in scheme consume so much memory?

雨燕双飞 提交于 2021-02-10 21:54:10
问题 I'm currently learning to use some slightly more advanced features of scheme, and I've hit a road-block with lazy lists. Basically, I'm trying to create an infinite, lazily generated list, and apply a lazy filter on it, and only take a single element. My hope was that this would consume very little memory: the filter looks at only one element at a time, and there's no need to store the previous entries. Here is my attempt at this: (define lazy-inf-seq (lambda (start next) (delay (cons start

Use mit-scheme with REPL and editor together

拈花ヽ惹草 提交于 2021-02-10 20:12:10
问题 I'm going through SICP course and as recommended installed mit-scheme. I want to use the REPL together with a scheme file. The reason is because I can add scheme code in the file and then run the commands in REPL. What I have works, but the problem is every time I edit the file, I have to quit terminal and reload the file for REPL to see changes. Is there a way to reload the file easily or some other way for REPL to see changes from the file? This my setup: I installed mit-scheme with brew

Scheme Error: execute: unbound symbol: “error”

孤街浪徒 提交于 2021-02-10 19:38:19
问题 I am learning Scheme language in school and trying to use error function to handle corner cases. When I try this code from a similar question, I got an error Error: execute: unbound symbol: "error" [in?] instead of printing out the error message. This is the example code from the link above: (define in? (lambda (el lst) (if (or (null? lst) (pair? lst)) (if (null? lst) #f (if (equal? (car lst) el ) #t (in? el (cdr lst)))) (error "ERROR")))) The input and output should be like: (in? 1 '(2 5 3))

Seeking clarification on Scheme eval

吃可爱长大的小学妹 提交于 2021-02-10 18:31:52
问题 I am confused about eval . I looked at the specification of eval in schemers.org. It says procedure: (eval expression environment-specifier) It indicates to me that environment-specifier is mandatory requirement. However, when I tested eval using two interpreters -- the one at repl.it and Elk Scheme -- both of them work without environment-specifier . My question is: Are they both non-conformant interpreters or did I read the documentation at schmers.org wrong? And then.. Elk Scheme has no

Seeking clarification on Scheme eval

∥☆過路亽.° 提交于 2021-02-10 18:28:05
问题 I am confused about eval . I looked at the specification of eval in schemers.org. It says procedure: (eval expression environment-specifier) It indicates to me that environment-specifier is mandatory requirement. However, when I tested eval using two interpreters -- the one at repl.it and Elk Scheme -- both of them work without environment-specifier . My question is: Are they both non-conformant interpreters or did I read the documentation at schmers.org wrong? And then.. Elk Scheme has no

Scheme zip function with possible uneven lists

笑着哭i 提交于 2021-02-10 17:27:42
问题 I know this question has been asked before, and my solution is the same as many of the answers but I have a special test case that won't work correctly with the common solution to this problem. The solution that I have found for the zip problem like many others is (define (zip l1 l2)(map list l1 l2)) . . .which works great with given arguments such as (zip '(a b c) '(1 2 3)) => ((a 1) (b 2) (c 3)) but I also want the zip function to work for cases where my arguments do not match length like

Scheme zip function with possible uneven lists

懵懂的女人 提交于 2021-02-10 17:26:39
问题 I know this question has been asked before, and my solution is the same as many of the answers but I have a special test case that won't work correctly with the common solution to this problem. The solution that I have found for the zip problem like many others is (define (zip l1 l2)(map list l1 l2)) . . .which works great with given arguments such as (zip '(a b c) '(1 2 3)) => ((a 1) (b 2) (c 3)) but I also want the zip function to work for cases where my arguments do not match length like