racket

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

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

Capturing a variable number of arguments via an ellipsis in a nested macro; Missing pattern variable error

[亡魂溺海] 提交于 2019-12-05 10:13:06
Consider a scenario of two macros: the outer-macro defines a general structure of some entity, and the inner-macro expands in the scope of the outer macro. My intent is captured in the following code, where the expected output is a print statement. This example throws the following error for the pattern of the inner macro: (_ value ...) . syntax: no pattern variables before ellipsis in template in: ... I intend to use value ... in the same way as the body ... pattern of the outer macro. In fact, a list of the 'values' is exactly what I need (not necessarily a very flexible 'ellipsis pattern').

Cartesian product in Scheme

纵然是瞬间 提交于 2019-12-05 09:08:46
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. Here's a concise implementation that is also designed to minimize the size of the resulting structure in memory, by sharing the tails of the component lists. It uses SRFI-1. (define (cartesian-product . lists) (fold

Does Scheme/Racket have an enumeration operation?

痴心易碎 提交于 2019-12-05 08:50:03
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]. (for/list ([i (in-range 1 6)]) i) (sequence->list (in-range 1 6)) (require srfi/1) (iota 5 1) (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

Racket: Identifying tail recursion?

浪尽此生 提交于 2019-12-05 08:32:39
I wrote two different functions in racket to determine whether a list of numbers is ascending: (define (ascending list) (if (<= (length list) 1) #t (and (< (car list) (car (cdr list))) (ascending (cdr list))))) (define (ascending-tail list) (ascending-tail-helper #t list)) (define (ascending-tail-helper prevBool rest) (if (<= (length rest) 1) prevBool (ascending-tail-helper (and prevBool (< (car rest) (car (cdr rest)))) (cdr rest)))) I had the hardest time determining whether or not the first ascending was tail recursive, so I rewrote it using what I believe to be tail recursion. The reason

Multidimensional vectors in Scheme?

一世执手 提交于 2019-12-05 07:19:22
I earlier asked a question about arrays in scheme (turns out they're called vectors but are basically otherwise the same as you'd expect). Is there an easy way to do multidimensional arrays vectors in PLT Scheme though? For my purposes I'd like to have a procedure called make-multid-vector or something. By the way if this doesn't already exist, I don't need a full code example of how to implement it. If I have to roll this myself I'd appreciate some general direction though. The way I'd probably do it is to just iterate through each element of the currently highest dimension of the vector to

Variable in a function

白昼怎懂夜的黑 提交于 2019-12-05 06:29:40
I have see the following code... The first call of (next-num) returns 1 , and the second returns 2 . (define next-num (let ((num 0)) (lambda () (set! num (+ num 1)) num))) (next-num) ; 1 (next-num) ; 2 What I can not understand is... num is created by let inside next-num , it is kind of a local variable... How does scheme know that each time next-num is called, the value of num is not erased by let ((num 0)) ; How does scheme know that it is always the same num that we modify whenever next-num is called? It seems that num is both local and static... How can we define a local variable, but not