scheme

Tail-Recursive Power Function in Scheme

时间秒杀一切 提交于 2019-12-24 00:57:55
问题 I am having trouble writing a tail-recursive power function in scheme. I want to write the function using a helper function. I know that I need to have a parameter to hold an accumulated value, but I am stuck after that. My code is as follows. (define (pow-tr a b) (define (pow-tr-h result) (if (= b 0) result pow-tr a (- b 1))(* result a)) pow-tr-h 1) I edited my code, and now it works. It is as follows: (define (pow-tr2 a b) (define (pow-tr2-h a b result) (if (= 0 b) result (pow-tr2-h a (- b

defining map in scheme to take many lists

…衆ロ難τιáo~ 提交于 2019-12-24 00:26:32
问题 I am creating an evaluator for scheme in scheme, and one of the features I want it to have is map. However, all the definitions for map I've found do not allow for multiple lists. For example: (define (my-map proc lis) (cond ((null? lis) '()) ((pair? lis) (cons (proc (car lis)) (my-map proc (cdr lis)))))) This definition of map is 'incomplete' because, for example, you cannot add 2 lists of numbers with it like so (it only allows one list as an argument): (my-map + '(1 2 3) '(4 5 6)) How do I

File Stats in Scheme

岁酱吖の 提交于 2019-12-23 18:13:24
问题 I'll be upfront: this is Homework. The following code defines a function countup , called as: (countup "file1") It appears that the loop is running indefinitely. Why, and how can I fix this? (define stats (lambda (srcf) (begin (define in (open-input-file srcf)) (let loop ( (l 0) (w 0) (c 0) (char (read-char in))) (case char ((#\newline) (loop (+ l 1) w(+ c 1) (read-char in))) ((#\space #\tab) (loop l (+ w 1) (+ c 1) (read-char in))) (else (loop l w (+ c 1) (read-char in)))) ) (close-input

Swap two elements in list in Scheme [closed]

孤街浪徒 提交于 2019-12-23 16:06:02
问题 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 7 years ago . I need to switch 2 elements on entered indexes in list in Scheme lang. For example: (swap-index 0 3 '(1 2 3 4 5)) (4 2 3 1 5) Can

(load “file.scm”) in a New Environment in Scheme

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 13:07:42
问题 MIT Scheme's (load ...) procedure apparently takes in an environment as a parameter. Is there any way I could "clone" the current environment and pass it there, so that I can isolate the file's environment from my own? (I've looked here but I haven't found anything...) 回答1: How about something like this ? (define (clone-env env) (let ((bindings (environment-bindings env))) (make-top-level-environment (map car bindings) (map cadr bindings)))) 1 ]=> (define foo 1) ;Value: foo 1 ]=> (eq? (the

Unable to use eval on user input in Racket

人走茶凉 提交于 2019-12-23 09:50:35
问题 I'm currently learning Scheme (using Racket), but one of the challenges I'm coming upon is trying to execute the following bit of code, which is meant to execute Racket code from user input using eval : (display (eval (read))) Here's some of the weird behavior I've observed so far: (display (eval (read))) in the definition window prompts for keyboard input, as expected, when the definitions are run. However, providing the input ((lambda (x) (+ x 1)) 1) gives the error ?: function application

How to check if a list contains only #t

余生长醉 提交于 2019-12-23 08:50:08
问题 I was trying with the following code in racket and MIT scheme, surprise me that the compiler throw err (foldr and #t '(#t #t #f)) Is there any way to use reduce/fold way to check if a list contains only true or false? I know a lambda can do the job, but it really make we wonder why this is not a valid code. I remember I can do it in Haskell..... TIA. 回答1: and is a macro, so it doesn't have a value by itself. Specifically, it short-circuits evaluation, and using it as you tried to will not

zip function in Racket/Scheme

隐身守侯 提交于 2019-12-23 07:47:14
问题 Given two lists, return a list whose elements are lists of size two, such that for the i -th list, the first element is the i -th element of the first original list, and the second element is the i -th element of the second original list. If one list is smaller than the other, the resulting list is of the smallest size; and so if one of the lists is empty, return an empty list. For example: > (zip '(1 2) '(3 4)) '((1 3) (2 4)) > (zip '(1 2 3) '()) '() > (zip '() '(4 5 6)) '() > (zip '(8 9) '

Can you return nothing from a function in Scheme?

三世轮回 提交于 2019-12-23 07:24:53
问题 I'm writing a scheme interpreter, and in the case of an if statement such as: (if (< 1 0) 'true) Any interpreter I've tried just returns a new prompt. But when I coded this, I had an if for whether there was an alternative expression. What can I return in the if such that nothing gets printed? (if (has-alternative if-expr) (eval (alternative if-expr)) #f) ;; what do I return here? 回答1: According to the R6RS specification: If <test> yields #f and no <alternate> is specified, then the result of

Can you return nothing from a function in Scheme?

筅森魡賤 提交于 2019-12-23 07:24:04
问题 I'm writing a scheme interpreter, and in the case of an if statement such as: (if (< 1 0) 'true) Any interpreter I've tried just returns a new prompt. But when I coded this, I had an if for whether there was an alternative expression. What can I return in the if such that nothing gets printed? (if (has-alternative if-expr) (eval (alternative if-expr)) #f) ;; what do I return here? 回答1: According to the R6RS specification: If <test> yields #f and no <alternate> is specified, then the result of