scheme

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

和自甴很熟 提交于 2019-12-05 12:55:17
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/racket/collects/racket/private/norm-define.rkt:8:4: normalize-definition /usr/share/racket/collects

Understanding Scheme function

陌路散爱 提交于 2019-12-05 11:52:53
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))))) (lambda (x) 2) (lambda (y) 3)) The answer to the question is 3. We humans like to name things. Succinct

Is it possible to “extend” a function / lambda / macro in Scheme?

风格不统一 提交于 2019-12-05 11:34:11
For example: if I want the function equal? recognize my own type or record, can I add a new behavior of equal? ? without erasing or overwriting the old one? Or for example if I want to make the function "+" accept also string? Rather than using import , a better solution is to keep track of the original function by let -binding it. It's also better to check that the type of the argument is a string, rather than that it is not a number. Using both of these approaches means that it's possible to compose the technique. (define + (let ((old+ +)) (lambda args (if (string? (car args)) (apply string

Different kinds of continuations in Racket

╄→尐↘猪︶ㄣ 提交于 2019-12-05 11:17:31
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. A very thorough explanation is found in the paper "Adding Delimited and Composable Control to a Production Programming

Using trace to display a procedure in racket

人盡茶涼 提交于 2019-12-05 10:54:35
I've been working through the last few exercises ch 1 of SICP, where several of the exercises uses higher-order functions. Currently I'm trying to debug a problem in my solution to 1.45, which is raising an arity mismatch. The function which is raising the error is the result of twice applying an averaging operation to a fixed-point function solver. It would make my debugging efforts a lot easier if I could just dump some sort of representation of procedures, given that the procedure has been run through several other procedures that alter it before it raises an error. I've looked at the

Scheme Macro for nesting expressions

余生颓废 提交于 2019-12-05 10:38:27
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??])) (define bop list) (define-syntax op (syntax-rules () ((op a b) (bop a b)) ((op a b c ...) (op (bop a b) c ...)))) For example, (op 1 2 3 4) expands to (bop (bop (bop 1 2) 3) 4)

How to set default or optional parameters in scheme?

*爱你&永不变心* 提交于 2019-12-05 10:31:57
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 This works fine in Racket: (define (func a (b 0)) ; same as [b 0] (+ a b)) For example: (func 4) => 4 (func 3 2) => 5

Y Combinator学习总结

倖福魔咒の 提交于 2019-12-05 10:24:57
最近看完The little schemer,第一次看到这种编排的书,一问一答的形式,不知不觉就翻完了整本。很自然的教会大家写递归,怎样写程序,一点点的CPS,还有Y combinator。 个人觉得第九章推导Y combinator过程是我看到的最清晰,简单的一个了,只要花点耐心,就能学会。 scheme版本Y组合子 (define Y (lambda (le) ((lambda (f) (f f)) (lambda (f) (le (lambda (x) ((f f) x))))))) 直接翻译为erlang代码 Y = fun(M) -> (fun(F) -> F(F) end)( fun(F) -> M(fun(X) -> (F(F))(X) end) end ) end. 这样看起来不怎么清晰,抽取函数体,定义为变量,再带入 fun(F) -> F(F) end 即获得 Y = fun(M) -> G = fun (F) -> M(fun(X) -> (F(F))(X) end) end, G(G) end. Y组合子是用于lambda演算中实现递归逻辑的,即是可以实现匿名函数的递归调用。原理就是fixed-point combinator,不动点组合子。高阶函数 f 的不动点是另一个函数 g ,使得 f(g) = g 。那么不动点算子是任何函数 fix 使得对于任何函数

How to start REPL for slimv with MIT-Scheme

荒凉一梦 提交于 2019-12-05 09:59:07
问题 My operating system is Debian Squeeze. Here's the vim version: VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Jul 12 2010 02:29:33) I read a tutorial on http://kovisoft.bitbucket.org/tutorial.html and tried to start REPL for MIT-Scheme. Unfortunately, I failed to start. When I pressed ",c", it started a terminal window loading mit-scheme. Nothing showed in the REPL buffer of vim. Some errors showed in the terminal: Listening on port: 4005 ;netcat: "4005: inverse host lookup failed: Unknown host"

Scheme: Remove duplicated numbers from list

半世苍凉 提交于 2019-12-05 09:37:56
I wrote this code to create a list from en number of arguments given (define (create-list . e) e) But I need it to remove any duplicated numbers from the list within this block itself. I have tried and searched for hours and can't find a solution without placing dozens of lines of code on other blocks. For example let's say my input is (create-list . 2 2 3 5 5 ) I need the list created to be '(2 3 5) and not '(2 2 3 5 5 )... The order of the numbers doesn't matter. Basically, you need to do something like: (define (create-list . e) (dedupe e)) I can think of a really simple but probably