From trent.shipley at gmail.com Tue Jan 2 08:48:34 2018 From: trent.shipley at gmail.com (trent shipley) Date: Tue, 02 Jan 2018 08:48:34 +0000 Subject: [Haskell-beginners] Simplified Luhn Algorithm In-Reply-To: References: Message-ID: luhnDouble = (2*x) `mod` 9 almost works, but produces 0 for 2*9 when the answer should be 9. So. luhnDouble :: Int -> Int luhnDouble x = let y = (2 * x) in if y > 9 then y - 9 else y (Note, at this point in the book, the "let" trick has not been introduced.) luhn :: Int -> Int -> Int -> Int -> Bool luhn x1 x2 x3 x4 = 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod` 10 (This suggested modification works wonderfully.) A solution for a list of arbitrary length awaits chapter 7, this question was from chapter 4. Note, Wikipedia.en implies that originally the digits of the multiplication result were to be added, as in 8 * 2 = 16 = 1 + 6 = 7 OR 16 - 9 = 7. On Sun, Dec 31, 2017 at 8:09 AM Alex Rozenshteyn wrote: > Haskell can totally return the result of the "==", and that would be one > of my suggestions as well. The other suggestion is for "luhnDouble": I > would just compute `rem (2 * x) 9`, but if you need to explicitly subtract, > you can do `let d = 2 * x in if d > 9 then d - 9 else d`, which does the > computation just once. > > > On Sun, Dec 31, 2017 at 9:12 AM John Lusk wrote: > >> Looks fine to me. Maybe drop the if-then, and simply return the result of >> the == ? (Maybe not possible in Haskell (I'm just a duffer myself) but >> extraneous trues and falses always drive me nuts.) >> >> -- >> Sent from my tablet, which has a funny keyboard. Makes me sound more >> curt and muted than normal. >> >> On Dec 30, 2017 11:03 PM, "trent shipley" >> wrote: >> >>> I have the following, and it works, but I am trying teach myself >>> Haskell, and I have the suspicion that my solutions is both inefficient and >>> graceless. Any feedback would be appreciated. >>> >>> Trent. >>> >>> ------------------------------------ >>> >>> {- >>> 8.The Luhn algorithm is used to check bank card numbers for simple >>> errors such as mistyping a digit, and proceeds as follows: >>> >>> * consider each digit as a separate number; >>> * moving left, double every other number from the second last; >>> * subtract 9 from each number that is now greater than 9; >>> * add all the resulting numbers together; >>> * if the total is divisible by 10, the card number is valid. >>> >>> Define a function luhnDouble :: Int -> Int that doubles a digit >>> and subtracts 9 if the result is greater than 9. >>> >>> For example: >>> >>> > luhnDouble 3 >>> 6 >>> >>> > luhnDouble 6 >>> 3 >>> >>> Using luhnDouble and the integer remainder function mod, define a >>> function >>> luhn :: Int -> Int -> Int -> Int -> Bool >>> that decides if a four-digit bank card number is valid. >>> >>> For example: >>> > luhn 1 7 8 4 >>> True >>> >>> > luhn 4 7 8 3 >>> False >>> >>> In the exercises for chapter 7 we will consider a more general version >>> of this function that accepts card numbers of any length. >>> >>> Hutton, Graham. Programming in Haskell (pp. 45-46). Cambridge University >>> Press. Kindle Edition. >>> -} >>> >>> luhnDouble :: Int -> Int >>> luhnDouble x = if (2 * x) > 9 >>> then (2 * x) - 9 >>> else 2 * x >>> >>> >>> luhn :: Int -> Int -> Int -> Int -> Bool >>> luhn x1 x2 x3 x4 = if 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] >>> `mod` 10 >>> then True >>> else False >>> >>> >>> >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >>> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From aquagnu at gmail.com Wed Jan 3 12:14:54 2018 From: aquagnu at gmail.com (Baa) Date: Wed, 3 Jan 2018 14:14:54 +0200 Subject: [Haskell-beginners] Version mismatch with Stack Message-ID: <20180103141454.41051c14@Pavel> Hello! I have library: mylib. And application: myapp. Library will use Cardinality package. So, I added to mylib's stack.yaml: ==added=== extra-deps: - https://hackage.haskell.org/package/Cardinality-0.2/Cardinality-0.2.tar.gz # Override default flag values for local packages and extra-deps flags: Cardinality: allow-never: true ===end-of-added=== and I added to mylib's cabal file: ===added=== build-depends: ... ... , Cardinality ===end-of-added=== Seems that it's compiling. OK, now I try to build myapp, which early was building without any problems (it depends on mylib). But I get error: ===error=== Cardinality must match -any, but the stack configuration has no specified version (latest matching version is 0.2) needed since mylib is a build target. Some potential ways to resolve this: * Recommended action: try adding the following to your extra-deps in /xxxxx/myapp/stack.yaml: - Cardinality-0.2 * Set 'allow-newer: true' to ignore all version constraints and build anyway. * You may also want to try using the 'stack solver' command. Plan construction failed. ===end-of-error=== So, what am I doing wrongly? Why stack want adding of the same things in myapp's stack.yaml which were already added in mylib's stack.yaml?! /Best regards, Paul From r.mark.volkmann at gmail.com Sat Jan 6 13:22:18 2018 From: r.mark.volkmann at gmail.com (Mark Volkmann) Date: Sat, 6 Jan 2018 07:22:18 -0600 Subject: [Haskell-beginners] hspec questions Message-ID: It seems that I can use either "before" or "before_" to specify a function to run before each of my tests. However, to specify a function to run after each of my tests I can use "after_", but not "after". Is that correct? Also, I can't get "around_" or "around" to work. The example at https://hspec.github.io/writing-specs.html uses the bracket function which I hadn't seen before, but it doesn't work for me. I'd love to see another example of this. -- R. Mark Volkmann Object Computing, Inc. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aquagnu at gmail.com Mon Jan 8 13:20:20 2018 From: aquagnu at gmail.com (Pv) Date: Mon, 8 Jan 2018 15:20:20 +0200 Subject: [Haskell-beginners] Replace instances of module Message-ID: <20180108152020.60f7318a@Pavel> Hello, all. Is it possible to import all types except some of instances and to re-defined hiding instances, so resulting (new) module will be the same types, mostly the same instances, but some of instances will be replaced? I tried to import all hiding interesting type, then to import interesting type without instances, but when I redefine its instances I get error about duplication of them: import Old hiding (MyType) import Old (MyType) instance ... MyType where <-- error!! Interesting that in Old module I'm exporting MyType without instances: module (... , MyType, ...) where Goal is to replace some instances and may be some types instead of copy-paste all module (something like patching or inheritance of module). === Best regards, Paul From fa-ml at ariis.it Mon Jan 8 14:02:44 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Mon, 8 Jan 2018 15:02:44 +0100 Subject: [Haskell-beginners] Replace instances of module In-Reply-To: <20180108152020.60f7318a@Pavel> References: <20180108152020.60f7318a@Pavel> Message-ID: <20180108140244.anbavce4lcd2atnt@x60s.casa> Hello Paul, On Mon, Jan 08, 2018 at 03:20:20PM +0200, Pv wrote: > Hello, all. > > Is it possible to import all types except some of instances and to > re-defined hiding instances, so resulting (new) module will be the same > types, mostly the same instances, but some of instances will be > replaced? Nope, instances are automatically imported! You can create newtypes and have them instances of what you need or check the newfangled `backpack` [1]! [1] https://ghc.haskell.org/trac/ghc/wiki/Backpack From aquagnu at gmail.com Mon Jan 8 14:31:53 2018 From: aquagnu at gmail.com (Baa) Date: Mon, 8 Jan 2018 16:31:53 +0200 Subject: [Haskell-beginners] Replace instances of module In-Reply-To: <20180108140244.anbavce4lcd2atnt@x60s.casa> References: <20180108152020.60f7318a@Pavel> <20180108140244.anbavce4lcd2atnt@x60s.casa> Message-ID: <20180108163153.478949d4@Pavel> Hello, Francesco. Actually the whole problem is that I have library (consisting of several modules: types, API calls, JSON instances, etc) to work with some REST service. Now I have 2 nodes with this service: version 1 and version 2, so API is little bit changed in version 2 (mostly in JSON instances, but types seems to be the same, API calls will be the same too). So, clients must work with both services: of version 1 and version 2. And I can create new library, but this will be big copy-paste, because mostly code (except JSON instances) will be the same. Different will be returned value in some API calls, maybe - I'm not sure. Nothing else. So, if I'll create new version of library: how they will live together with the same names, etc, only different version?! And common code-base will lead to complex bug-fixing and test modification. If I'll name new library like "oldlibv2" - problem with bug fixing and testing is still here. My idea was to "patch" in some way existing library by import some modules into new modules and reimplement some instances only. May be the same with API calls, I'm not sure here. I know that it's easy in F#, ML and Python, for example. Are backpacks something like ML modules? I see that they are not supported by stack. What does it mean? "import" will not work? Actually I don;t know - is some better way to solve such problem? I'm sure many of us communicate with some RESTfull APIs. Then APIs change. And your apps should work with old and new APIs (services), so you should clone existing library to support both versions (together, at the same time, in one client application!). === Best regards, Paul > Hello Paul, > > On Mon, Jan 08, 2018 at 03:20:20PM +0200, Pv wrote: > > Hello, all. > > > > Is it possible to import all types except some of instances and to > > re-defined hiding instances, so resulting (new) module will be the > > same types, mostly the same instances, but some of instances will be > > replaced? > > Nope, instances are automatically imported! You can create newtypes > and have them instances of what you need or check the newfangled > `backpack` [1]! > > [1] https://ghc.haskell.org/trac/ghc/wiki/Backpack > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From toad3k at gmail.com Mon Jan 8 15:31:20 2018 From: toad3k at gmail.com (David McBride) Date: Mon, 8 Jan 2018 10:31:20 -0500 Subject: [Haskell-beginners] Replace instances of module In-Reply-To: <20180108163153.478949d4@Pavel> References: <20180108152020.60f7318a@Pavel> <20180108140244.anbavce4lcd2atnt@x60s.casa> <20180108163153.478949d4@Pavel> Message-ID: The reason you don't get to override instances within a package is that haskell accounts for the fact that a library user could import both old and new, ending up with two conflicting instances for the same type. If one of the types now needs two different JSON instances, one way to do that is to wrap it in a newtype that has a different JSON instance. data MyType = ... -- old type newtype MyType_v2 = MyType_v2 MyType -- data MyType_v2 = ... -- alternatively, define same structure, if it didn't change. instance JSON MyType_v2 = ... - slightly different json instance. Then you can have a module that has a function that returns MyType_v2 from its functions instead and exports a JSON instance for it. Users can import either or both and will have access to both instances and both types. You could also do it in the reverse, with a _v1 type, but old clients will have to be updated. As an addendum, you might future proof your API a little by having a phantom type variable. data V1 data V2 data MyType tag = ... -- old type v1_func :: MyType V1 v1_func = undefined v2_func :: MyType V2 v2_func = undefined At the cost of a slightly more complicated API for your users. But that won't protect you if MyType itself changes. On Mon, Jan 8, 2018 at 9:31 AM, Baa wrote: > Hello, Francesco. > > Actually the whole problem is that I have library (consisting of > several modules: types, API calls, JSON instances, etc) to work with > some REST service. Now I have 2 nodes with this service: version 1 and > version 2, so API is little bit changed in version 2 (mostly in JSON > instances, but types seems to be the same, API calls will be the same > too). > > So, clients must work with both services: of version 1 and version 2. > And I can create new library, but this will be big copy-paste, because > mostly code (except JSON instances) will be the same. Different will be > returned value in some API calls, maybe - I'm not sure. Nothing else. > > So, if I'll create new version of library: how they will live together > with the same names, etc, only different version?! And common > code-base will lead to complex bug-fixing and test modification. If > I'll name new library like "oldlibv2" - problem with bug fixing and > testing is still here. > > My idea was to "patch" in some way existing library by import some > modules into new modules and reimplement some instances only. May be > the same with API calls, I'm not sure here. > > I know that it's easy in F#, ML and Python, for example. Are backpacks > something like ML modules? I see that they are not supported by stack. > What does it mean? "import" will not work? > > Actually I don;t know - is some better way to solve such problem? I'm > sure many of us communicate with some RESTfull APIs. Then APIs change. > And your apps should work with old and new APIs (services), so you > should clone existing library to support both versions (together, at > the same time, in one client application!). > > === > Best regards, Paul > > > Hello Paul, > > > > On Mon, Jan 08, 2018 at 03:20:20PM +0200, Pv wrote: > > > Hello, all. > > > > > > Is it possible to import all types except some of instances and to > > > re-defined hiding instances, so resulting (new) module will be the > > > same types, mostly the same instances, but some of instances will be > > > replaced? > > > > Nope, instances are automatically imported! You can create newtypes > > and have them instances of what you need or check the newfangled > > `backpack` [1]! > > > > [1] https://ghc.haskell.org/trac/ghc/wiki/Backpack > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Mon Jan 8 15:50:45 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Mon, 8 Jan 2018 16:50:45 +0100 Subject: [Haskell-beginners] Replace instances of module In-Reply-To: <20180108163153.478949d4@Pavel> References: <20180108152020.60f7318a@Pavel> <20180108140244.anbavce4lcd2atnt@x60s.casa> <20180108163153.478949d4@Pavel> Message-ID: <20180108155045.tlq4lp3we5imyv6h@x60s.casa> On Mon, Jan 08, 2018 at 04:31:53PM +0200, Baa wrote: > Actually I don;t know - is some better way to solve such problem? I'm > sure many of us communicate with some RESTfull APIs. Then APIs change. > And your apps should work with old and new APIs (services), so you > should clone existing library to support both versions (together, at > the same time, in one client application!). A somewhat primitive but doable solution would be: instead of using instances pass a datatype around. E.g. data J a = J { readjs :: a -> MyTpe } myfun :: J a -> a -> Int -- when before it probably was -- myfun :: FromJson a => a -> Int You could enforce some sanity at type level with phantom types, e.g.: data InterfaceA data InterfaceB data J a b = -- something -- J is to be exported without (..) creaIntfA :: J a InterfaceA > Are backpacks something like ML modules? Indeed backpack is a way to have an ML-like module system in Haskell. The question is interesting and a "real life" one. I urge you to post it on cafe- too, I am sure there will be some more ideas/solutions in there. From aquagnu at gmail.com Mon Jan 8 16:48:14 2018 From: aquagnu at gmail.com (Baa) Date: Mon, 8 Jan 2018 18:48:14 +0200 Subject: [Haskell-beginners] Replace instances of module In-Reply-To: References: <20180108152020.60f7318a@Pavel> <20180108140244.anbavce4lcd2atnt@x60s.casa> <20180108163153.478949d4@Pavel> Message-ID: <20180108184814.7844a05f@Pavel> Thanks very much, David and Francesco! I got your solutions. > The reason you don't get to override instances within a package is > that haskell accounts for the fact that a library user could import > both old and new, ending up with two conflicting instances for the > same type. > > If one of the types now needs two different JSON instances, one way > to do that is to wrap it in a newtype that has a different JSON > instance. > > data MyType = ... -- old type > > newtype MyType_v2 = MyType_v2 MyType > -- data MyType_v2 = ... -- alternatively, define same structure, if it > didn't change. > > instance JSON MyType_v2 = ... - slightly different json instance. > > Then you can have a module that has a function that returns MyType_v2 > from its functions instead and exports a JSON instance for it. Users > can import either or both and will have access to both instances and > both types. > > You could also do it in the reverse, with a _v1 type, but old clients > will have to be updated. > > As an addendum, you might future proof your API a little by having a > phantom type variable. > > data V1 > data V2 > data MyType tag = ... -- old type > > v1_func :: MyType V1 > v1_func = undefined > v2_func :: MyType V2 > v2_func = undefined > > At the cost of a slightly more complicated API for your users. But > that won't protect you if MyType itself changes. > > On Mon, Jan 8, 2018 at 9:31 AM, Baa wrote: > > > Hello, Francesco. > > > > Actually the whole problem is that I have library (consisting of > > several modules: types, API calls, JSON instances, etc) to work with > > some REST service. Now I have 2 nodes with this service: version 1 > > and version 2, so API is little bit changed in version 2 (mostly in > > JSON instances, but types seems to be the same, API calls will be > > the same too). > > > > So, clients must work with both services: of version 1 and version > > 2. And I can create new library, but this will be big copy-paste, > > because mostly code (except JSON instances) will be the same. > > Different will be returned value in some API calls, maybe - I'm not > > sure. Nothing else. > > > > So, if I'll create new version of library: how they will live > > together with the same names, etc, only different version?! And > > common code-base will lead to complex bug-fixing and test > > modification. If I'll name new library like "oldlibv2" - problem > > with bug fixing and testing is still here. > > > > My idea was to "patch" in some way existing library by import some > > modules into new modules and reimplement some instances only. May be > > the same with API calls, I'm not sure here. > > > > I know that it's easy in F#, ML and Python, for example. Are > > backpacks something like ML modules? I see that they are not > > supported by stack. What does it mean? "import" will not work? > > > > Actually I don;t know - is some better way to solve such problem? > > I'm sure many of us communicate with some RESTfull APIs. Then APIs > > change. And your apps should work with old and new APIs (services), > > so you should clone existing library to support both versions > > (together, at the same time, in one client application!). > > > > === > > Best regards, Paul > > > > > Hello Paul, > > > > > > On Mon, Jan 08, 2018 at 03:20:20PM +0200, Pv wrote: > > > > Hello, all. > > > > > > > > Is it possible to import all types except some of instances and > > > > to re-defined hiding instances, so resulting (new) module will > > > > be the same types, mostly the same instances, but some of > > > > instances will be replaced? > > > > > > Nope, instances are automatically imported! You can create > > > newtypes and have them instances of what you need or check the > > > newfangled `backpack` [1]! > > > > > > [1] https://ghc.haskell.org/trac/ghc/wiki/Backpack > > > _______________________________________________ > > > Beginners mailing list > > > Beginners at haskell.org > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > From trent.shipley at gmail.com Mon Jan 15 15:02:35 2018 From: trent.shipley at gmail.com (trent shipley) Date: Mon, 15 Jan 2018 15:02:35 +0000 Subject: [Haskell-beginners] Hoogle q5.8: write your own findindices Message-ID: {- 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: From raabe at froglogic.com Mon Jan 15 15:25:58 2018 From: raabe at froglogic.com (Frerich Raabe) Date: Mon, 15 Jan 2018 16:25:58 +0100 Subject: [Haskell-beginners] Hoogle q5.8: write your own findindices In-Reply-To: References: Message-ID: 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 - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From toad3k at gmail.com Mon Jan 15 15:31:55 2018 From: toad3k at gmail.com (David McBride) Date: Mon, 15 Jan 2018 10:31:55 -0500 Subject: [Haskell-beginners] Hoogle q5.8: write your own findindices In-Reply-To: References: Message-ID: 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 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: From trent.shipley at gmail.com Thu Jan 18 07:58:08 2018 From: trent.shipley at gmail.com (trent shipley) Date: Thu, 18 Jan 2018 07:58:08 +0000 Subject: [Haskell-beginners] Hoogle q5.8: write your own findindices In-Reply-To: References: Message-ID: {- 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. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrik.mrx at gmail.com Sat Jan 20 20:20:33 2018 From: patrik.mrx at gmail.com (Patrik Iselind) Date: Sat, 20 Jan 2018 21:20:33 +0100 Subject: [Haskell-beginners] Making my own type belong to Data.Ord Message-ID: Hi, I'm trying to follow the "Real World Haskell" book online and have reached exercise 12 in chapter three. See the bottom of http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html for reference. It's the very last exercise on the page concerning constructing a Convex Hull. I have no problem implementing a Graham Scan in non-functional languages as i have far more experience with those. But the book hasn't yet covered how i make my own data belong to for example the Data.Ord class. The question then for me is how am i supposed to sort my coordinates? Type classes are not covered for another three chapters in the book. Given this it seems to me that i should be able to solve this without using Data.Ord, i just cannot see how. Any pointers would be highly appreciated. -- Patrik Iselind From fa-ml at ariis.it Sat Jan 20 20:27:52 2018 From: fa-ml at ariis.it (Francesco Ariis) Date: Sat, 20 Jan 2018 21:27:52 +0100 Subject: [Haskell-beginners] Making my own type belong to Data.Ord In-Reply-To: References: Message-ID: <20180120202752.4nsc5lswjl2zwth4@x60s.casa> On Sat, Jan 20, 2018 at 09:20:33PM +0100, Patrik Iselind wrote: > It's the very last exercise on the page concerning constructing a Convex > Hull. I have no problem implementing a Graham Scan in non-functional > languages as i have far more experience with those. But the book hasn't yet > covered how i make my own data belong to for example the Data.Ord class. The > question then for me is how am i supposed to sort my coordinates? > > Type classes are not covered for another three chapters in the book. Given > this it seems to me that i should be able to solve this without using > Data.Ord, i just cannot see how. Any pointers would be highly appreciated. Hello Patrik, as you suggested, implementing an instance of Data.Ord would be ok; if you don't want that, just write a myTypeCompare :: SomeData -> SomeData -> Ordering function to use with `sortBy` from `Data.List`. Does that make sense? -F From patrik.mrx at gmail.com Sat Jan 20 20:48:26 2018 From: patrik.mrx at gmail.com (Patrik Iselind) Date: Sat, 20 Jan 2018 21:48:26 +0100 Subject: [Haskell-beginners] Making my own type belong to Data.Ord In-Reply-To: <20180120202752.4nsc5lswjl2zwth4@x60s.casa> References: <20180120202752.4nsc5lswjl2zwth4@x60s.casa> Message-ID: <23095b91-a353-c4b3-32d5-86e276b69fed@gmail.com> Den 2018-01-20 kl. 21:27, skrev Francesco Ariis: > On Sat, Jan 20, 2018 at 09:20:33PM +0100, Patrik Iselind wrote: >> It's the very last exercise on the page concerning constructing a Convex >> Hull. I have no problem implementing a Graham Scan in non-functional >> languages as i have far more experience with those. But the book hasn't yet >> covered how i make my own data belong to for example the Data.Ord class. The >> question then for me is how am i supposed to sort my coordinates? >> >> Type classes are not covered for another three chapters in the book. Given >> this it seems to me that i should be able to solve this without using >> Data.Ord, i just cannot see how. Any pointers would be highly appreciated. > Hello Patrik, > as you suggested, implementing an instance of Data.Ord would be ok; if > you don't want that I wouldn't mind writing it, at all. It's probably the way i would do it if i knew more Haskell. My problem is just that the book i follow hasn't covered that topic yet, so even if i could google how to do it i guess i shouldn't until the book has covered the topic. As i understand it this would be the 'preferred' way in the real world, is this correctly understood? > , just write a > > myTypeCompare :: SomeData -> SomeData -> Ordering > > function to use with `sortBy` from `Data.List`. > Does that make sense? That would make sense, i seem to remember something along those lines from previous topics covered in the book. Thanks a lot Francesco, much appreciated! // Patrik