# Hoogle q5.8: write your own findindices

4 messages
Open this post in threaded view
|

## Hoogle q5.8: write your own findindices

 {-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 -- Trentimport qualified Data.Foldable as DFold -- Trentpositions' :: 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.txtI 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.hsThese 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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Hoogle q5.8: write your own findindices

 On 2018-01-15 16:02, trent shipley 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. Sounds like a sensible plan to me! > 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. Do you really need to do it using a list comprehension? As far as I can see, the only requirement is that you use the 'find' function. > 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. Yes, there is a standard 'find' function in the Data.List module. See here:    http://hackage.haskell.org/package/base-4.10.1.0/docs/Data-List.html#v:findIt is part of the 'base' package, i.e. you most certainly already have it. You can import it by using e.g.    -- Bring everything in Data.List into scope    import Data.List    -- Bring just the 'find' function into scope    import Data.List (find) -- Frerich Raabe - [hidden email] www.froglogic.com - Multi-Platform GUI Testing _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners