[Haskell-beginners] Hoogle q5.8: write your own findindices

trent shipley trent.shipley at gmail.com
Thu Jan 18 07:58:08 UTC 2018


{-
8. Redefine the function positions using the function find.
-}

positions :: Eq a => a -> [a] -> [Int]
positions x xs = [i | (x', i) <- zip xs [0..], x == x']

{-
Hutton, Graham. Programming in Haskell (Kindle Locations 1640-1642).
Cambridge University Press. Kindle Edition.
-}

find :: Eq a => a -> [(a,b)] -> [b]
find k t = [v | (k',v) <- t, k == k']

{-
Hutton, Graham. Programming in Haskell (p. 49). Cambridge University Press.
Kindle Edition.
-}

positions' :: Eq a => a -> [a] -> [Int]
positions' x xs = find x (zip xs [0..])
-------------------------------------------------------------------
Having read the chapter a week ago, I had forgotten that Hutton had
previously defined a function "find" for tuples (see above). After trying a
search of the Kindle book the solution was trivial.

Trent.


>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180118/e2b31403/attachment.html>


More information about the Beginners mailing list