Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Using Functions with Fold (A. Mc.) 2. Re: Using Functions with Fold (Francesco Ariis) ---------------------------------------------------------------------- Message: 1 Date: Sun, 31 Jan 2021 20:49:39 -0800 From: "A. Mc." <47dragonf...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Using Functions with Fold Message-ID: <CAOsti3=yXK96dTfm2c1LWhce8oX+9Dk=245uuezo0spdvda...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello, I feel like I need to go back to basics, so I've made a toy problem that takes a list of Ints, say [10, 20, 30, 40, 50, 60] and, in theory, iterates through the list in successive pairs i.e. [10, 20], [20, 30], [30, 40], [40, 50], [50, 60] and looks for a specific pair set, say 30, 40, and accumulates the number of times that pair appears in the list (for example purpose, there are no repeats, but assuming there were....). What I've done is the following: ---take starting at position 2, in increments of 2 with overlap allowed takeAtTwo :: [Int] -> Int -> [Int] takeAtTwo list position = take (2 + position) list ---expects range of 0 to length list - 2 grabTwo :: [Int] -> Int -> [Int] grabTwo list position = drop position (takeAtTwo list position) -- and this does NOT work, half-pseudocode --- pass in a pair of Int, say [30, 40] and sum up the number of appearances numPair list = foldr (\pair acc -> if grabTwo list iterator == pair then acc + 1 else acc) 0 However, I am unsure how to get this to work properly with fold. Suggestions? Thanks in advance and thank you for your time. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210131/89cb4e6d/attachment-0001.html> ------------------------------ Message: 2 Date: Mon, 1 Feb 2021 06:07:32 +0100 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Using Functions with Fold Message-ID: <20210201050732.GA21439@extensa> Content-Type: text/plain; charset=utf-8 Il 31 gennaio 2021 alle 20:49 A. Mc. ha scritto: > I feel like I need to go back to basics, so I've made a toy problem that > takes a list of Ints, say [10, 20, 30, 40, 50, 60] and, in theory, iterates > through the list in successive pairs i.e. [10, 20], [20, 30], [30, 40], > [40, 50], [50, 60] and looks for a specific pair set, say 30, 40, and > accumulates the number of times that pair appears in the list (for example > purpose, there are no repeats, but assuming there were....). What I've > done is the following: > > ---take starting at position 2, in increments of 2 with overlap allowed > takeAtTwo :: [Int] -> Int -> [Int] > takeAtTwo list position = take (2 + position) list > > ---expects range of 0 to length list - 2 > grabTwo :: [Int] -> Int -> [Int] > grabTwo list position = drop position (takeAtTwo list position) > > -- and this does NOT work, half-pseudocode > --- pass in a pair of Int, say [30, 40] and sum up the number of appearances > numPair list = foldr (\pair acc -> if grabTwo list iterator == pair then > acc + 1 else acc) 0 > > However, I am unsure how to get this to work properly with fold. > Suggestions? Thanks in advance and thank you for your time. You might have better luck with first generating the pairs and then passing them to another function. λ> prova as = zip as (tail as) λ> prova [10,20..60] [(10,20),(20,30),(30,40),(40,50),(50,60)] Counting now is a simple matter of λ> length . filter (==(30,40)) $ it 1 -- or use a fold if you prefer ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 151, Issue 1 *****************************************