From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.ada Subject: Re: If not Ada, what else... Date: Wed, 29 Jul 2015 22:17:37 -0700 Organization: A noiseless patient Spider Message-ID: <87oaiumdfy.fsf@jester.gateway.sonic.net> References: <87y4io63jy.fsf@jester.gateway.sonic.net> <7a29d3e9-d1bd-4f4a-b1a6-14d3e1a83a4d@googlegroups.com> <87mvz36fen.fsf@jester.gateway.sonic.net> <2215b44f-8a89-47c6-a4c4-52b74d2dac45@googlegroups.com> <9e492c82-868d-43d3-a18a-38274400e337@googlegroups.com> <40184feb-4053-4ac3-8eaa-c3bd9cd8a77c@googlegroups.com> <10272577-945f-4682-85bc-8ad47f3653ae@googlegroups.com> <87si8i81k2.fsf@atmarama.net> <8076cbd0-2655-4c98-b70e-cb5f0c32e4ba@googlegroups.com> <5e6cb30b-5f8c-4fed-969e-3941315ecba0@googlegroups.com> <87si87nf8k.fsf@jester.gateway.sonic.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="d4217d68945dedf510265c644f2a7daa"; logging-data="20861"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++9UgRq1pZ5TjQPC+1Ovj9" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:4++EStiUj0HU/Q1v0a+AHbLKOQk= sha1:4lEmh+/vzeFZ2QitG1xbiUDINS8= Xref: news.eternal-september.org comp.lang.ada:27163 Date: 2015-07-29T22:17:37-07:00 List-Id: Georg Bauhaus writes: > picksum :: [Int] -> Int -> Int > picksum [] m = 0 > picksum xs m > | m <= 0 = 0 > | m > 0 = mth xs > where mth l@(y:ys) = y + mth (drop m l) > mth [] = 0 > > Then, try this with the justly famed easy list notation, passing > [1 .. 12345678], or larger. (I have GHC 7.8.3, maybe that's too old?) > Of course, the language war soldier of Haskell will argue that the > enemy writer of the above algorithm was stupid, Nah, there's a trick that's not obvious to beginners but it's one of the first things you learn in functional programming, and it's not a big deal once you know it. Basically the recursive call to "mth" has to return a value that gets added to y, which means the stack frame can't get released til the addition has happened. You instead want to write it tail recursively with an accumulator, something like: > ... | m > 0 = mth xs 0 > where mth l@(y:ys) !a = mth (drop m l) (y+a) > mth [] a = a Note the !a in the pattern-- that's a strictness marker to stop unevaluated sums from piling up on the stack. Again it takes some getting used to but you learn to spot these things. I might have written the function like this: p2 :: [Int] -> Int -> Int p2 xs m = sum . map head . takeWhile (not . null) . iterate (drop m) $ xs but the sum might have to be replaced with "foldl1' (+)" for strictness.