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
<[email protected]> 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 [email protected].
> 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAMTzy%2BZDuZbSW3ga%2BeDdiXhfgJSZpNS2u_nhB%2Bj9u40tYKXJKQ%40mail.gmail.com.

Reply via email to