selcukahmed
2021-1-2 12:54:09

@selcukahmed has joined the channel


anything
2021-1-2 15:19:09

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


anything
2021-1-2 15:36:25

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.


soegaard2
2021-1-2 15:38:18

According to the error message, my-let expects a body-expr , a single expression.


soegaard2
2021-1-2 15:38:42

In (my-let ([var-id #f] ...) (set! var-id rhs-expr) ... body-expr)) there several expressions.


soegaard2
2021-1-2 15:39:50

With a begin we can make it a single expression: (my-let ([var-id #f] ...) (begin (set! var-id rhs-expr) ... body-expr)))


anything
2021-1-2 15:46:09

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.


soegaard2
2021-1-2 15:47:45

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


anything
2021-1-2 15:49:53

Yes, the lambda application protects them.


anything
2021-1-2 15:52:11

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


soegaard2
2021-1-2 15:53:13

The pattern pat ... matches zero or more occurences of pat.


soegaard2
2021-1-2 15:54:08

The pattern pat ...+ matches one or more.


anything
2021-1-2 15:55:01

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


anything
2021-1-3 03:20:34

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?


anything
2021-1-3 03:28:54

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.


rokitna
2021-1-3 05:10:43

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.


rokitna
2021-1-3 05:10:59

@anything ^


amol.jha
2021-1-3 05:38:56

@amol.jha has joined the channel