racket

Getting all possible combinations of x booleans (Racket, Scheme)

耗尽温柔 提交于 2019-12-08 01:52:42
问题 i have a problem. How do I get all possible combinations of x booleans in Racket? (on a low language level) I need something like that: For x=1 (list (list false) (list true)) For x=2 (list (list false false) (list false true) (list true false) (list true true)) For x=3 (list (list false false false) (list false false true) (list false true false) (list false true true) (list true false false) (list true false true) (list true true false) (list true true true)) etc. I have no idea how to do

Can I make a macro that expands into more than one value?

霸气de小男生 提交于 2019-12-08 01:19:56
问题 Is there a way to define a racket macro foo so that (list 1 (foo 2 3) 4) expands into (list 1 2 3 4) ? 回答1: As other answers have mentioned, you cannot have a macro expand into more than one value, and have that spliced into the calling context. But you can do something similar using quasiquotation. Assuming your macro is adapted to return a list instead, you can do this (for your given example): `(1 ,@(foo 2 3) 4) Example (tested in Racket): > `(1 ,@(map sqrt '(2 3)) 4) '(1 1

Time Code in PLT-Scheme

房东的猫 提交于 2019-12-07 23:44:14
问题 I want to see how long a function takes to run. What's the easiest way to do this in PLT-Scheme? Ideally I'd want to be able to do something like this: > (define (loopy times) (if (zero? times) 0 (loopy (sub1 times)))) > (loopy 5000000) 0 ;(after about a second) > (timed (loopy 5000000)) Took: 0.93 seconds 0 > It doesn't matter if I'd have to use some other syntax like (timed loopy 5000000) or (timed '(loopy 5000000)) , or if it returns the time taken in a cons or something. 回答1: The standard

How to install sicp package module in racket?

两盒软妹~` 提交于 2019-12-07 14:04:36
问题 I'm newbie in programming world. I'm using ubuntu OS. I have started my journey with sicp book. I'm working with scheme repl . But suddenly I get stuck with section 2.2.4 I'm not able to execute it's example with scheme repl . I tried to run given example of section, I got an error as given below 1 ]=> (define wave2 (beside wave (flip-vert wave))) ;Unbound variable: wave Even In book, painter is given as primitive procedure. when I ran it, it thrown an error too 1 ]=> painter ;Unbound

scheme/racket: canvas manipulation

心已入冬 提交于 2019-12-07 08:20:32
问题 1) As title says, the objects i draw disappear when i resize the window, but the rectangle stays as is. 2) The origin starts from the top left, but i wish for it to be at the bottom left. 3) I couldn't find any zoom functions, other than in the plot library so if i wish to implement such a thing, one option would to be "zooming" in by drawing bigger objects and refreshing the canvas instead? (define top-frame (new frame% [label "KR"] [width 500] [height 500])) ;Make a frame by instantiating

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

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)))))

for/list annotations in typed/racket

不羁的心 提交于 2019-12-07 06:54:55
问题 I'm trying to add types to some numerical racket code in the hopes of making it faster, but I am stuck dealing with for/list macro expansion in the code below. (: index-member ((Listof Any) (Listof Any) -> (Listof Index))) (define (index-member xs ys) (filter-not negative? (for/list ([(ann i Index) (in-range (ann (length xs) Index))]) (if (member (list-ref xs i) ys) i -1)))) This function returns a list of indexes foreach x which is a member of y. It works in Racket, but I can't seem to get

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

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

喜欢而已 提交于 2019-12-07 05:04:42
问题 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