[Haskell-beginners] Using Functions with Fold
A. Mc.
47dragonfyre at gmail.com
Mon Feb 1 04:49:39 UTC 2021
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.html>
More information about the Beginners
mailing list