Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Hoogle q5.8: write your own findindices (trent shipley) 2. Re: Hoogle q5.8: write your own findindices (Frerich Raabe) 3. Re: Hoogle q5.8: write your own findindices (David McBride) ---------------------------------------------------------------------- Message: 1 Date: Mon, 15 Jan 2018 15:02:35 +0000 From: trent shipley <trent.ship...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: [Haskell-beginners] Hoogle q5.8: write your own findindices Message-ID: <CAEFLybL5=j91hs80rpjr+i8ac+_xfjv+fyn3xkb0fwtpuad...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" {- 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-0001.html> ------------------------------ Message: 2 Date: Mon, 15 Jan 2018 16:25:58 +0100 From: Frerich Raabe <ra...@froglogic.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Hoogle q5.8: write your own findindices Message-ID: <a92c251164c1510bf93ad5bfe16bb...@froglogic.com> Content-Type: text/plain; charset=US-ASCII; format=flowed 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 - ra...@froglogic.com www.froglogic.com - Multi-Platform GUI Testing ------------------------------ Message: 3 Date: Mon, 15 Jan 2018 10:31:55 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Hoogle q5.8: write your own findindices Message-ID: <can+tr42uzvhrzpkikhbejwxkcodnunuxtxhszu1kq1bcxgc...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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.ship...@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@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-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 115, Issue 6 *****************************************