student produces absolutely bonkers environment lookup code

88 views
Skip to first unread message

John Clements

unread,
May 7, 2021, 11:53:21 AM5/7/21
to 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

Shu-Hung You

unread,
May 7, 2021, 12:26:26 PM5/7/21
to John Clements, Racket Users
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
> --
> 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...@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.

Ben Greenman

unread,
May 7, 2021, 7:38:52 PM5/7/21
to Racket Users
On 5/7/21, Shu-Hung You <shh...@u.northwestern.edu> 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

John Clements

unread,
May 13, 2021, 8:24:55 PM5/13/21
to Ben Greenman, 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!
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAFUu9R4SJ7OisUeH5mFxGHpycrxhKYNFuSMLLgSzKCprt2aQWQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages