<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Jul 5, 2014, at 2:25 PM, Alexander D. Knauth <<a href="mailto:alexander@knauth.org">alexander@knauth.org</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><br>On Jul 5, 2014, at 12:10 PM, Brian Adkins <<a href="mailto:racketusers@lojic.com">racketusers@lojic.com</a>> wrote:<br><br><blockquote type="cite">Is match typically used to destructure lists?</blockquote><br>Match can be used for a lot of things, but (looking ahead) you might want to look at one of the match-lambda s:<br><br><a href="http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29">http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29</a><br><br>Probably the one you want is either match-lambda* or maybe match-lambda**.<br></blockquote><div><br></div><div>Also you might want to look at match-define. </div><br><blockquote type="cite"><blockquote type="cite">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.<br></blockquote><br>I think nested folds are discouraged in favor of for loops:<br><a href="http://docs.racket-lang.org/style/Choosing_the_Right_Construct.html?q=match-lambda#%28part._.Traversals%29">http://docs.racket-lang.org/style/Choosing_the_Right_Construct.html?q=match-lambda#%28part._.Traversals%29</a><br><br>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<br>for/fold in typed racket?<br><br>Because for/fold made no sense to me until I tried for/lists and for/fold in typed racket, and types/contracts make it<br>easier for me to wrap my head around what it’s doing (at least for this case). <br></blockquote><div><br></div><div>I haven’t tested it, but would this work for what you want?:</div><div><div><br></div><div><font face="Courier New">(define (summed-history history)</font></div><div><font face="Courier New"> (for/fold ([hsh (hash)]) ([3tuple (in-list history)])</font></div><div><font face="Courier New"> (match-define (list _ rest users) 3tuple)</font></div><div><font face="Courier New"> (for/fold ([hsh hsh]) ([user (in-list users)])</font></div><div><font face="Courier New"> (hash-set hsh</font></div><div><font face="Courier New"> (list user rest)</font></div><div><font face="Courier New"> (+ 1 (hash-ref hsh (list user rest) 0))))))</font></div><div><br></div><div>By the way (to the rest of the list) is there anything like a for/match that would allow something like this?:</div><div><font face="Courier New">(for/fold/match ([hsh (hash)]) ([(list _ rest users) (in-list history)])</font></div><div><font face="Courier New"> ...)</font></div><div><br></div><div>And if there was, would this work?:</div><div><font face="Courier New">(for*/fold/match ([hsh (hash)]) ([(list _ rest users) (in-list history)]</font></div><div><font face="Courier New"> [user (in-list users)])</font></div><div><font face="Courier New"> (hash-set hsh</font></div><div><font face="Courier New"> (list user rest)</font></div><div><font face="Courier New"> (+ 1 (hash-ref hsh (list user rest) 0)))</font></div><div><br></div></div><br><blockquote type="cite"><br><blockquote type="cite">(define (summed-history history)<br> (foldl (λ (3tuple hsh)<br> (define rest (second 3tuple))<br> (define users (third 3tuple))<br> (foldl (λ (user hsh) <br> (hash-set hsh (list user rest) (+ 1 (hash-ref hsh (list user rest) 0))))<br> hsh <br> users))<br> (hash) <br> history))<br><br>vs.<br><br>(define (summed-history history)<br> (foldl (λ (3tuple hsh)<br> (match 3tuple <br> [(list _ rest users)<br> (foldl (λ (user hsh) <br> (hash-set hsh <br> (list user rest) <br> (+ 1 (hash-ref hsh (list user rest) 0))))<br> hsh <br> users)]))<br> (hash) <br> history))<br><br>What I'd really like is to replace the 3tuple arg with a pattern:<br><br>(define (summed-history history)<br> (foldl (λ ((_ rest users) hsh)<br> (foldl (λ (user hsh) <br> (hash-set hsh (list user rest) (+ 1 (hash-ref hsh (list user rest) 0))))<br> hsh <br> users))<br> (hash) <br> history))<br></blockquote><br>I think one of the match-lambda s would be a good thing for that.<br><br><a href="http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29">http://docs.racket-lang.org/reference/match.html?q=match-lambda#%28form._%28%28lib._racket%2Fmatch..rkt%29._match-lambda%29%29</a><br><br>Probably the one you want is either match-lambda* or maybe match-lambda**.<br><br><blockquote type="cite"><br><br>Thanks,<br>Brian<br><br><br>____________________<br> Racket Users list:<br> http://lists.racket-lang.org/users<br></blockquote><br><br>____________________<br> Racket Users list:<br> http://lists.racket-lang.org/users<br></blockquote></div><br></body></html>