[racket] Lunch choosing program ported from Haskell to Racket

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sat Jul 5 14:25:52 EDT 2014

On Jul 5, 2014, at 12:10 PM, Brian Adkins <racketusers at lojic.com> wrote:

> I've modified the code with Matthias' feedback and a brief reading of the style guide. I must have been confused on the use of "local". I got the impression that it was required if you wanted to (define (foo arg ...)) within another function, but I took it out, and everything works fine. Maybe local is only required for circumstances that I haven't hit yet. The rank-restaurants functions is nicer looking w/o the local.

I’m pretty sure that local is never required for that in any circumstances.  

See http://lists.racket-lang.org/users/archive/2013-November/060352.html

> Is match typically used to destructure lists?

Match can be used for a lot of things, but (looking ahead) you might want to look at one of the match-lambda s:

http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29

Probably the one you want is either match-lambda* or maybe match-lambda**.

> I used it a few times in the program, but in the following example, a couple defines w/ second & third seemed better given the complexity of the nested folds.

I think nested folds are discouraged in favor of for loops:
http://docs.racket-lang.org/style/Choosing_the_Right_Construct.html?q=match-lambda#%28part._.Traversals%29

By the way (to the rest of the list) is there something like a for/fold/contract that allows contracts in similar places as for
for/fold in typed racket?

Because for/fold made no sense to me until I tried for/lists and for/fold in typed racket, and types/contracts make it
easier for me to wrap my head around what it’s doing (at least for this case).  

> (define (summed-history history)
>  (foldl (λ (3tuple hsh)
>           (define rest (second 3tuple))
>           (define users (third 3tuple))
>           (foldl (λ (user hsh) 
>                    (hash-set hsh (list user rest) (+ 1 (hash-ref hsh (list user rest) 0))))
>                  hsh 
>                  users))
>         (hash) 
>         history))
> 
> vs.
> 
> (define (summed-history history)
>  (foldl (λ (3tuple hsh)
>           (match 3tuple 
>             [(list _ rest users)
>              (foldl (λ (user hsh) 
>                       (hash-set hsh 
>                                 (list user rest) 
>                                 (+ 1 (hash-ref hsh (list user rest) 0))))
>                     hsh 
>                     users)]))
>         (hash) 
>         history))
> 
> What I'd really like is to replace the 3tuple arg with a pattern:
> 
> (define (summed-history history)
>  (foldl (λ ((_ rest users) hsh)
>           (foldl (λ (user hsh) 
>                    (hash-set hsh (list user rest) (+ 1 (hash-ref hsh (list user rest) 0))))
>                  hsh 
>                  users))
>         (hash) 
>         history))

I think one of the match-lambda s would be a good thing for that.

http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29

Probably the one you want is either match-lambda* or maybe match-lambda**.

> 
> 
> Thanks,
> Brian
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.