scheme

Error during expansion of macro in Chicken Scheme

不想你离开。 提交于 2019-12-25 01:48:41
问题 I'm learning how the macro system in Scheme works and I'm trying to make my code look more JavaScript-y. So I thought I would start with the function macro. This is how I want a function definition to look: (function id (x) x) It should expand to the following: (define (id x) x) So I write a macro as follows: (define-syntax function (lambda (name args . body) `(define (,name ,@args) ,@body))) However when I use it I get the following error (in Chicken Scheme): Error: during expansion of

Clarification on callCC

风格不统一 提交于 2019-12-25 01:14:11
问题 My background is Javascript, Python & a bit of Haskell. Hi I am new to Scheme (1 day old). I want to understand the difference between below 2 code snippets. (define onePlus (lambda (v) (+ 1 v))) (onePlus 4) ; 5 With CallCC (define anotherOnePlus 0) (+ 1 (call/cc (lambda (k) (set! anotherOnePlus k) (k 4)))) ; 5 (anotherOnePlus 4); 5 Why anyone want to do the 2nd way to get hold of the function you live in. What am I missing in a bigger picture? Is there any limitations of Scope for getting

tree-fold defintion that works both with + and append

亡梦爱人 提交于 2019-12-25 01:09:22
问题 (define (tree-fold f tree) (if (pair? tree) (apply f (car tree) (map (lambda (t) (tree-fold f t)) (cdr tree))) (f tree))) works for example with: (tree-fold + '(1 (2 2)(2 2)) -> 9 However if I want to use (tree-fold append '(1 (2 2)(2 2))) , I have to modify the tree-fold with list around (car tree) , which breaks it for + . Is there some mechanism that can be used in the tree-fold definition that would make it work with both + and append ? 回答1: This should work, adding one parameter to

Evaluation in Scheme

吃可爱长大的小学妹 提交于 2019-12-25 01:07:30
问题 ok, i have this problem here. i was asked to write a function in Scheme, that takes an "environment", and an expression and it returns the value of this expression, for the variable bindings found in the enviroment. and a definition of a boolean expression is this according to the question below. edit sorry my question is what does it mean by "it takes an environment" as an argument and what exactly does the function need to do? evaluate for example "T OR F" and return "F" ??? "<expr> ::=

How do I combine two foldr/ foldl functions for this output? (Racket/Scheme)

99封情书 提交于 2019-12-24 19:24:25
问题 I am quite new to racket, and I wondered if there is a way to combine two foldr` functions: '(foldr + 0 (list 1 2 3 4)) ;; output = 10 -> (4+(3+(2+(1+0)))) (foldr * 1 (list 1 2 3 4)) ;; output = 24 -> (4*(3*(2*(1*0))))' I want to receive this output : output = 64 -> (4+4∗(3+3∗(2+2∗(1+1∗0)))) 回答1: #| (4*(3*(2*(1*0)))) -> 0 (not 24) (4+(3+(2+(1+0)))) -> 10 (foldr + 0 (list 1 2 3 4)) -> (+ 1 (+ 2 (+ 3 (+ 4 0)))) -> 10 (foldr * 1 (list 1 2 3 4)) -> (* 1 (* 2 (* 3 (* 4 1)))) -> 24 if you want this

Scheme/Racket: most idiomatic way to append single element to end of list

浪尽此生 提交于 2019-12-24 16:22:23
问题 I want to append the element b to the list a (let's say (a1, a2, ... an) ), e.g. appending the number 3 to (1 2) gives (1 2 3) So far I've been doing (append a (list b)) , which is kind of long and inelegant, so I wonder if there's a "better" way... 回答1: Are you building a list piecemeal, an item at a time? If so, the idiomatic way to do this is to build the list backward, using cons , and then reversing the final result: (define (map-using-cons-and-reverse f lst) (let loop ((result '())

Scheme Function (DrRacket)

喜夏-厌秋 提交于 2019-12-24 15:53:31
问题 So, i'm trying to write the following function in scheme, and to be able to run it on DrRacket. The problem is as follows, make5 - takes two integers, and returns a 5-digit integer constructed of the rightmost 3 digits of the first input, and the leftmost 2 digits of the second input. For example, (make5 561432 254) would return 43225. Negative signs on either input number should be ignored - that is, (make5 561432 -254) would also return 43225. If the first number has less than three digits

Getting the effects of “set!” without using it

纵饮孤独 提交于 2019-12-24 15:50:18
问题 I have railroad1 and station1 defined, and I want to update the value of railroad1 without using set! or another define. For example: (define railroad1 (list 1991)) (define station1 (list "station")) (define (add-station railroad station) (append railroad station) ) When I call (add-station railroad1 station1) I get (1991 "station") Now I could do this: (define railroad1 (add-station railroad1 station1)) So that railroad1 is now (1991 "station") instead of just (1991). However, my end goal is

Need #t but get first element (Scheme)

≯℡__Kan透↙ 提交于 2019-12-24 14:33:58
问题 I think I have almost done solution with previous problem: Foldr in scheme but in code is a small trouble. I need #t but I get first element, false is OK. Here is my code: (define accum (lambda (list1 pre?) (foldr (lambda (x y) (if y (if (or (equal? y #t) (pre? x y)) x #f) #f)) #t list1))) (accum '(1 2 3 4) <=) --> 1 (should be #t) (accum '(2 2 4 4) <=) --> 2 (should be #t) (accum '(1 2 5 4) <=) --> #f (accum '(5 7 2 3) <=) --> #f If I write "x --> #t", I always get #t, even if is #f. 回答1:

Sorting in scheme following a pattern

混江龙づ霸主 提交于 2019-12-24 14:26:57
问题 A little help, guys. How do you sort a list according to a certain pattern An example would be sorting a list of R,W,B where R comes first then W then B. Something like (sortf '(W R W B R W B B)) to (R R W W W B B B) Any answer is greatly appreciated. 回答1: This is a functional version of the Dutch national flag problem. Here are my two cents - using the sort procedure with O(n log n) complexity: (define sortf (let ((map '#hash((R . 0) (W . 1) (B . 2)))) (lambda (lst) (sort lst (lambda (x y) (