Re: [racket-users] Making change in Racket / lazy language question
Indeed it does. Thanks! On Sat, Jun 17, 2017 at 7:53 AM, Jon Zeppieriwrote: > On Fri, Jun 16, 2017 at 5:41 PM, Daniel Prager > wrote: > > > > But I get a perplexing error if I try to use foldl for added elegance > when > > defining twos: > > > > (define (cc x . xs) (foldl sadd (fcoin x) xs)) > > (define twos (cc 1 2 5 10 20 50 100 200)) > > > > take: expects type as 1st argument, given: > '(1 > > . #); other arguments were: 2 > > > > What's going on? > > The procedure that you use as `foldl`'s first argument expects the > accumulator last, not first, so you'd just need to swap the order of > `sadd`'s arguments. > > if you define `sadd` as: > >(define (sadd n xs) (append (take n xs) (map + (list-tail xs n) > (sadd n xs > > Then it should work. > > - Jon -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Making change in Racket / lazy language question
On Fri, Jun 16, 2017 at 5:41 PM, Daniel Pragerwrote: > > But I get a perplexing error if I try to use foldl for added elegance when > defining twos: > > (define (cc x . xs) (foldl sadd (fcoin x) xs)) > (define twos (cc 1 2 5 10 20 50 100 200)) > > take: expects type as 1st argument, given: '(1 > . #); other arguments were: 2 > > What's going on? The procedure that you use as `foldl`'s first argument expects the accumulator last, not first, so you'd just need to swap the order of `sadd`'s arguments. if you define `sadd` as: (define (sadd n xs) (append (take n xs) (map + (list-tail xs n) (sadd n xs Then it should work. - Jon -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Making change in Racket / lazy language question
The classic making change problem cropped up in discussion on the Racket reddit with solutions proffered in Haskell and Clojure. See: https://www.reddit.com/r/Racket/comments/6gumem/what_is_a_string_p/dixlvpr/ I was able to contribute a Lazy Racket translation: #lang lazy (define zeros (cons 0 zeros)) (define (fcoin n) (append (take n (cons 1 zeros)) (fcoin n))) (define (sadd xs n) (append (take n xs) (map + (list-tail xs n) (sadd xs n (define twos (sadd (sadd (sadd (sadd (sadd (sadd (sadd (fcoin 1) 2) 5) 10) 20) 50) 100) 200)) (! (list-ref twos 200)) But I get a perplexing error if I try to use foldl for added elegance when defining twos: (define (cc x . xs) (foldl sadd (fcoin x) xs)) (define twos (cc 1 2 5 10 20 50 100 200)) take: expects type as 1st argument, given: '(1 . #); other arguments were: 2 What's going on? Also, I failed to get a solution in regular Racket using streams, wherein my attempt hanged. Dan -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.