{- 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 {- I can't get this to work. -} {- See: Also see: 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 [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
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:find It 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 |
In reply to this post by trent shipley
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 <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
{- 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. _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |