scheme

Is there a valid usecase for redefining “define” in scheme/racket?

依然范特西╮ 提交于 2019-12-07 07:47:59
问题 I'm playing around with racket/scheme and it allows me to redefine for instance define and bind it as a value. > (define define 2) > define 2 In that scope I can no longer define anything using define since it is obviously bound to 2. This works for all "keywords" I tried it with ( if , cond etc.). However it is not possible to use define to specify my own definition function: > (define mydef define) stdin::14: define: not allowed in an expression context in: define === context === /usr/share

How to set default or optional parameters in scheme?

微笑、不失礼 提交于 2019-12-07 07:32:47
问题 I'm trying to figure out how to how to set default or optional parameters in Scheme. I've tried (define (func a #!optional b) (+ a b)) but I can't find of a way to check if b is a default parameter, because simply calling (func 1 2) will give the error: Error: +: number required, but got #("halt") [func, +] I've also tried (define (func a [b 0]) (+ a b)) but I get the following error: Error: execute: unbound symbol: "b" [func] If it helps I'm using BiwaScheme as used in repl.it 回答1: This

Understanding Scheme function

不羁的心 提交于 2019-12-07 07:20:57
问题 The following question is given in our Programming language practice exam and I am having a hard time understating how this works. Could someone tell me what the flow of code is? I have run it in racket and know what the answer is. It looks like the first lambda function is taking the other two functions as argument. But then where are the inputs (lambda (x) 2) and (lambda (y) 3) passed to? (((lambda (x y) (x y)) (lambda (y) (lambda (y x) (x (x y)))) (lambda (x) (lambda (x y) (x (y x)))))

Scheme Macro for nesting expressions

非 Y 不嫁゛ 提交于 2019-12-07 07:20:35
问题 Can a macro be written in Scheme (with define-syntax , for example) which will take expressions like this: (op a b c d e f g h i j) And yield expressions like this as output? (op (op (op (op (op (op (op (op (op a b) c) d) e) f) g) h) i) j) Of course, for arbitrary lengths. I can't think of a way to do it, given some template like this: (define-syntax op (syntax-rules () [(_) 'base-case] [(v1 v2 ...) 'nested-case??])) 回答1: (define bop list) (define-syntax op (syntax-rules () ((op a b) (bop a b

Does Scheme/Racket have an enumeration operation?

天大地大妈咪最大 提交于 2019-12-07 05:38:53
问题 Does Scheme/Racket have an enumeration notation equivalent to the [a..b] notation in Haskell? In Haskell, [1..5] evaluates to a list [1,2,3,4,5]. 回答1: (for/list ([i (in-range 1 6)]) i) (sequence->list (in-range 1 6)) (require srfi/1) (iota 5 1) 回答2: (for/list ([i 5]) (+ 1 i)) (build-list 5 add1) Also, (in-range 1 6) (which is a sequence) by itself is useful in many contexts. 来源: https://stackoverflow.com/questions/7144248/does-scheme-racket-have-an-enumeration-operation

Continuation-Passing Style in Scheme?

梦想与她 提交于 2019-12-07 05:34:09
问题 I ran into this code on Wikipedia: (define (pyth x y k) (* x x (lambda (x2) (* y y (lambda (y2) (+ x2 y2 (lambda (x2py2) (sqrt x2py2 k)))))))) The article says that that code is the Continuation-Passing version of another piece of code: (define (pyth x y) (sqrt (+ (* x x) (* y y)))) However, I'm quite confused: How does that even work? How do you multiply a number by a lambda here? (* x x (lambda ...)) 回答1: In the Wikipedia example, * doesn't mean the same thing as * in the conventional

Letrec and reentrant continuations

╄→гoц情女王★ 提交于 2019-12-07 05:31:59
问题 I have been told that the following expression is intended to evaluate to 0, but that many implementations of Scheme evaluate it as 1: (let ((cont #f)) (letrec ((x (call-with-current-continuation (lambda (c) (set! cont c) 0))) (y (call-with-current-continuation (lambda (c) (set! cont c) 0)))) (if cont (let ((c cont)) (set! cont #f) (set! x 1) (set! y 1) (c 0)) (+ x y)))) I must admit that I cannot tell where even to start with this. I understand the basics of continuations and call/cc , but

How can I tell if a list has a third item?

心不动则不痛 提交于 2019-12-07 05:17:21
问题 I have a function that takes a list that either has two or three elements. ;; expecting either ((a b c) d) or ((a b c) d e) (define (has-third-item ls) (if (null? (caddr ls)) false true) ) But this code fails with mcar: expects argument of type <mutable-pair>; given () on the (null? (caddr ls)) expression. I also tried (eq? '() (caddr ls)) but it didn't work either. How do I tell if there's a third item or not? 回答1: You don't want caddr, you want (if (null? (cddr ls)) ... Or just use length

Cartesian product in Scheme

独自空忆成欢 提交于 2019-12-07 04:50:24
问题 I've been trying to do a function that returns the Cartesian Product of n sets,in Dr Scheme,the sets are given as a list of lists,I've been stuck at this all day,I would like a few guidelines as where to start. ----LATER EDIT ----- Here is the solution I came up with,I'm sure that it's not by far the most efficent or neat but I'm only studing Scheme for 3 weeks so be easy on me. 回答1: Here's a concise implementation that is also designed to minimize the size of the resulting structure in

Different kinds of continuations in Racket

﹥>﹥吖頭↗ 提交于 2019-12-07 04:45:06
问题 Can someone give a relatively simple example of the differences in Racket between call-with-composable-continuation and call-with-current-continuation . I've worked through the examples in the Racket Guide 10.3 of call-with-composable-continuation , and the examples of call-with-current-continuation in The Scheme Programming language section 3.3 but I'm not clear on the difference. Could someone give an example where they would give different results in the same context. 回答1: A very thorough