[racket] Lunch choosing program ported from Haskell to Racket

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

On Jul 5, 2014, at 2:25 PM, Alexander D. Knauth <alexander at knauth.org> wrote:

> 
> On Jul 5, 2014, at 12:10 PM, Brian Adkins <racketusers at lojic.com> wrote:
> 
>> 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**.

Also you might want to look at match-define.  

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

I haven’t tested it, but would this work for what you want?:

(define (summed-history history)
  (for/fold ([hsh (hash)]) ([3tuple (in-list history)])
    (match-define (list _ rest users) 3tuple)
    (for/fold ([hsh hsh]) ([user (in-list users)])
      (hash-set hsh
                (list user rest)
                (+ 1 (hash-ref hsh (list user rest) 0))))))

By the way (to the rest of the list) is there anything like a for/match that would allow something like this?:
(for/fold/match ([hsh (hash)]) ([(list _ rest users) (in-list history)])
  ...)

And if there was, would this work?:
(for*/fold/match ([hsh (hash)]) ([(list _ rest users) (in-list history)]
                                 [user (in-list users)])
  (hash-set hsh
            (list user rest)
            (+ 1 (hash-ref hsh (list user rest) 0)))


> 
>> (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
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140705/71fcf8eb/attachment-0001.html>

Posted on the users mailing list.