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

David McBride toad3k at gmail.com
Mon Jan 15 15:31:55 UTC 2018


There is no standard find function that returns a list of found elements.
There is a lookup function but that also returns Maybe because it assumes
that keys are unique. That is why the author of that script made his own
find, which is what you should do.

It would be something like find a = map snd . filter (\(x,y) -> a == x)

On Mon, Jan 15, 2018 at 10:02 AM, trent shipley <trent.shipley at gmail.com>
wrote:

> {-
> 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.
> -}
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180115/dcf3794b/attachment.html>


More information about the Beginners mailing list