scheme

Scheme getting error is not a function

坚强是说给别人听的谎言 提交于 2019-12-08 04:13:28
问题 ,i am doing my assignment in Scheme. I am using Scheme MIT Interpreter and https://repl.it/languages/scheme to test my code. First question is ; - in? procedure takes an element ‘el’ and a list ‘lst’. ; - It returns a boolean value. ; - When el is in lst it returns true, otherwise returns false. ; - Examples: ; (in? 3 ’(2 5 3)) ; evaluates to #t ; (in? 2 ’(1 (2) 5)) ; evaluates to #f ; - If lst is not a list, it produces an error.. my code is (define lst()) (define el()) (define in? (lambda

defining map in terms of reduce

帅比萌擦擦* 提交于 2019-12-08 02:27:23
问题 I have just picked up John Hughes' essay Why Functional Programming Matters, and i'm trying to follow along using Scheme. The first higher order function he defines is reduce, which i've defined in Scheme as follows: (define u-reduce (lambda (ff init lst) (if (null? lst) init (ff (car lst) (u-reduce ff init (cdr lst)))))) I can re-create some of the applications of reduce in the essay using this function, however things break apart in the move from reduce to map . The motivating example is

Getting all possible combinations of x booleans (Racket, Scheme)

耗尽温柔 提交于 2019-12-08 01:52:42
问题 i have a problem. How do I get all possible combinations of x booleans in Racket? (on a low language level) I need something like that: For x=1 (list (list false) (list true)) For x=2 (list (list false false) (list false true) (list true false) (list true true)) For x=3 (list (list false false false) (list false false true) (list false true false) (list false true true) (list true false false) (list true false true) (list true true false) (list true true true)) etc. I have no idea how to do

Shallow and Deep Binding

99封情书 提交于 2019-12-08 01:50:27
问题 I was trying to understand the concept of dynamic/static scope with deep and shallow binding. Below is the code- (define x 0) (define y 0) (define (f z) (display ( + z y)) (define (g f) (let ((y 10)) (f x))) (define (h) (let ((x 100)) (g f))) (h) I understand at dynamic scoping value of the caller function is used by the called function. So using dynamic binding I should get the answer- 110 . Using static scoping I would get the answer 0 . But I got these results without considering shallow

Can I make a macro that expands into more than one value?

霸气de小男生 提交于 2019-12-08 01:19:56
问题 Is there a way to define a racket macro foo so that (list 1 (foo 2 3) 4) expands into (list 1 2 3 4) ? 回答1: As other answers have mentioned, you cannot have a macro expand into more than one value, and have that spliced into the calling context. But you can do something similar using quasiquotation. Assuming your macro is adapted to return a list instead, you can do this (for your given example): `(1 ,@(foo 2 3) 4) Example (tested in Racket): > `(1 ,@(map sqrt '(2 3)) 4) '(1 1

“cond”,“and” and “or” in Scheme

雨燕双飞 提交于 2019-12-08 01:18:24
问题 I'm reading The Little Schemer . And thanks to my broken English, I was confused by this paragraph: (cond ... ) also has the property of not considering all of its arguments. Because of this property, however, neither (and ... ) nor (or ... ) can be defined as functions in terms of (cond ... ), though both (and ... ) and (or ... ) can be expressed as abbreviations of (cond ... )-expressions: (and a b) = (cond (a b) (else #f) and (or a b) = (cond (a #t) (else (b)) If I understand it correctly,

Time Code in PLT-Scheme

房东的猫 提交于 2019-12-07 23:44:14
问题 I want to see how long a function takes to run. What's the easiest way to do this in PLT-Scheme? Ideally I'd want to be able to do something like this: > (define (loopy times) (if (zero? times) 0 (loopy (sub1 times)))) > (loopy 5000000) 0 ;(after about a second) > (timed (loopy 5000000)) Took: 0.93 seconds 0 > It doesn't matter if I'd have to use some other syntax like (timed loopy 5000000) or (timed '(loopy 5000000)) , or if it returns the time taken in a cons or something. 回答1: The standard

Error setting load-noisily? and auto-exiting in MIT-Scheme

帅比萌擦擦* 提交于 2019-12-07 20:28:38
问题 In order to debug MIT-Scheme scripts with Vim, I want to be able to run the script file currently being edited as conveniently as possible. Here is what I'm doing: sicp.scm (set! load-noisily? #t) (define (abs x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)) ) ) (abs 42) (abs -24) (exit) After executing :!mit-scheme --eval "(load \"sicp\")" when editing sicp.scm in Vim, I get: Image saved on Saturday May 17, 2014 at 2:39:25 AM Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR

about the dot “.” in scheme

一曲冷凌霜 提交于 2019-12-07 18:41:50
问题 I saw there are other questions about the dot "." I followed but it didn't work for my code.... it's a part of code, the implementation is not focused to this symbol. but output should be included this dot. when I give input of two lists '(1 2 3) '(4 5) my expected output => (1 . 4) (2 . 5) I managed to get (1 4) (2 5) just need to add "." in the middle. Part of mycode (cons (list (car lst1) (car lst2)) .... for the "." symbol , if I try **trial-1** (cons '(list (car lst1) (car lst2)) ...)

How to translate the following into a tail recursive procedure?

…衆ロ難τιáo~ 提交于 2019-12-07 18:35:31
问题 I have the following mathematical expression: ; f(n) = f(n - 1) + f(n - 2) where n >= 2 ; f(n) = n where n < 2` Which I translated into a normal recursive LISP call: (define (f n) (cond ((< n 2) n) (else (+ (f (- n 1)) (f (- n 2)))))) How would I translate the above into a tail-recursive procedure? I'm not used to functional programming so I'm struggling a bit. 回答1: You are talking about established example of tail recursive transformation for computing Fibonacci numbers. You can find