@selcukahmed has joined the channel
I think this code for my-letrec doesn’t work with my Racket 7.9 BC. (The code comes from Ryal Culpepper, see source below, and I’m considering it might have worked before on previous versions or something.) (define-syntax-rule (my-let ([var-id rhs-expr] ...) body-expr)
((lambda (var-id ...) body-expr) rhs-expr ...))
;(my-let ([x 1] [y 2]) (+ x y))
;(let ([x 1] [y 2]) (+ x y))
(define-syntax-rule (my-letrec ([var-id rhs-expr] ...) body-expr)
(my-let ([var-id #f] ...)
(set! var-id rhs-expr) ...
body-expr))
;(my-let ([x 1] [y 2]) (+ x y))
(my-letrec ([x 1] [y 2]) (+ x y))
Executing the this last expression yields ; fact-example-syntax.rkt::5663: my-let: use does not match pattern: (my-let ((var-id rhs-expr) ...) body-expr)
; in: (my-let ((x #f) (y #f)) (set! x 1) (set! y 2) (+ x y))
I figure body-expr
implies a single expression and I have three there. If I replace the call to my-let (inside the definition of my-letrec) to the primitive let, it works.
Source (section 1.8): http://rmculpepper.github.io/malr/basic.html
Exercise 11 asks me to break my-let in two different lambda-syntax-errors. I can’t find a single one. Correct use of the pattern seems to never produce a lambda-syntax-error.
According to the error message, my-let
expects a body-expr
, a single expression.
In (my-let ([var-id #f] ...)
(set! var-id rhs-expr) ...
body-expr))
there several expressions.
With a begin
we can make it a single expression: (my-let ([var-id #f] ...)
(begin
(set! var-id rhs-expr) ...
body-expr)))
This works. I see that variables outside of my-let are unaffected by the set! inside it. Is this due to the begin form? I think not because using the primitive let and removing the begin-form, the behavior is the same — a (define x 18) before (my-letrec ([x 1] [y 2]) (+ x y)) has no effect on (+ x y). So begin here is just grouping expressions. The scope of the variables inside my-let are already protected by let.
> I see that variables outside of my-let are unaffected by the set! inside it. Yes. As you explain, it is due to my-let
.
Yes, the lambda application protects them.
Here’s another problem I can’t solve. I thought that (my-let () 1) should not fit the pattern because the pattern demands at least one var-id rhs-expr pair. But I put none and it works just fine — (my-let () 1)
==> 1
. (define-syntax-rule (my-let ([var-id rhs-expr] ...) body-expr)
((λ (var-id ...) body-expr) rhs-expr ...))
The pattern pat ...
matches zero or more occurences of pat
.
The pattern pat ...+
matches one or more.
Yes, that makes sense. I fell there because of section 1.7 where we define a macro (capture-output e …) and then (capture-output) produces a lambda expression with no body. (I see what’s going on each case now. Thanks!!)
I don’t understand the production of random-sample when :#replacement? is false.
fact-example-syntax.rkt> (take (make-deck) 3)
'((hearts . A) (hearts . 1) (hearts . 2))
fact-example-syntax.rkt> (random-sample (take (make-deck) 3) 3 #:replacement? #f)
'((hearts . A) (hearts . 1) (hearts . 2))
fact-example-syntax.rkt> (random-sample (take (make-deck) 3) 3 #:replacement? #f)
'((hearts . A) (hearts . 1) (hearts . 2))
fact-example-syntax.rkt> (random-sample (take (make-deck) 3) 3 #:replacement? #f)
'((hearts . A) (hearts . 1) (hearts . 2))
It doesn’t seem to randomize if I say replacement is false. If I say true, it comes out as expected. The documentation doesn’t seem to offer much difficulty in interpretation. Where is my use incorrect?
Looking at the source code, I see it uses Knuth’s S algorithm. I might be misunderstanding what the procedure is for. I don’t quite understand what the algorithm is for, but I see that shutffle
is what I need. Problem solved.
That does seem wrong to me too, or at least it seems like an experience you should only have 1/216 of the time. Maybe that’s a bug? Does it keep happening if you run it more than three times? Might be worth filing it as a bug just in case.
@anything ^
@amol.jha has joined the channel