scheme

A scheme procedure that returns a list of every other element

主宰稳场 提交于 2019-12-01 22:57:19
I'm having a bit of trouble implementing this program in Scheme, although I think I'm 90% of the way there. Unfortunately I need to be a little vague about it since this is a homework assignment. I want to (A B C D ) to return ( B D) . but i am getting an error that says The object (), passed as an argument to safe-car, is not a pair | " This is my code: (DEFINE (other_el lis) (COND (( NULL? lis ) '()) ((LIST? lis) (append (CADR lis) (other_el (CDR lis)))) (ELSE (show " USAGE: (other_el [LIST])")))) There are a number of minor issues with this code that should be mentioned before I demonstrate

Modification of the basic if expression in Scheme. Why does it go into an infinite loop?

拥有回忆 提交于 2019-12-01 22:09:25
In Scheme, I modified the basic 'if' command as: (define (modified-if predicate then-clause else-clause) (if predicate then-clause else-clause)) And then I defined a simple factorial generating program using the modified version of if: (define (factorial n) (modified-if (= n 0) (* n (factorial (- n 1))))) Now, when I call the above function, it goes into an infinite loop. Why does that happen? Scheme has eager evaluation. This means that, unless you're using a special form (like if ) or a macro (like cond or case ) that delegates to such a special form, all subexpressions are evaluated first.

'(quote quote) in scheme

走远了吗. 提交于 2019-12-01 21:50:58
I'm trying to learn scheme by myself. Could anyone tell me why '(quote quote) will output 'quote , and '(quote 'quote) will output ''quote ? Thank you very much! This expression: '(quote quote) ... after expanding '<something> to (quote <something>) is equivalent to (quote (quote quote)) , notice that the symbol quote is being quoted two times, and this expression is evaluated and printed as ''quote . On the other hand, this expression: '(quote 'quote) ... is equivalent to (quote (quote (quote quote))) , notice that the symbol quote is being quoted three times, and this expression is evaluated

Scheme Continuation: What's the difference between call 'call/cc' in top level and non-top level?

那年仲夏 提交于 2019-12-01 21:50:52
This code works as expected: (define saved #f) (cons 'wo (call/cc (lambda (k) (set! saved k) '()))) (saved 'ca!) output (Racket console): '(wo) '(wo . ca!) But when I wrap it in a function and call it, the program never stops. Why? (define (test) (define saved #f) (cons 'wo (call/cc (lambda (k) (set! saved k) '()))) (saved 'ca!)) (test) A continuation is all that's left to be done in the execution context where it's saved. In the first case, the continuation is saved when calling cons , so it's just to cons 'wo to something and return to the REPL. In the second case, you call procedure test ,

Why do we need `nil`?

家住魔仙堡 提交于 2019-12-01 21:42:10
问题 I do not see why we need nil [1] when to cons a sequence (so-called proper list) of items. It seems to me we can achieve the same goal by using the so-called improper list ( cons -ed pairs without an ending nil ) alone. Since Lisps [2] have already provided a primitive procedure to distinguish between a pair? and an atom (some implementations even provide atom? ), when defining a procedure on a list, e.g., length , I can do the same with just dotted-pairs, as shown below: (define len (lambda

Currying a function n times in Scheme

China☆狼群 提交于 2019-12-01 21:10:39
I'm having trouble figuring out a way to curry a function a specified number of times. That is, I give the function a natural number n and a function fun, and it curries the function n times. For example: (curry n fun) Is the function and a possible application would be: (((((curry 4 +) 1) 2) 3) 4) Which would produce 10. I'm really not sure how to implement it properly. Could someone please give me a hand? Thanks :) You can write your own n-curry procedure by repeatedly calling curry : (define (n-curry n func) (let loop ([i 1] [acc func]) (if (= i n) acc (loop (add1 i) (curry acc))))) If you

Remove multiple characters from a list if they are next to each other in Scheme

前提是你 提交于 2019-12-01 20:52:34
问题 I have to make a Dr. Racket program that removes letters from a list if they are following the same letter as itself. For example: (z z f a b b d d) would become (z f a b d). I have written code for this but all it does is remove the first letter from the list. Can anyone help? #lang racket (define (remove-duplicates x) (cond ((null? x) '()) ((member (car x) (cons(car(cdr x)) '()))) (remove-duplicates (cdr x)) (else (cons (car x) (remove-duplicates (cdr x)))))) (define x '( b c c d d a a))

How do I handle a variable number of arguments passed to a function in Racket?

♀尐吖头ヾ 提交于 2019-12-01 20:16:36
问题 I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a

How do I handle a variable number of arguments passed to a function in Racket?

谁说胖子不能爱 提交于 2019-12-01 20:09:55
I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a simple function to determine difference between map-nodes (works, just not sure why it works): (define

Remove multiple characters from a list if they are next to each other in Scheme

て烟熏妆下的殇ゞ 提交于 2019-12-01 20:01:42
I have to make a Dr. Racket program that removes letters from a list if they are following the same letter as itself. For example: (z z f a b b d d) would become (z f a b d). I have written code for this but all it does is remove the first letter from the list. Can anyone help? #lang racket (define (remove-duplicates x) (cond ((null? x) '()) ((member (car x) (cons(car(cdr x)) '()))) (remove-duplicates (cdr x)) (else (cons (car x) (remove-duplicates (cdr x)))))) (define x '( b c c d d a a)) (remove-duplicates x) (define (remove-dups x) (cond [(empty? x) '()] [(empty? (cdr x)) (list (car x))] [