Re: [racket-users] student produces absolutely bonkers environment lookup code

2021-05-13 Thread 'John Clements' via Racket Users
Yep, I think that’s probably on the money. I guess this is really a classic 
example of the problem with writing code using internet search.

Many thanks!

> On May 7, 2021, at 4:38 PM, Ben Greenman  wrote:
> 
> On 5/7/21, Shu-Hung You  wrote:
>> Not that I have any idea of what's going on, but interestingly, Typed
>> Racket's second ->* example has (1)(3)(4). The use of list* may be
>> possible if one follows the type (List* String Natural x) in the
>> example.
>> 
>> https://docs.racket-lang.org/ts-reference/type-ref.html?#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._-~3e%2A%29%29
>> 
>> Shu-Hung
> 
> +1, that example in the docs looks _very_ similar to the student code
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/CAFUu9R4SJ7OisUeH5mFxGHpycrxhKYNFuSMLLgSzKCprt2aQWQ%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/36e88c8e-d2eb-43b5-8a8d-4144487fe347%40mtasv.net.


Re: [racket-users] student produces absolutely bonkers environment lookup code

2021-05-07 Thread Ben Greenman
On 5/7/21, Shu-Hung You  wrote:
> Not that I have any idea of what's going on, but interestingly, Typed
> Racket's second ->* example has (1)(3)(4). The use of list* may be
> possible if one follows the type (List* String Natural x) in the
> example.
>
> https://docs.racket-lang.org/ts-reference/type-ref.html?#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._-~3e%2A%29%29
>
> Shu-Hung

+1, that example in the docs looks _very_ similar to the student code

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAFUu9R4SJ7OisUeH5mFxGHpycrxhKYNFuSMLLgSzKCprt2aQWQ%40mail.gmail.com.


Re: [racket-users] student produces absolutely bonkers environment lookup code

2021-05-07 Thread Shu-Hung You
Not that I have any idea of what's going on, but interestingly, Typed
Racket's second ->* example has (1)(3)(4). The use of list* may be
possible if one follows the type (List* String Natural x) in the
example.

https://docs.racket-lang.org/ts-reference/type-ref.html?#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._-~3e%2A%29%29

Shu-Hung

On Fri, May 7, 2021 at 10:53 AM 'John Clements' via Racket Users
 wrote:
>
> Background: I teach a PL course, using Shriram’s PLAI. Many of the 
> assignments require students to maintain an environment mapping symbols to 
> values. Shriram illustrates a nice easy way to do this, as a list of 
> two-element structures. You can also use an immutable hash. Fine. So I’m 
> grading a student submission, and I come across this:
>
> (: extend-environment* (-> Environment (Listof (List Symbol Value)) 
> Environment))
> (define (extend-environment* env kv)
>   (let loop ([kv : (Listof (List Symbol Value)) kv]
>  [env env])
> (cond
>   [(empty? kv) env]
>   [else
>(let* ([kv-pair (first kv)]
>   [new-env (KVEnvironment (first kv-pair) (second kv-pair) env)])
>  (loop (rest kv) new-env))])))
>
>
> ;; Returns a new environment with old-env as parent
> (: extend-environment (->* (Environment Symbol Value) #:rest-star (Symbol 
> Value) Environment))
> (define (extend-environment env key value . kv)
>   (define kv* : (Listof (List Symbol Value))
> (let loop : (Listof (List Symbol Value))
>   ([kv : (Rec x (U Null (List* Symbol Value x))) (list* key value kv)])
>   (if (empty? kv)
>   '()
>   (let* ([take-two (list (first kv) (second kv))]
>  [drop-two (rest (rest kv))])
> (list* take-two (loop drop-two))
>   (extend-environment* env kv*))
>
> This solution uses
>
> 1) a named let,
> 2) the list* function,
> 3) a ->* type with a #:rest-star argument, and
> 4) A custom rec type for alternating symbols and values.
>
> To cap it all off, the student DOESN’T EVEN USE the extra hardware. This is 
> the only use of extend-environment in the code:
>
> (extend-environment e name arg)
>
> So, uh, any idea where this code came from?
>
> :)
>
> John
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/ee31119c-aab4-48e4-94dd-39272dd34724%40mtasv.net.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAMTzy%2BZDuZbSW3ga%2BeDdiXhfgJSZpNS2u_nhB%2Bj9u40tYKXJKQ%40mail.gmail.com.


[racket-users] student produces absolutely bonkers environment lookup code

2021-05-07 Thread 'John Clements' via Racket Users
Background: I teach a PL course, using Shriram’s PLAI. Many of the assignments 
require students to maintain an environment mapping symbols to values. Shriram 
illustrates a nice easy way to do this, as a list of two-element structures. 
You can also use an immutable hash. Fine. So I’m grading a student submission, 
and I come across this:

(: extend-environment* (-> Environment (Listof (List Symbol Value)) 
Environment))
(define (extend-environment* env kv)
  (let loop ([kv : (Listof (List Symbol Value)) kv]
 [env env])
(cond
  [(empty? kv) env]
  [else
   (let* ([kv-pair (first kv)]
  [new-env (KVEnvironment (first kv-pair) (second kv-pair) env)])
 (loop (rest kv) new-env))])))


;; Returns a new environment with old-env as parent
(: extend-environment (->* (Environment Symbol Value) #:rest-star (Symbol 
Value) Environment))
(define (extend-environment env key value . kv)
  (define kv* : (Listof (List Symbol Value))
(let loop : (Listof (List Symbol Value))
  ([kv : (Rec x (U Null (List* Symbol Value x))) (list* key value kv)])
  (if (empty? kv)
  '()
  (let* ([take-two (list (first kv) (second kv))]
 [drop-two (rest (rest kv))])
(list* take-two (loop drop-two))
  (extend-environment* env kv*))

This solution uses

1) a named let,
2) the list* function,
3) a ->* type with a #:rest-star argument, and
4) A custom rec type for alternating symbols and values.

To cap it all off, the student DOESN’T EVEN USE the extra hardware. This is the 
only use of extend-environment in the code:

(extend-environment e name arg)

So, uh, any idea where this code came from? 

:)

John 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/ee31119c-aab4-48e4-94dd-39272dd34724%40mtasv.net.