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

trent shipley trent.shipley at gmail.com
Mon Jan 15 15:02:35 UTC 2018


{-
My "intuitive" way of doing this is to use recursion. Take the list, Find
the first list element. Send the rest of the list back to the function to
keep building the list of positions. Until you are out of list, then you
halt.

I have zero intuition on how to do that on a list comprehension, and the
tuple pairs you get from zip just makes it harder.

I don't really want a solution. What I really want to know is whether there
is a "find" function I can import, how to import it, and some relevant
documentation.

I would like to add that I am working my way through Hutton, 2nd ed.
without benefit of a class or instructor.

On that note, can anybody recommend, preferably by personal experience, an
Intro to Functional Programming MOOC taught in Haskell. No, I haven't
Googled or searched the archives.
-}

---------------------------------------

{-
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.
-}

-- OOO! look, SO simple!! Surely I could have done that! (cough.)

---------------------------------------

--import qualified Data.List as DList -- Trent
import qualified Data.Foldable as DFold -- Trent

positions' :: Eq a => a -> [a] -> [Int]
positions' x xs = DFold.find x (zip xs [0..n])
    where n = (length xs) - 1

{-
https://github.com/arcomber/haskell/blob/master/exercises5.txt

I can't get this to work.
-}

{-
See:
https://searchcode.com/codesearch/view/71122841/

Also see:
https://github.com/macalimlim/programming-in-haskell/blob/master/Chapter5Ex.hs

These look like they should work, but the author is not using a common,
predefined Haskell function, which is what I take as Hutton's intent. That
is, students should use "the" existing find function. Hutton is usually
pretty clear when he wants the student to write or rewrite an available
function by the same name.
-}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180115/93b3a592/attachment.html>


More information about the Beginners mailing list