From martin.drautzburg at web.de Tue Apr 1 04:15:58 2014 From: martin.drautzburg at web.de (martin) Date: Tue, 01 Apr 2014 06:15:58 +0200 Subject: [Haskell-beginners] Mapping inside State In-Reply-To: References: <533992B8.3090206@web.de> Message-ID: <533A3D7E.9030401@web.de> Am 03/31/2014 10:42 PM, schrieb Chadda? Fouch?: > > Le 31 mars 2014 18:07, "martin" > a ?crit : >> >> Hello all, >> >> I found myselft writing this ugly piece of code >> >> silenceAll :: TI -> State (M.Map CH StringState,[DtzEvent]) TI >> silenceAll t = > Cut >> >> because I couldn't figure out how to map (silence t) over [0..6]. What is the standard way of doing this > > As said, mapM_ is the standard way to do that . > How could you have found this for yourself ? The recommended way would be to use hoogle to search for the type signature > you wish : > (Monad m) => (a -> m b) -> [a] -> m () > mapM_ would be among the first results. Thanks Ozgur and Chaddai, this works. Two more questions: (1) I had tried mapM, but this didn't work. I am having trouble to understand why the return type is m() and not m [a] (2) Hoogle is http://www.haskell.org/hoogle, right? I mean, it is a web thing, not something I use from the commandline? From karl at karlv.net Tue Apr 1 04:36:17 2014 From: karl at karlv.net (Karl Voelker) Date: Mon, 31 Mar 2014 21:36:17 -0700 Subject: [Haskell-beginners] Mapping inside State In-Reply-To: <533A3D7E.9030401@web.de> References: <533992B8.3090206@web.de> <533A3D7E.9030401@web.de> Message-ID: <1396326977.13383.101266373.5B6F5D9E@webmail.messagingengine.com> On Mon, Mar 31, 2014, at 09:15 PM, martin wrote: > (1) I had tried mapM, but this didn't work. I am having trouble to > understand why the return type is m() and not m [a] mapM_ is like mapM, except that mapM_ ignores the result value. In other words, mapM_ is for when you only want the monadic effects. You could define it like this: mapM_ f xs = mapM f xs >> return () -Karl From nadirsampaoli at gmail.com Tue Apr 1 05:31:39 2014 From: nadirsampaoli at gmail.com (Nadir Sampaoli) Date: Tue, 1 Apr 2014 07:31:39 +0200 Subject: [Haskell-beginners] Mapping inside State In-Reply-To: <533A3D7E.9030401@web.de> References: <533992B8.3090206@web.de> <533A3D7E.9030401@web.de> Message-ID: Il 01/apr/2014 06:17 "martin" ha scritto: > > (2) Hoogle is http://www.haskell.org/hoogle, right? I mean, it is a web thing, not something I use from the commandline? > You can also install it locally (depending on your OS/distro you might find a pre-packaged version or install it through cabal-install) and use it in the shell or even from inside ghci if you create the appropriate bindings in your ghci config. -- Nadir -------------- next part -------------- An HTML attachment was scrubbed... URL: From nishantgeek at gmail.com Wed Apr 2 10:53:33 2014 From: nishantgeek at gmail.com (Nishant) Date: Wed, 2 Apr 2014 16:23:33 +0530 Subject: [Haskell-beginners] Functor on Tree data constructor Message-ID: Hi, Can some explain me the error and a possibly a fix : data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show instance Functor Tree where ............fmap f NULL = NULL ............fmap f (Node left x right) | (left == NULL) && (right == NULL) = Node left (f x) right ................................................... | (left /= NULL) = Node (fmap f left) x right ................................................... | (right /= NULL) = Node left x (fmap f right) ghci gives: Prelude> :load programs\tree.hs [1 of 1] Compiling Main ( programs\tree.hs, interpreted ) programs\tree.hs:32:78: Couldn't match type `a' with `b' `a' is a rigid type variable bound by the type signature for fmap :: (a -> b) -> Tree a -> Tree b at programs\tree.hs:31:7 `b' is a rigid type variable bound by the type signature for fmap :: (a -> b) -> Tree a -> Tree b at programs\tree.hs:31:7 Expected type: Tree b Actual type: Tree a In the first argument of `Node', namely `left' In the expression: Node left (f x) right In an equation for `fmap': fmap f (Node left x right) | (left == NULL) && (right == NULL) = Node left (f x) right | (left /= NULL) = Node (fmap f left) x right | (right /= NULL) = Node left x (fmap f right) Failed, modules loaded: none. Regards. Nishant -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Wed Apr 2 11:07:34 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Wed, 02 Apr 2014 13:07:34 +0200 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: References: Message-ID: <26d2d8ce546f74acd2f92346fd36d117@roundcube.froglogic.com> On 2014-04-02 12:53, Nishant wrote: > Can some explain me the error and a possibly ?a fix :? > > data Tree a = NULL | Node (Tree a ) a (Tree a) > > instance Functor Tree where > fmap f NULL = NULL > fmap f (Node left x right) | (left == NULL) && (right == NULL) = > Node left (f x) right > | (left /= NULL) = Node (fmap f left) > x right > | (right /= NULL) = Node left x (fmap > f right) [..] > programstree.hs:32:78: > Couldn't match type `a' with `b' > ? ? ? `a' is a rigid type variable bound by > ? ? ? ? ? the type signature for fmap :: (a -> b) -> Tree a -> Tree b > ? ? ? ? ? at programstree.hs:31:7 > ? ? ? `b' is a rigid type variable bound by > ? ? ? ? ? the type signature for fmap :: (a -> b) -> Tree a -> Tree b > ? ? ? ? ? at programstree.hs:31:7 > ? ? Expected type: Tree b > ? ? ? Actual type: Tree a > ? ? In the first argument of `Node', namely `left' The issue is that in 'fmap f (Node left x right)', 'left' has type 'Tree a'. Your 'fmap' function should yield a 'Tree b'though, i.e. the 'Node' constructor should take a 'Tree b' as well - and 'left' doesn't fit. You can fix this by using 'NULL' directly, e.g. instance Functor Tree where fmap f NULL = NULL fmap f (Node left x right) | (left == NULL) && (right == NULL) = Node NULL (f x) NULL | (left /= NULL) = Node (fmap f left) (f x) NULL | (right /= NULL) = Node NULL (f x) (fmap f right) ...note that instead of 'x' you also have to use 'f x' (which is also the whole point of a Functor instance). -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From daniel.trstenjak at gmail.com Wed Apr 2 11:12:09 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Wed, 2 Apr 2014 13:12:09 +0200 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: References: Message-ID: <20140402111209.GA8573@machine> Hi Nishant, > fmap f (Node left x right) | (left ?== NULL) && (right == NULL) = > Node left (f x) right The function 'f' maps from 'a -> b' and 'left' and 'right' have the type 'Tree a', by not mapping 'left' and 'right' with 'f', you're telling the compiler that the types 'a' and 'b' are equal, but the compiler doesn't consider these types to be equal. You also have to map 'left' and 'right': instance Functor Tree where fmap f NULL = NULL fmap f (Node left a right) = Node (fmap f left) (f a) (fmap f right) Greetings, Daniel From gesh at gesh.uni.cx Wed Apr 2 11:37:03 2014 From: gesh at gesh.uni.cx (Gesh) Date: Wed, 02 Apr 2014 14:37:03 +0300 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: <26d2d8ce546f74acd2f92346fd36d117@roundcube.froglogic.com> References: <26d2d8ce546f74acd2f92346fd36d117@roundcube.froglogic.com> Message-ID: <63aa7bcb-9eac-4810-a0f3-d5e1677638de@email.android.com> >On 2014-04-02 12:53, Nishant wrote: >> Can some explain me the error and a possibly ?a fix :? >> >> data Tree a = NULL | Node (Tree a ) a (Tree a) >> >> instance Functor Tree where >> fmap f NULL = NULL >> fmap f (Node left x right) | (left == NULL) && (right == NULL) = > >> Node left (f x) right >> | (left /= NULL) = Node (fmap f left) > >> x right >> | (right /= NULL) = Node left x (fmap >> f right) Note that you can simplify your instance declaration to: > instance Functor Tree where > fmap f NULL = NULL > fmap f (Node left x right) = Node (f' left) (f x) (f' right) > where f' x = if x == NULL then NULL else fmap f x Also note that the NULL in the then clause differs from x. Let f :: a -> b, then x :: Tree a and the NULL in that clause :: Tree b. These values are as distinct as 2 :: Int and 2.0 :: Double. HTH, Gesh From tonymorris at gmail.com Wed Apr 2 11:42:39 2014 From: tonymorris at gmail.com (Tony Morris) Date: Thu, 03 Apr 2014 00:42:39 +1300 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: <63aa7bcb-9eac-4810-a0f3-d5e1677638de@email.android.com> References: <26d2d8ce546f74acd2f92346fd36d117@roundcube.froglogic.com> <63aa7bcb-9eac-4810-a0f3-d5e1677638de@email.android.com> Message-ID: <533BF7AF.8050709@gmail.com> On 03/04/14 00:37, Gesh wrote: >> On 2014-04-02 12:53, Nishant wrote: >>> Can some explain me the error and a possibly a fix : >>> >>> data Tree a = NULL | Node (Tree a ) a (Tree a) >>> >>> instance Functor Tree where >>> fmap f NULL = NULL >>> fmap f (Node left x right) | (left == NULL) && (right == NULL) = >>> Node left (f x) right >>> | (left /= NULL) = Node (fmap f left) >>> x right >>> | (right /= NULL) = Node left x (fmap >>> f right) > Note that you can simplify your instance declaration to: >> instance Functor Tree where >> fmap f NULL = NULL >> fmap f (Node left x right) = Node (f' left) (f x) (f' right) >> where f' x = if x == NULL then NULL else fmap f x > Also note that the NULL in the then clause differs from x. Let f :: a -> b, then x :: Tree a and the NULL in that clause :: Tree b. These values are as distinct as 2 :: Int and 2.0 :: Double. > HTH, > Gesh > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners instance Functor Tree where fmap f NULL = NULL fmap f (Node left x right) = Node (f' left) (f x) (f' right) where f' = fmap f -- Tony Morris http://tmorris.net/ From gesh at gesh.uni.cx Wed Apr 2 12:19:18 2014 From: gesh at gesh.uni.cx (Gesh) Date: Wed, 02 Apr 2014 15:19:18 +0300 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: <533BF7AF.8050709@gmail.com> References: <26d2d8ce546f74acd2f92346fd36d117@roundcube.froglogic.com> <63aa7bcb-9eac-4810-a0f3-d5e1677638de@email.android.com> <533BF7AF.8050709@gmail.com> Message-ID: On April 2, 2014 2:42:39 PM GMT+03:00, Tony Morris wrote: >On 03/04/14 00:37, Gesh wrote: >>> On 2014-04-02 12:53, Nishant wrote: >>>> Can some explain me the error and a possibly a fix : >>>> >>>> data Tree a = NULL | Node (Tree a ) a (Tree a) >>>> >>>> instance Functor Tree where >>>> fmap f NULL = NULL >>>> fmap f (Node left x right) | (left == NULL) && (right == NULL) >= >>>> Node left (f x) right >>>> | (left /= NULL) = Node (fmap f >left) >>>> x right >>>> | (right /= NULL) = Node left x >(fmap >>>> f right) >> Note that you can simplify your instance declaration to: >>> instance Functor Tree where >>> fmap f NULL = NULL >>> fmap f (Node left x right) = Node (f' left) (f x) (f' right) >>> where f' x = if x == NULL then NULL else fmap f x >> Also note that the NULL in the then clause differs from x. Let f :: a >-> b, then x :: Tree a and the NULL in that clause :: Tree b. These >values are as distinct as 2 :: Int and 2.0 :: Double. >> HTH, >> Gesh >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > >instance Functor Tree where > fmap f NULL = NULL > fmap f (Node left x right) = Node (f' left) (f x) (f' right) > where f' = fmap f Oops. I should have noticed my case analysis was unnecessary. Still, the comments accompanying it are correct. NULL :: Tree a and NULL :: Tree b are distinct values of distinct types, and therefore using one where the other is expected will make GHC disappointed in you. Gesh From vlatko.basic at gmail.com Wed Apr 2 13:54:39 2014 From: vlatko.basic at gmail.com (Vlatko Basic) Date: Wed, 02 Apr 2014 15:54:39 +0200 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: References: Message-ID: <533C169F.90408@gmail.com> An HTML attachment was scrubbed... URL: From gilbertomelfe at gmail.com Wed Apr 2 14:56:12 2014 From: gilbertomelfe at gmail.com (Gilberto Melfe) Date: Wed, 2 Apr 2014 15:56:12 +0100 Subject: [Haskell-beginners] Help installing Esqueleto (for Yesod)! Message-ID: Hello community! I'm trying to install Yesod on a freshly setup openSUSE 13.1 virtual machine! I installed the Haskell Platform and three other (ghc) packages required (for yesod-bin), from the standard online repositories! Then i issued: cabal update cabal install cabal-install (because cabal told me so) export PATH=$HOME/.cabal/bin:$PATH cabal install yesod-platform After churning for a while, that last command stated that some packages, more precisely the Esqueleto package, failed to install. I tried to install it alone and got these error messages: -- Begin Command Output Resolving dependencies... Configuring esqueleto-1.3.5... Building esqueleto-1.3.5... Preprocessing library esqueleto-1.3.5... [1 of 4] Compiling Database.Esqueleto.Internal.PersistentImport ( src/Database/Esqueleto/Internal/PersistentImport.hs, dist/build/Database/Esqueleto/Internal/PersistentImport.o ) [2 of 4] Compiling Database.Esqueleto.Internal.Language ( src/Database/Esqueleto/Internal/Language.hs, dist/build/Database/Esqueleto/Internal/Language.o ) [3 of 4] Compiling Database.Esqueleto.Internal.Sql ( src/Database/Esqueleto/Internal/Sql.hs, dist/build/Database/Esqueleto/Internal/Sql.o ) src/Database/Esqueleto/Internal/Sql.hs:531:46: Not in scope: type constructor or class `C.ResourceT' src/Database/Esqueleto/Internal/Sql.hs:559:42: Not in scope: type constructor or class `C.ResourceT' src/Database/Esqueleto/Internal/Sql.hs:619:31: Not in scope: type constructor or class `C.ResourceT' src/Database/Esqueleto/Internal/Sql.hs:634:24: Not in scope: type constructor or class `C.ResourceT' src/Database/Esqueleto/Internal/Sql.hs:636:17: Not in scope: `C.runResourceT' Failed to install esqueleto-1.3.5 cabal: Error: some packages failed to install: esqueleto-1.3.5 failed during the building phase. The exception was: ExitFailure 1 -- End Command Output Can someone figure out what might be wrong? (I guess it's not a dependencies issue...) Thank You Very Much, in advance, for your time! Gilberto PS: Should I have included the command output in an attached file? Which "method" is preferred? -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed Apr 2 16:00:47 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 2 Apr 2014 12:00:47 -0400 Subject: [Haskell-beginners] Help installing Esqueleto (for Yesod)! In-Reply-To: References: Message-ID: Conduit was changed so that it no longer exports resourcet related types and functions, and esqueleto is attempting to use those. It is a bug in esqueleto, and I've registered a pull request on github here: https://github.com/meteficha/esqueleto/pull/55 While you are waiting for that to be merged, you can pull from https://github.com/mindreader/esqueleto.git. Just git clone it, cd into the directory, then cabal install. On Wed, Apr 2, 2014 at 10:56 AM, Gilberto Melfe wrote: > Hello community! > > I'm trying to install Yesod on a freshly setup openSUSE 13.1 virtual > machine! > > I installed the Haskell Platform and three other (ghc) packages required > (for yesod-bin), from the standard online repositories! > > Then i issued: > cabal update > cabal install cabal-install (because cabal told me so) > export PATH=$HOME/.cabal/bin:$PATH > > cabal install yesod-platform > > After churning for a while, that last command stated that some packages, > more precisely the Esqueleto package, failed to install. > > I tried to install it alone and got these error messages: > > -- Begin Command Output > > Resolving dependencies... > Configuring esqueleto-1.3.5... > Building esqueleto-1.3.5... > Preprocessing library esqueleto-1.3.5... > [1 of 4] Compiling Database.Esqueleto.Internal.PersistentImport ( > src/Database/Esqueleto/Internal/PersistentImport.hs, > dist/build/Database/Esqueleto/Internal/PersistentImport.o ) > [2 of 4] Compiling Database.Esqueleto.Internal.Language ( > src/Database/Esqueleto/Internal/Language.hs, > dist/build/Database/Esqueleto/Internal/Language.o ) > [3 of 4] Compiling Database.Esqueleto.Internal.Sql ( > src/Database/Esqueleto/Internal/Sql.hs, > dist/build/Database/Esqueleto/Internal/Sql.o ) > > src/Database/Esqueleto/Internal/Sql.hs:531:46: > Not in scope: type constructor or class `C.ResourceT' > > src/Database/Esqueleto/Internal/Sql.hs:559:42: > Not in scope: type constructor or class `C.ResourceT' > > src/Database/Esqueleto/Internal/Sql.hs:619:31: > Not in scope: type constructor or class `C.ResourceT' > > src/Database/Esqueleto/Internal/Sql.hs:634:24: > Not in scope: type constructor or class `C.ResourceT' > > src/Database/Esqueleto/Internal/Sql.hs:636:17: > Not in scope: `C.runResourceT' > Failed to install esqueleto-1.3.5 > cabal: Error: some packages failed to install: > esqueleto-1.3.5 failed during the building phase. The exception was: > ExitFailure 1 > > -- End Command Output > > Can someone figure out what might be wrong? > (I guess it's not a dependencies issue...) > > Thank You Very Much, in advance, for your time! > > Gilberto > > PS: Should I have included the command output in an attached file? Which > "method" is preferred? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jzdudek at gmail.com Thu Apr 3 01:52:21 2014 From: jzdudek at gmail.com (Jacek Dudek) Date: Wed, 2 Apr 2014 21:52:21 -0400 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? Message-ID: -- As an exercise I wanted to define a datatype that is an alternating list of elements of two different types. The best that I could do are the type definitions below: module BiList ( BiList (..) , AList (EmptyA) , BList (EmptyB) ) where data BiList a b = Empty | BA (AList a b) | BB (BList a b) data AList a b = EmptyA | AL a (BList a b) data BList a b = EmptyB | BL b (AList a b) (<#) :: a -> (BList a b) -> (AList a b) a <# bs = AL a bs (<@) :: b -> (AList a b) -> (BList a b) b <@ as = BL b as infixr 5 <# infixr 5 <@ example :: BiList Int Char example = BA $ 1 <# 'a' <@ 2 <# 'b' <@ 3 <# 'c' <@ 4 <# 'd' <@ EmptyA -- There are two things that I don't like about this implementation. -- (1) The BA and BB constructors that must be applied on top of instances of (AList a b) and (BList a b) to lift them to be of type (BiList a b). -- (2) Having three different constructors for an empty list: Empty, EmptyA, EmptyB, where ideally I would just have one. -- Is it possible to get around either of these annoyances with some type theory gymnastics? Maybe something like the function fromIntegral (the mechanics of which I don't really understand at this point)? From toad3k at gmail.com Thu Apr 3 02:22:19 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 2 Apr 2014 22:22:19 -0400 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: References: Message-ID: Would this work for you? data BiList a b = Empty | a :# (BiList b a) infixr 5 :# blah :: BiList Char Int blah = 'a' :# 1 :# 'a' :# Empty On Wed, Apr 2, 2014 at 9:52 PM, Jacek Dudek wrote: > -- As an exercise I wanted to define a datatype that is an alternating > list of elements of two different types. The best that I could do are > the type definitions below: > > module BiList > ( BiList (..) > , AList (EmptyA) > , BList (EmptyB) > ) where > > data BiList a b > = Empty > | BA (AList a b) > | BB (BList a b) > > data AList a b > = EmptyA > | AL a (BList a b) > > data BList a b > = EmptyB > | BL b (AList a b) > > (<#) :: a -> (BList a b) -> (AList a b) > a <# bs = AL a bs > > (<@) :: b -> (AList a b) -> (BList a b) > b <@ as = BL b as > > infixr 5 <# > infixr 5 <@ > > example :: BiList Int Char > example = BA $ 1 <# 'a' <@ 2 <# 'b' <@ 3 <# 'c' <@ 4 <# 'd' <@ EmptyA > > -- There are two things that I don't like about this implementation. > > -- (1) The BA and BB constructors that must be applied on top of > instances of (AList a b) and (BList a b) to lift them to be of type > (BiList a b). > > -- (2) Having three different constructors for an empty list: Empty, > EmptyA, EmptyB, where ideally I would just have one. > > -- Is it possible to get around either of these annoyances with some > type theory gymnastics? Maybe something like the function fromIntegral > (the mechanics of which I don't really understand at this point)? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nishantgeek at gmail.com Thu Apr 3 08:50:20 2014 From: nishantgeek at gmail.com (Nishant) Date: Thu, 3 Apr 2014 14:20:20 +0530 Subject: [Haskell-beginners] Functor on Tree data constructor In-Reply-To: <533C169F.90408@gmail.com> References: <533C169F.90408@gmail.com> Message-ID: Thanks Vlatko ... That analogy helped ..... I created my own functor typeclass where i defined fmap :: MyFucntor f => (a->a) -> f a ->f a and that worked ... On Wed, Apr 2, 2014 at 7:24 PM, Vlatko Basic wrote: > Try to visualize with concrete types > > > data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show > > Tree Int = Node (Tree Int) Int (Node Int) -- OK > Tree String = Node (Tree String) String (Node String) -- OK > > f :: Int -> String > f i = show i > > and see what you get: > > > instance Functor Tree where > fmap f NULL = NULL > fmap f (Node left x > right) -- Node > (Tree Int) Int (Node Int) > | (left == NULL) && (right == NULL) = Node left (f x) right -- Node > (Tree Int) String (Node Int) === Not OK > | (left /= NULL) = Node (fmap f left) x right > -- Node (Tree String) Int (Node Int) === Not OK > | (right /= NULL) = Node left x (fmap f > right) -- Node (Tree Int) Int (Node > String) === Not OK > > > > -------- Original Message -------- > Subject: [Haskell-beginners] Functor on Tree data constructor > From: Nishant > To: beginners at haskell.org > Date: 02.04.2014 12:53 > > > instance Functor Tree where > ............fmap f NULL = NULL > ............fmap f (Node left x right) | (left == NULL) && (right == > NULL) = Node left (f x) right > ................................................... | (left /= NULL) = > Node (fmap f left) x right > ................................................... | (right /= NULL) = > Node left x (fmap f right) > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Nishant -------------- next part -------------- An HTML attachment was scrubbed... URL: From gilbertomelfe at gmail.com Thu Apr 3 13:16:53 2014 From: gilbertomelfe at gmail.com (Gilberto Melfe) Date: Thu, 3 Apr 2014 14:16:53 +0100 Subject: [Haskell-beginners] Help installing Esqueleto (for Yesod)! In-Reply-To: References: Message-ID: Hi! Thank you for your response! The author of Esqueleto has updated his code and now I can install the package! However the Yesod-Platform still requires version 1.3.5 (although cabal, after a cabal update, informs that there is a newer version 1.3.9); hence I still can get Yesod installed! Is there some way around this problem? Mr. Michael Snoyman if you're reading this could you also update Yesod-Platform's dependencies? Thank You! Gilberto On Wed, Apr 2, 2014 at 5:00 PM, David McBride wrote: > Conduit was changed so that it no longer exports resourcet related types > and functions, and esqueleto is attempting to use those. It is a bug in > esqueleto, and I've registered a pull request on github here: > https://github.com/meteficha/esqueleto/pull/55 > > While you are waiting for that to be merged, you can pull from > https://github.com/mindreader/esqueleto.git. Just git clone it, cd into > the directory, then cabal install. > > > On Wed, Apr 2, 2014 at 10:56 AM, Gilberto Melfe wrote: > >> Hello community! >> >> I'm trying to install Yesod on a freshly setup openSUSE 13.1 virtual >> machine! >> >> I installed the Haskell Platform and three other (ghc) packages required >> (for yesod-bin), from the standard online repositories! >> >> Then i issued: >> cabal update >> cabal install cabal-install (because cabal told me so) >> export PATH=$HOME/.cabal/bin:$PATH >> >> cabal install yesod-platform >> >> After churning for a while, that last command stated that some packages, >> more precisely the Esqueleto package, failed to install. >> >> I tried to install it alone and got these error messages: >> >> -- Begin Command Output >> >> Resolving dependencies... >> Configuring esqueleto-1.3.5... >> Building esqueleto-1.3.5... >> Preprocessing library esqueleto-1.3.5... >> [1 of 4] Compiling Database.Esqueleto.Internal.PersistentImport ( >> src/Database/Esqueleto/Internal/PersistentImport.hs, >> dist/build/Database/Esqueleto/Internal/PersistentImport.o ) >> [2 of 4] Compiling Database.Esqueleto.Internal.Language ( >> src/Database/Esqueleto/Internal/Language.hs, >> dist/build/Database/Esqueleto/Internal/Language.o ) >> [3 of 4] Compiling Database.Esqueleto.Internal.Sql ( >> src/Database/Esqueleto/Internal/Sql.hs, >> dist/build/Database/Esqueleto/Internal/Sql.o ) >> >> src/Database/Esqueleto/Internal/Sql.hs:531:46: >> Not in scope: type constructor or class `C.ResourceT' >> >> src/Database/Esqueleto/Internal/Sql.hs:559:42: >> Not in scope: type constructor or class `C.ResourceT' >> >> src/Database/Esqueleto/Internal/Sql.hs:619:31: >> Not in scope: type constructor or class `C.ResourceT' >> >> src/Database/Esqueleto/Internal/Sql.hs:634:24: >> Not in scope: type constructor or class `C.ResourceT' >> >> src/Database/Esqueleto/Internal/Sql.hs:636:17: >> Not in scope: `C.runResourceT' >> Failed to install esqueleto-1.3.5 >> cabal: Error: some packages failed to install: >> esqueleto-1.3.5 failed during the building phase. The exception was: >> ExitFailure 1 >> >> -- End Command Output >> >> Can someone figure out what might be wrong? >> (I guess it's not a dependencies issue...) >> >> Thank You Very Much, in advance, for your time! >> >> Gilberto >> >> PS: Should I have included the command output in an attached file? Which >> "method" is preferred? >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at snoyman.com Thu Apr 3 13:21:01 2014 From: michael at snoyman.com (Michael Snoyman) Date: Thu, 3 Apr 2014 16:21:01 +0300 Subject: [Haskell-beginners] Help installing Esqueleto (for Yesod)! In-Reply-To: References: Message-ID: Yes, I've just updated yesod-platform. I'm not sure how that release made it through; I tested the install locally and it worked, but I must have had a tweaked version of esqueleto. In any event, 1.2.9.1 should work just fine (and please let me know if it doesn't). On Thu, Apr 3, 2014 at 4:16 PM, Gilberto Melfe wrote: > Hi! > > Thank you for your response! > > The author of Esqueleto has updated his code and now I can install the > package! > > However the Yesod-Platform still requires version 1.3.5 (although cabal, > after a cabal update, informs that there is a newer version 1.3.9); hence I > still can get Yesod installed! > > Is there some way around this problem? > > Mr. Michael Snoyman if you're reading this could you also update > Yesod-Platform's dependencies? > > Thank You! > > Gilberto > > On Wed, Apr 2, 2014 at 5:00 PM, David McBride wrote: > >> Conduit was changed so that it no longer exports resourcet related types >> and functions, and esqueleto is attempting to use those. It is a bug in >> esqueleto, and I've registered a pull request on github here: >> https://github.com/meteficha/esqueleto/pull/55 >> >> While you are waiting for that to be merged, you can pull from >> https://github.com/mindreader/esqueleto.git. Just git clone it, cd into >> the directory, then cabal install. >> >> >> On Wed, Apr 2, 2014 at 10:56 AM, Gilberto Melfe wrote: >> >>> Hello community! >>> >>> I'm trying to install Yesod on a freshly setup openSUSE 13.1 virtual >>> machine! >>> >>> I installed the Haskell Platform and three other (ghc) packages required >>> (for yesod-bin), from the standard online repositories! >>> >>> Then i issued: >>> cabal update >>> cabal install cabal-install (because cabal told me so) >>> export PATH=$HOME/.cabal/bin:$PATH >>> >>> cabal install yesod-platform >>> >>> After churning for a while, that last command stated that some packages, >>> more precisely the Esqueleto package, failed to install. >>> >>> I tried to install it alone and got these error messages: >>> >>> -- Begin Command Output >>> >>> Resolving dependencies... >>> Configuring esqueleto-1.3.5... >>> Building esqueleto-1.3.5... >>> Preprocessing library esqueleto-1.3.5... >>> [1 of 4] Compiling Database.Esqueleto.Internal.PersistentImport ( >>> src/Database/Esqueleto/Internal/PersistentImport.hs, >>> dist/build/Database/Esqueleto/Internal/PersistentImport.o ) >>> [2 of 4] Compiling Database.Esqueleto.Internal.Language ( >>> src/Database/Esqueleto/Internal/Language.hs, >>> dist/build/Database/Esqueleto/Internal/Language.o ) >>> [3 of 4] Compiling Database.Esqueleto.Internal.Sql ( >>> src/Database/Esqueleto/Internal/Sql.hs, >>> dist/build/Database/Esqueleto/Internal/Sql.o ) >>> >>> src/Database/Esqueleto/Internal/Sql.hs:531:46: >>> Not in scope: type constructor or class `C.ResourceT' >>> >>> src/Database/Esqueleto/Internal/Sql.hs:559:42: >>> Not in scope: type constructor or class `C.ResourceT' >>> >>> src/Database/Esqueleto/Internal/Sql.hs:619:31: >>> Not in scope: type constructor or class `C.ResourceT' >>> >>> src/Database/Esqueleto/Internal/Sql.hs:634:24: >>> Not in scope: type constructor or class `C.ResourceT' >>> >>> src/Database/Esqueleto/Internal/Sql.hs:636:17: >>> Not in scope: `C.runResourceT' >>> Failed to install esqueleto-1.3.5 >>> cabal: Error: some packages failed to install: >>> esqueleto-1.3.5 failed during the building phase. The exception was: >>> ExitFailure 1 >>> >>> -- End Command Output >>> >>> Can someone figure out what might be wrong? >>> (I guess it's not a dependencies issue...) >>> >>> Thank You Very Much, in advance, for your time! >>> >>> Gilberto >>> >>> PS: Should I have included the command output in an attached file? Which >>> "method" is preferred? >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Tue Apr 1 23:08:59 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 01 Apr 2014 18:08:59 -0500 Subject: [Haskell-beginners] How do you pronounce these operators? Message-ID: I'm having some difficult reading because I don't have "names" for many of the operators, such as <*>. I know that >>= is pronounced ?bind?, but what about the others? Is there some common consensus, a list somewhere, or at least the proper mathematical names to serve as a starting point? I'd hate to have to make something up (see < http://www.muppetlabs.com/~breadbox/intercal-man/tonsila.html >) ?John From ky3 at atamo.com Thu Apr 3 14:28:05 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Thu, 3 Apr 2014 21:28:05 +0700 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: Message-ID: On Wed, Apr 2, 2014 at 6:08 AM, John M. Dlugosz wrote: > I'm having some difficult reading because I don't have "names" for many of > the operators, such as <*>. I know that >>= is pronounced "bind", but what > about the others? Is there some common consensus, a list somewhere, or at > least the proper mathematical names to serve as a starting point? Did you try a search? There are links out there. But it's true that a search will only get you so far. I think what we don't have enough is idiomatic English coupled to idiomatic Haskell. It's like a proof/program of a proposition in Euclidean geometry. One just gets it visually and doesn't really bother with verbalizing on the tongue. Provide a code fragment here, and folks will help you with it. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From evohunz at gmail.com Thu Apr 3 15:36:25 2014 From: evohunz at gmail.com (Thiago Negri) Date: Thu, 3 Apr 2014 12:36:25 -0300 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: Message-ID: As far as I know, there are no standard out there for this. As Haskell allows to define custom operators, they tend to add up pretty quick and it's completely optional to give it a pronounceable name. Maybe we can propose a syntax to define a pronounceable name next to an operator, and make emacs say it out loud each time you type it (missed april's fool release notes). As Kim-Ee said, a quick search can bring good content into the matter: HaskellWiki has a page about this: http://www.haskell.org/haskellwiki/Pronunciation The HaskellWiki points to a thread: www.haskell.org/pipermail/haskell-cafe/2008-January/038756.html And there's a question at Stackoverflow: http://stackoverflow.com/questions/7746894/are-there-pronounceable-names-for-common-haskell-operators Hope it can help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davidleothomas at gmail.com Thu Apr 3 17:17:31 2014 From: davidleothomas at gmail.com (David Thomas) Date: Thu, 3 Apr 2014 10:17:31 -0700 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: Message-ID: For things that have a textual equivalent, I tend to use that (possibly "infix X" or "X operator" if it's not clear from context) even if the equivalent is more or less specialized than the operator. For <*> in particular, that is "ap". On Apr 3, 2014 6:29 AM, "John M. Dlugosz" wrote: > I'm having some difficult reading because I don't have "names" for many of > the operators, such as <*>. I know that >>= is pronounced "bind", but what > about the others? Is there some common consensus, a list somewhere, or at > least the proper mathematical names to serve as a starting point? > > I'd hate to have to make something up (see < http://www.muppetlabs.com/~ > breadbox/intercal-man/tonsila.html >) > > --John > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Thu Apr 3 20:58:18 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Thu, 3 Apr 2014 16:58:18 -0400 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: References: Message-ID: <20140403205818.GA4162@seas.upenn.edu> On Wed, Apr 02, 2014 at 09:52:21PM -0400, Jacek Dudek wrote: > -- As an exercise I wanted to define a datatype that is an alternating > list of elements of two different types. The best that I could do are > the type definitions below: > > module BiList > ( BiList (..) > , AList (EmptyA) > , BList (EmptyB) > ) where > > data BiList a b > = Empty > | BA (AList a b) > | BB (BList a b) > > data AList a b > = EmptyA > | AL a (BList a b) > > data BList a b > = EmptyB > | BL b (AList a b) > > (<#) :: a -> (BList a b) -> (AList a b) > a <# bs = AL a bs > > (<@) :: b -> (AList a b) -> (BList a b) > b <@ as = BL b as > > infixr 5 <# > infixr 5 <@ > > example :: BiList Int Char > example = BA $ 1 <# 'a' <@ 2 <# 'b' <@ 3 <# 'c' <@ 4 <# 'd' <@ EmptyA As David McBride noted, AList and BList are isomorphic (up to the order of their arguments) so you don't need both. Unfortunately you do still need BiList; but you don't need its Empty. So: data BiList a b = BA (AltList a b) | BB (AltList b a) data AltList a b = Empty | Cons a (AltList b a) So this addresses (2) but not (1). I don't think there is any way around the need for (1). (Note, however, that you do still have two distinct representations of the empty list: BA Empty and BB Empty. I can't see any way around that either.) -Brent From denis.kasak at gmail.com Thu Apr 3 21:43:59 2014 From: denis.kasak at gmail.com (Denis Kasak) Date: Thu, 3 Apr 2014 23:43:59 +0200 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: <20140403205818.GA4162@seas.upenn.edu> References: <20140403205818.GA4162@seas.upenn.edu> Message-ID: On 3 April 2014 22:58, Brent Yorgey wrote: > > data BiList a b > = BA (AltList a b) > | BB (AltList b a) > > data AltList a b > = Empty > | Cons a (AltList b a) > > So this addresses (2) but not (1). I don't think there is any way > around the need for (1). (Note, however, that you do still have two > distinct representations of the empty list: BA Empty and BB Empty. I > can't see any way around that either.) You could move the Empty constructor to BiList while making AltList a non-empty list, i.e. data BiList a b = Empty | BA (AltList a b) | BB (AltList b a) data AltList a b = Elem a | Cons a (AltList b a) -- Denis Kasak From tonymorris at gmail.com Fri Apr 4 01:32:20 2014 From: tonymorris at gmail.com (Tony Morris) Date: Fri, 04 Apr 2014 14:32:20 +1300 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: Message-ID: <533E0BA4.10506@gmail.com> On 04/04/14 03:28, Kim-Ee Yeoh wrote: > > On Wed, Apr 2, 2014 at 6:08 AM, John M. Dlugosz > > wrote: > > I'm having some difficult reading because I don't have "names" for > many of the operators, such as <*>. I know that >>= is pronounced > "bind", but what about the others? Is there some common > consensus, a list somewhere, or at least the proper mathematical > names to serve as a starting point? > > > Did you try a search? There are links out there. > > But it's true that a search will only get you so far. I think what we > don't have enough is idiomatic English coupled to idiomatic Haskell. > > It's like a proof/program of a proposition in Euclidean geometry. One > just gets it visually and doesn't really bother with verbalizing on > the tongue. > > Provide a code fragment here, and folks will help you with it. > > -- Kim-Ee > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners I have heard the following for (<*>) * angle butt * spaceship * apply * ap * angry eye -- Tony Morris http://tmorris.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From jzdudek at gmail.com Fri Apr 4 02:04:04 2014 From: jzdudek at gmail.com (Jacek Dudek) Date: Thu, 3 Apr 2014 22:04:04 -0400 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: References: <20140403205818.GA4162@seas.upenn.edu> Message-ID: Thanks David, that's really clever! I was trying to do it without any auxiliary data types, but couldn't see how I could use an instance of (BiList a b) in the constructor expression for (BiList a b) without losing the property that the list elements alternate from one type to the other with each new element. But now I see that when you write (BiList b a) in the constructor expression, that's written in the context provided by the (data BiList a b) line, so having the type variables in the opposite order makes all the difference. Brent, David's definition actually solved both (1) and (2), try it out! On 4/3/14, Denis Kasak wrote: > On 3 April 2014 22:58, Brent Yorgey wrote: >> >> data BiList a b >> = BA (AltList a b) >> | BB (AltList b a) >> >> data AltList a b >> = Empty >> | Cons a (AltList b a) >> >> So this addresses (2) but not (1). I don't think there is any way >> around the need for (1). (Note, however, that you do still have two >> distinct representations of the empty list: BA Empty and BB Empty. I >> can't see any way around that either.) > > You could move the Empty constructor to BiList while making AltList a > non-empty list, i.e. > > data BiList a b > = Empty > | BA (AltList a b) > | BB (AltList b a) > > data AltList a b > = Elem a > | Cons a (AltList b a) > > -- > Denis Kasak > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From joel.neely at gmail.com Fri Apr 4 12:14:28 2014 From: joel.neely at gmail.com (Joel Neely) Date: Fri, 4 Apr 2014 07:14:28 -0500 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: <533E0BA4.10506@gmail.com> References: <533E0BA4.10506@gmail.com> Message-ID: As somewhat of an outsider (background in Math, working in the software field), I find this topic a fascinating illustration of differences in optimization. Math (inclusive of statistics and theoretical physics) and computing (inclusive of IT and applications programming) seem to be two of the fields that have the greatest need for constant definition of new terms, frequently on-the-fly in support of a larger goal. (I'm excluding the legal, political, and marketing fields, for reasons best explained by Edwin Newman in _Strictly_Speaking_.) The mathematical approach optimizes for economy of writing/presentation, favoring single-letter, context-sensitive variable names and a typographer's paradise of symbols and alphabetic variations. Anecdotes abound of the specialist who finds papers from a different specialty to be cryptic. Computing, especially of the commercial variety, tends to optimize for recognition/hinting for the first-time or infrequent reader, with a bias toward semantic naming, metaphor, and the palette of the QWERTY-based keyboard. Anecdotes abound of the complex agglutination of naming patterns in enterprise-focused frameworks. I agree with the observation that standardizing some idiomatic, natural-language verbalizations would tend to help bridge the gap between those two cultures (hints toward C.P. Snow intended). To use examples from this thread, verbalizing <*> as "apply" seems to bridge those two optimizations, with "ap" betraying the mathematical style of compression over obviousness, and the rest ("spaceship", etc.) showing a hacker-style love of humor and inside-jokes that is gratifying to the insiders but off-putting to outsiders/novices. Each of the optimizations above is legitimate and valued by its community; insensitivity to those cultural issues will likely continue to reinforce the separation. The interesting question to me is whether that's what each community wants. On Thu, Apr 3, 2014 at 8:32 PM, Tony Morris wrote: > On 04/04/14 03:28, Kim-Ee Yeoh wrote: > > > On Wed, Apr 2, 2014 at 6:08 AM, John M. Dlugosz wrote: > >> I'm having some difficult reading because I don't have "names" for many >> of the operators, such as <*>. I know that >>= is pronounced "bind", but >> what about the others? Is there some common consensus, a list somewhere, >> or at least the proper mathematical names to serve as a starting point? > > > Did you try a search? There are links out there. > > But it's true that a search will only get you so far. I think what we > don't have enough is idiomatic English coupled to idiomatic Haskell. > > It's like a proof/program of a proposition in Euclidean geometry. One > just gets it visually and doesn't really bother with verbalizing on the > tongue. > > Provide a code fragment here, and folks will help you with it. > > -- Kim-Ee > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > I have heard the following for (<*>) > > * angle butt > * spaceship > * apply > * ap > * angry eye > > > -- > Tony Morrishttp://tmorris.net/ > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Beauty of style and harmony and grace and good rhythm depend on simplicity. - Plato -------------- next part -------------- An HTML attachment was scrubbed... URL: From exitconsole at gmail.com Fri Apr 4 12:40:26 2014 From: exitconsole at gmail.com (=?ISO-8859-1?B?ROFuaWVsIEFyYXTz?=) Date: Fri, 4 Apr 2014 14:40:26 +0200 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: <533E0BA4.10506@gmail.com> Message-ID: I call (>>) "proceed". Not canonical or anything. Daniel From tmorris at tmorris.net Fri Apr 4 13:24:38 2014 From: tmorris at tmorris.net (Tony Morris) Date: Sat, 5 Apr 2014 02:24:38 +1300 Subject: [Haskell-beginners] How do you pronounce these operators? In-Reply-To: References: <533E0BA4.10506@gmail.com> Message-ID: When instructing, I use the term "apply" for many reasons. I highly recommend this term. On Sat, Apr 5, 2014 at 1:40 AM, D?niel Arat? wrote: > I call (>>) "proceed". Not canonical or anything. > > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Fri Apr 4 16:19:20 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri, 4 Apr 2014 12:19:20 -0400 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: References: <20140403205818.GA4162@seas.upenn.edu> Message-ID: <20140404161920.GA21005@seas.upenn.edu> Ah, yes, having a nonempty AltList and moving Empty to BiList is a good idea. You still do need two different constructors BA and BB, but yes, overall this seems like a nice way to encode things. Exercise: write a conversion function fromList :: [Either a b] -> Maybe (BiList a b) which returns Just when the input list really is alternating, and Nothing otherwise. -Brent On Thu, Apr 03, 2014 at 10:04:04PM -0400, Jacek Dudek wrote: > Thanks David, that's really clever! I was trying to do it without any > auxiliary data types, but couldn't see how I could use an instance of > (BiList a b) in the constructor expression for (BiList a b) without > losing the property that the list elements alternate from one type to > the other with each new element. > > But now I see that when you write (BiList b a) in the constructor > expression, that's written in the context provided by the (data BiList > a b) line, so having the type variables in the opposite order makes > all the difference. > > Brent, David's definition actually solved both (1) and (2), try it out! > > On 4/3/14, Denis Kasak wrote: > > On 3 April 2014 22:58, Brent Yorgey wrote: > >> > >> data BiList a b > >> = BA (AltList a b) > >> | BB (AltList b a) > >> > >> data AltList a b > >> = Empty > >> | Cons a (AltList b a) > >> > >> So this addresses (2) but not (1). I don't think there is any way > >> around the need for (1). (Note, however, that you do still have two > >> distinct representations of the empty list: BA Empty and BB Empty. I > >> can't see any way around that either.) > > > > You could move the Empty constructor to BiList while making AltList a > > non-empty list, i.e. > > > > data BiList a b > > = Empty > > | BA (AltList a b) > > | BB (AltList b a) > > > > data AltList a b > > = Elem a > > | Cons a (AltList b a) > > > > -- > > Denis Kasak > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From defigueiredo at ucdavis.edu Fri Apr 4 18:17:48 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Fri, 04 Apr 2014 12:17:48 -0600 Subject: [Haskell-beginners] Definition of last: why complicate it? Message-ID: <533EF74C.1040003@ucdavis.edu> I was looking at the definition of last in the prelude and saw this code (from http://hackage.haskell.org/package/base-4.2.0.1/docs/src/GHC-List.html) -- | Extract the last element of a list, which must be finite and non-empty. last :: [a] -> a #ifdef USE_REPORT_PRELUDE last [x] = x last (_:xs) = last xs last [] = errorEmptyList "last" #else -- eliminate repeated cases last [] = errorEmptyList "last" last (x:xs) = last' x xs where last' y [] = y last' _ (y:ys) = last' y ys #endif Question: What does the second "eliminate repeated cases" definition of last gain compared to the first (simpler) one? Thanks Dimitri From rudy at matela.com.br Fri Apr 4 19:12:50 2014 From: rudy at matela.com.br (Rudy Matela) Date: Fri, 4 Apr 2014 20:12:50 +0100 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <533EF74C.1040003@ucdavis.edu> References: <533EF74C.1040003@ucdavis.edu> Message-ID: Hi, Basically the second one should be faster. I'm *guessing* here as *I'm no Haskell wizard*, so someone correct me if I'm wrong. In the first version, at each iteraction (applying to a non-empty list), the program will: 1. Check for a non-empty list, with one element -- return x if true *then* 2. Check for a non-empty list -- do something to the tail if true So basically, at each iteration you're doing two "check operations", and you know that the first operation will be true only for the last element which is wasteful. On a array with 10 elements you do roughly 19 "check operations" (2n - 1). In the second version: 0. Check for empty list error (you only do that once) because the recursion is on last' then, for each element: 1. check wether the passes list is empty (1 check) -- return y if true, apply recursion if false On a array with 10 elements you'll do roughly 11 "check operations" (n+1). According to a stackoverflow answer [1], this should be done automatically by GHC. Why it's still defined like that I don't know: maybe because the code is for when using other compilers, or maybe I misinterpreted the stackoverflow post and GHC is not able to do that. [1]: https://stackoverflow.com/a/12661937, under "Case Expressions" Regards, Rudy On Fri, Apr 4, 2014 at 7:17 PM, Dimitri DeFigueiredo wrote: > I was looking at the definition of last in the prelude and saw this code > > (from > http://hackage.haskell.org/package/base-4.2.0.1/docs/src/GHC-List.html) > > -- | Extract the last element of a list, which must be finite and non-empty. > last :: [a] -> a > #ifdef USE_REPORT_PRELUDE > last [x] = x > last (_:xs) = last xs > last [] = errorEmptyList "last" > #else > -- eliminate repeated cases > last [] = errorEmptyList "last" > last (x:xs) = last' x xs > where last' y [] = y > last' _ (y:ys) = last' y ys > #endif > > Question: What does the second "eliminate repeated cases" definition of last > gain compared to the first (simpler) one? > > Thanks > > Dimitri > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From rudy at matela.com.br Fri Apr 4 19:26:32 2014 From: rudy at matela.com.br (Rudy Matela) Date: Fri, 4 Apr 2014 20:26:32 +0100 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: References: <533EF74C.1040003@ucdavis.edu> Message-ID: You can test the speed in your ghci: -- pretty last plast :: [a] -> a plast [x] = x plast (_:xs) = plast xs plast [] = error "empty list!" -- ugly last ulast :: [a] -> a ulast [] = error "empty list!" ulast (x:xs) = ulast' x xs where ulast' y [] = y ulast' _ (y:ys) = ulast' y ys Not a big difference tough: ghci> :set +s ghci> ulast [1..20000000] 20000000 (2.59 secs, 2721962752 bytes) ghci> plast [1..20000000] 20000000 (2.70 secs, 2401902096 bytes) *Main> However, if you disable optimization (ghci -O0), the difference is more clear: ghci> ulast [1..20000000] 20000000 (2.56 secs, 2729847944 bytes) ghci> plast [1..20000000] 20000000 (3.30 secs, 2401899840 bytes) On Fri, Apr 4, 2014 at 8:12 PM, Rudy Matela wrote: > Hi, > > Basically the second one should be faster. I'm *guessing* here as > *I'm no Haskell wizard*, so someone correct me if I'm wrong. > > > In the first version, at each iteraction (applying to a non-empty > list), the program will: > > 1. Check for a non-empty list, with one element -- return x if true > *then* > 2. Check for a non-empty list -- do something to the tail if true > > So basically, at each iteration you're doing two "check operations", > and you know that the first operation will be true only for the last > element which is wasteful. On a array with 10 elements you do roughly > 19 "check operations" (2n - 1). > > > In the second version: > > 0. Check for empty list error (you only do that once) because the > recursion is on last' > then, for each element: > 1. check wether the passes list is empty (1 check) -- return y if > true, apply recursion if false > > On a array with 10 elements you'll do roughly 11 "check operations" (n+1). > > > According to a stackoverflow answer [1], this should be done > automatically by GHC. Why it's still defined like that I don't know: > maybe because the code is for when using other compilers, or maybe I > misinterpreted the stackoverflow post and GHC is not able to do that. > > > [1]: https://stackoverflow.com/a/12661937, under "Case Expressions" > > > Regards, > Rudy > > On Fri, Apr 4, 2014 at 7:17 PM, Dimitri DeFigueiredo > wrote: >> I was looking at the definition of last in the prelude and saw this code >> >> (from >> http://hackage.haskell.org/package/base-4.2.0.1/docs/src/GHC-List.html) >> >> -- | Extract the last element of a list, which must be finite and non-empty. >> last :: [a] -> a >> #ifdef USE_REPORT_PRELUDE >> last [x] = x >> last (_:xs) = last xs >> last [] = errorEmptyList "last" >> #else >> -- eliminate repeated cases >> last [] = errorEmptyList "last" >> last (x:xs) = last' x xs >> where last' y [] = y >> last' _ (y:ys) = last' y ys >> #endif >> >> Question: What does the second "eliminate repeated cases" definition of last >> gain compared to the first (simpler) one? >> >> Thanks >> >> Dimitri >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners From defigueiredo at ucdavis.edu Fri Apr 4 19:51:42 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Fri, 04 Apr 2014 13:51:42 -0600 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: References: <533EF74C.1040003@ucdavis.edu> Message-ID: <533F0D4E.3090901@ucdavis.edu> I think that in the second case (ulast' in your test) still performs 2 checks per iteration as it has to check for the empty list on its second argument at every recursive call (that's how it knows it's done). So, we are performing the same number of iterations. Maybe there's another reason for the "ugly" version to be preferred. Even if it is performance and not lazyness, I still don't understand what would make it faster. Thanks, Dimitri Em 04/04/14 13:26, Rudy Matela escreveu: > You can test the speed in your ghci: > > > -- pretty last > plast :: [a] -> a > plast [x] = x > plast (_:xs) = plast xs > plast [] = error "empty list!" > > -- ugly last > ulast :: [a] -> a > ulast [] = error "empty list!" > ulast (x:xs) = ulast' x xs > where ulast' y [] = y > ulast' _ (y:ys) = ulast' y ys > > > Not a big difference tough: > ghci> :set +s > ghci> ulast [1..20000000] > 20000000 > (2.59 secs, 2721962752 bytes) > ghci> plast [1..20000000] > 20000000 > (2.70 secs, 2401902096 bytes) > *Main> > > > However, if you disable optimization (ghci -O0), the difference is more clear: > ghci> ulast [1..20000000] > 20000000 > (2.56 secs, 2729847944 bytes) > ghci> plast [1..20000000] > 20000000 > (3.30 secs, 2401899840 bytes) > > > > On Fri, Apr 4, 2014 at 8:12 PM, Rudy Matela wrote: >> Hi, >> >> Basically the second one should be faster. I'm *guessing* here as >> *I'm no Haskell wizard*, so someone correct me if I'm wrong. >> >> >> In the first version, at each iteraction (applying to a non-empty >> list), the program will: >> >> 1. Check for a non-empty list, with one element -- return x if true >> *then* >> 2. Check for a non-empty list -- do something to the tail if true >> >> So basically, at each iteration you're doing two "check operations", >> and you know that the first operation will be true only for the last >> element which is wasteful. On a array with 10 elements you do roughly >> 19 "check operations" (2n - 1). >> >> >> In the second version: >> >> 0. Check for empty list error (you only do that once) because the >> recursion is on last' >> then, for each element: >> 1. check wether the passes list is empty (1 check) -- return y if >> true, apply recursion if false >> >> On a array with 10 elements you'll do roughly 11 "check operations" (n+1). >> >> >> According to a stackoverflow answer [1], this should be done >> automatically by GHC. Why it's still defined like that I don't know: >> maybe because the code is for when using other compilers, or maybe I >> misinterpreted the stackoverflow post and GHC is not able to do that. >> >> >> [1]: https://stackoverflow.com/a/12661937, under "Case Expressions" >> >> >> Regards, >> Rudy >> >> On Fri, Apr 4, 2014 at 7:17 PM, Dimitri DeFigueiredo >> wrote: >>> I was looking at the definition of last in the prelude and saw this code >>> >>> (from >>> http://hackage.haskell.org/package/base-4.2.0.1/docs/src/GHC-List.html) >>> >>> -- | Extract the last element of a list, which must be finite and non-empty. >>> last :: [a] -> a >>> #ifdef USE_REPORT_PRELUDE >>> last [x] = x >>> last (_:xs) = last xs >>> last [] = errorEmptyList "last" >>> #else >>> -- eliminate repeated cases >>> last [] = errorEmptyList "last" >>> last (x:xs) = last' x xs >>> where last' y [] = y >>> last' _ (y:ys) = last' y ys >>> #endif >>> >>> Question: What does the second "eliminate repeated cases" definition of last >>> gain compared to the first (simpler) one? >>> >>> Thanks >>> >>> Dimitri >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From daniel.is.fischer at googlemail.com Fri Apr 4 19:53:17 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Fri, 04 Apr 2014 21:53:17 +0200 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: References: <533EF74C.1040003@ucdavis.edu> Message-ID: <2398789.prq0aIc88x@linux-hpeb.site> On Friday 04 April 2014, 20:12:50, Rudy Matela wrote: > According to a stackoverflow answer [1], this should be done > automatically by GHC. Why it's still defined like that I don't know: > maybe because the code is for when using other compilers, or maybe I > misinterpreted the stackoverflow post and GHC is not able to do that. One can check that, just copy the source (hiding Prelude.last or renaming it) and compile, with -ddump-simpl to get the core GHC produces (it takes a little practice to read core, but it's sufficiently similar to Haskell to start understanding much of it quickly). With -O2, all GHC versions I tried (6.12.3, 7.0.2, 7.2.2, 7.4.2, 7.6.1, 7.6.3) produced (almost?) the more efficient version from the USE_REPORT_PRELUDE source, but with only -O, none did. If the libraries are guaranteed to be compiled with -O2 when building the compiler, there would be no need for the other source. But since that is not guaranteed, it's better to manually write the better version. Footnote (?): what GHC produces with -O2 is slightly different, it tests for a singleton list once in the wrapper before the worker is called, but it also creates a rule that when it is called on a list that is known at compile-time to be nonempty, the worker shall be called directly. Cheers, Daniel From daniel.is.fischer at googlemail.com Fri Apr 4 20:01:19 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Fri, 04 Apr 2014 22:01:19 +0200 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <533F0D4E.3090901@ucdavis.edu> References: <533EF74C.1040003@ucdavis.edu> <533F0D4E.3090901@ucdavis.edu> Message-ID: <2590905.871pVI0h1a@linux-hpeb.site> On Friday 04 April 2014, 13:51:42, Dimitri DeFigueiredo wrote: > I think that in the second case (ulast' in your test) still performs 2 > checks per iteration as it has to check for the empty list on its second > argument at every recursive call (that's how it knows it's done). So, we > are performing the same number of iterations. But in each iteration, there is only one case, versus the two in the plast version, we have ulast' y ys = case ys of [] -> y z:zs -> ulast' z zs > Maybe there's another > reason for the "ugly" version to be preferred. Even if it is > performance and not lazyness, I still don't understand what would make > it faster. > > Thanks, > > Dimitri > > Em 04/04/14 13:26, Rudy Matela escreveu: > > You can test the speed in your ghci: No. ghci is not a measure for efficiency of code. You need compiled (with optimisations) code to be able to judge and compare efficiency. Even when loading compiled and optimised code in ghci, ghci's runtime still behaves different from the runtime of the compiled code in binaries. To check the efficiency of code, ghc -O2 and the criterion library are your friends. Cheers, Daniel From tmorris at tmorris.net Fri Apr 4 20:19:27 2014 From: tmorris at tmorris.net (Tony Morris) Date: Sat, 5 Apr 2014 09:19:27 +1300 Subject: [Haskell-beginners] Data type definition for a list of elements of alternating types? In-Reply-To: References: Message-ID: See the separated package on hackage. On 03/04/2014 2:53 PM, "Jacek Dudek" wrote: > -- As an exercise I wanted to define a datatype that is an alternating > list of elements of two different types. The best that I could do are > the type definitions below: > > module BiList > ( BiList (..) > , AList (EmptyA) > , BList (EmptyB) > ) where > > data BiList a b > = Empty > | BA (AList a b) > | BB (BList a b) > > data AList a b > = EmptyA > | AL a (BList a b) > > data BList a b > = EmptyB > | BL b (AList a b) > > (<#) :: a -> (BList a b) -> (AList a b) > a <# bs = AL a bs > > (<@) :: b -> (AList a b) -> (BList a b) > b <@ as = BL b as > > infixr 5 <# > infixr 5 <@ > > example :: BiList Int Char > example = BA $ 1 <# 'a' <@ 2 <# 'b' <@ 3 <# 'c' <@ 4 <# 'd' <@ EmptyA > > -- There are two things that I don't like about this implementation. > > -- (1) The BA and BB constructors that must be applied on top of > instances of (AList a b) and (BList a b) to lift them to be of type > (BiList a b). > > -- (2) Having three different constructors for an empty list: Empty, > EmptyA, EmptyB, where ideally I would just have one. > > -- Is it possible to get around either of these annoyances with some > type theory gymnastics? Maybe something like the function fromIntegral > (the mechanics of which I don't really understand at this point)? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Fri Apr 4 20:25:25 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 04 Apr 2014 15:25:25 -0500 Subject: [Haskell-beginners] iterated function value sequence Message-ID: What's a good way to produce an iterated function value sequence? That is, f(x), f(f(x)), f(f(f(x))), etc. ref: https://www.youtube.com/watch?v=09JslnY7W_k From bob at redivi.com Fri Apr 4 20:35:50 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 4 Apr 2014 13:35:50 -0700 Subject: [Haskell-beginners] iterated function value sequence In-Reply-To: References: Message-ID: There's a function named iterate in Prelude: ?> :info iterate iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List' ?> take 8 (iterate (\x -> 2 * x + 1) 0) [0,1,3,7,15,31,63,127] On Fri, Apr 4, 2014 at 1:25 PM, John M. Dlugosz wrote: > What's a good way to produce an iterated function value sequence? That > is, f(x), f(f(x)), f(f(f(x))), etc. > > ref: https://www.youtube.com/watch?v=09JslnY7W_k > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Fri Apr 4 21:55:57 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Fri, 04 Apr 2014 15:55:57 -0600 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <2590905.871pVI0h1a@linux-hpeb.site> References: <533EF74C.1040003@ucdavis.edu> <533F0D4E.3090901@ucdavis.edu> <2590905.871pVI0h1a@linux-hpeb.site> Message-ID: <533F2A6D.6010904@ucdavis.edu> Hi Daniel, I really like your translation of ulast' below ulast' y ys = case ys of [] -> y z:zs -> ulast' z zs This makes it really clear there is only one comparison happening. I tried looking at core with the -O option (ghc 7.6.3), but am having a had time making sense of it. For comparison, could you also translate the inefficient version of plast into the case notation you use above? Thanks, Dimitri ==================== Tidy Core ==================== Result size of Tidy Core = {terms: 49, types: 58, coercions: 0} lvl_rgS :: [GHC.Types.Char] [GblId] lvl_rgS = GHC.CString.unpackCString# "empty list!" Test.ulast2 :: forall a_b. a_b [GblId, Str=DmdType b] Test.ulast2 = \ (@ a_b) -> GHC.Err.error @ a_b lvl_rgS Rec { Test.ulast1 [Occ=LoopBreaker] :: forall a_b. a_b -> [a_b] -> a_b [GblId, Arity=2, Caf=NoCafRefs, Str=DmdType LS] Test.ulast1 = \ (@ a_b) (y_aeQ :: a_b) (ds_df8 :: [a_b]) -> case ds_df8 of _ { [] -> y_aeQ; : y1_aeR ys_aeS -> Test.ulast1 @ a_b y1_aeR ys_aeS } end Rec } Test.ulast :: forall a_aeH. [a_aeH] -> a_aeH [GblId, Arity=1, Str=DmdType S, Unf=Unf{Src=, TopLvl=True, Arity=1, Value=True, ConLike=True, WorkFree=True, Expandable=True, Guidance=IF_ARGS [30] 50 0}] Test.ulast = \ (@ a_b) (ds_df6 :: [a_b]) -> case ds_df6 of _ { [] -> Test.ulast2 @ a_b; : x_aeN xs_aeO -> Test.ulast1 @ a_b x_aeN xs_aeO } lvl1_rgT :: forall a_d. a_d [GblId, Str=DmdType b] lvl1_rgT = \ (@ a_d) -> GHC.Err.error @ a_d lvl_rgS Rec { Test.plast [Occ=LoopBreaker] :: forall a_aeI. [a_aeI] -> a_aeI [GblId, Arity=1, Str=DmdType S] Test.plast = \ (@ a_d) (ds_dfc :: [a_d]) -> case ds_dfc of _ { [] -> lvl1_rgT @ a_d; : x_aeJ ds1_dfd -> case ds1_dfd of wild1_X8 { [] -> x_aeJ; : ipv_sfz ipv1_sfA -> Test.plast @ a_d wild1_X8 } } end Rec } ====== From allbery.b at gmail.com Fri Apr 4 22:51:11 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Fri, 4 Apr 2014 18:51:11 -0400 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <2398789.prq0aIc88x@linux-hpeb.site> References: <533EF74C.1040003@ucdavis.edu> <2398789.prq0aIc88x@linux-hpeb.site> Message-ID: On Fri, Apr 4, 2014 at 3:53 PM, Daniel Fischer < daniel.is.fischer at googlemail.com> wrote: > With -O2, all GHC versions I tried (6.12.3, 7.0.2, 7.2.2, 7.4.2, 7.6.1, > 7.6.3) > produced (almost?) the more efficient version from the USE_REPORT_PRELUDE > source, but with only -O, none did. > Also worth noting is that ghci does not not have an optimizer; this might well matter with large lists. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.is.fischer at googlemail.com Fri Apr 4 23:23:41 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Sat, 05 Apr 2014 01:23:41 +0200 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <533F2A6D.6010904@ucdavis.edu> References: <533EF74C.1040003@ucdavis.edu> <2590905.871pVI0h1a@linux-hpeb.site> <533F2A6D.6010904@ucdavis.edu> Message-ID: <3839543.xC6vtBB0qU@linux-hpeb.site> On Friday 04 April 2014, 15:55:57, Dimitri DeFigueiredo wrote: > Hi Daniel, > > I really like your translation of ulast' below > > ulast' y ys = case ys of > [] -> y > z:zs -> ulast' z zs > > This makes it really clear there is only one comparison happening. I > tried looking > at core with the -O option (ghc 7.6.3), but am having a had time making > sense of it. I'll add explanations between the core below. > > For comparison, could you also translate the inefficient version of plast > into the case notation you use above? GHC already did that, I'll put it next to the core at the bottom. > > > Thanks, > > Dimitri > > ==================== Tidy Core ==================== > Result size of Tidy Core = {terms: 49, types: 58, coercions: 0} > > lvl_rgS :: [GHC.Types.Char] > [GblId] > lvl_rgS = GHC.CString.unpackCString# "empty list!" > > Test.ulast2 :: forall a_b. a_b > [GblId, Str=DmdType b] > Test.ulast2 = \ (@ a_b) -> GHC.Err.error @ a_b lvl_rgS All of the above is uninteresting, apart from some stats at the header line, we have the error call in case the function is called with an empty list. What follows is the code for the worker ulast', named ulast1 in the core. > > Rec { > Test.ulast1 [Occ=LoopBreaker] :: forall a_b. a_b -> [a_b] -> a_b ulast1is a loop-breaker, it cannot be inlined (it is recursive, thus it cannot be inlined anyway), but that need not interest at the moment. Its type is a -> [a] -> a but GHC added a suffix to get unique identifiers. You can suppress that with the flag -dsuppress-uniques > [GblId, Arity=2, Caf=NoCafRefs, Str=DmdType LS] It takes two arguments, doesn't refer to any CAFs, and is lazy in the first argument, strict in the second. > Test.ulast1 = > \ (@ a_b) (y_aeQ :: a_b) (ds_df8 :: [a_b]) -> The arguments. First is the type at which it is called, then come - the last list element we have found so far, y_aeQ, and - the remaining part of the list, ds_df8. > case ds_df8 of _ { The list argument is evaluated (to weak head normal form), but it is never referred to later as a whole, hence it's not bound to an identifier, so we have a wild-card `_` after the `case ... of` > [] -> y_aeQ; Totally Haskell, if the remaining part of the list is empty, return the last element, otherwise > : y1_aeR ys_aeS -> Test.ulast1 @ a_b y1_aeR ys_aeS remember the next list element and recur. In core, we have unparenthesised prefix application also of infix operators, so the match that in Haskell looks y1_aeR : ys_aeS looks a bit different in core, but is recognisable. > } > end Rec } Now comes the code for the wrapper. That's not recursive, hence we don't have the "Rec" annotations, and it's not a loop-breaker, it can be inlined. The type signature is clear, with an explicit forall. > Test.ulast :: forall a_aeH. [a_aeH] -> a_aeH > [GblId, > Arity=1, > Str=DmdType S, It takes one argument and is strict in that argument, next comes unfolding info, in case it shall be inlined elsewhere, that doesn't need to interest us now. > Unf=Unf{Src=, TopLvl=True, Arity=1, Value=True, > ConLike=True, WorkFree=True, Expandable=True, > Guidance=IF_ARGS [30] 50 0}] > Test.ulast = > \ (@ a_b) (ds_df6 :: [a_b]) -> The type at which ulast is called, and the argument it is passed > case ds_df6 of _ { The list is evaluated (to WHNF), > [] -> Test.ulast2 @ a_b; If the list is empty, call error > > : x_aeN xs_aeO -> Test.ulast1 @ a_b x_aeN xs_aeO Otherwise call the worker > } > > lvl1_rgT :: forall a_d. a_d > [GblId, Str=DmdType b] > lvl1_rgT = \ (@ a_d) -> GHC.Err.error @ a_d lvl_rgS Another error call, this time to be called from plast > > Rec { > Test.plast [Occ=LoopBreaker] :: forall a_aeI. [a_aeI] -> a_aeI plast is recursive, a loop-breaker (no inlining possible), and its type > [GblId, Arity=1, Str=DmdType S] it takes one argument, and is strict in it > Test.plast = > \ (@ a_d) (ds_dfc :: [a_d]) -> > case ds_dfc of _ { The list is evaluated > [] -> lvl1_rgT @ a_d; If it is empty, call error, > > : x_aeJ ds1_dfd -> otherwise, bind the first element and the tail to names > > case ds1_dfd of wild1_X8 { and evaluate the tail. The tail may be referenced later, hence the evaluated tail is bound to a name - wild1_X8. > [] -> x_aeJ; If the tail is empty, return the first (only) element of plast's argument > : ipv_sfz ipv1_sfA -> Test.plast @ a_d wild1_X8 otherwise recur on the tail. > } > } > end Rec } The Haskell case-construct corresponding to the core is plast :: [a] -> a plast xs = case xs of [] -> error "empty list!" y:ys -> case ys of [] -> y z:zs -> plast ys From ky3 at atamo.com Sat Apr 5 03:19:03 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sat, 5 Apr 2014 10:19:03 +0700 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <533F2A6D.6010904@ucdavis.edu> References: <533EF74C.1040003@ucdavis.edu> <533F0D4E.3090901@ucdavis.edu> <2590905.871pVI0h1a@linux-hpeb.site> <533F2A6D.6010904@ucdavis.edu> Message-ID: On Sat, Apr 5, 2014 at 4:55 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > For comparison, could you also translate the inefficient version of plast > into the case notation you use above? > You probably already know this already, but it's worth underscoring for everyone that it's not the translation into case notation /per se/ that gives you the incremental performance improvement. A function definition by parts is syntactic sugar for a single case expression. All syntactic sugar, including others like do-notation, where clauses, etc., gets eliminated at the earliest stages when code is handed off to ghc. I encourage newcomers to familiarize themselves with desugaring to avoid unpleasant surprises. The sugared syntax often suggests mystery and magic when there's really nothing of that sort at all. Coming back to the topic of function definition by parts, aka piecewise definition, it appears that at least one reputable school thinks it's "weird" and "confusing" enough to be warrant a ban in homework: http://www.reddit.com/r/haskell/comments/223fi4/need_help/cgiywxi -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Sat Apr 5 15:13:00 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 05 Apr 2014 10:13:00 -0500 Subject: [Haskell-beginners] What does "(# #)" mean? Message-ID: The sources I've learned about thus far are not helping me with this one. What's "(# #)"? Haskell is search-hostile. From poczta at emanuelkoczwara.pl Sat Apr 5 15:20:27 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Sat, 05 Apr 2014 17:20:27 +0200 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: Message-ID: <1396711227.10143.2.camel@emanuel-laptop> Hi, Dnia 2014-04-05, sob o godzinie 10:13 -0500, John M. Dlugosz pisze: > The sources I've learned about thus far are not helping me with this one. > What's "(# #)"? > Haskell is search-hostile. > Can you provide some context? It looks like ordinary string "(# #)" :: String or "(# #)" :: [Char] Emanuel From allbery.b at gmail.com Sat Apr 5 15:26:12 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Sat, 5 Apr 2014 11:26:12 -0400 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: Message-ID: On Sat, Apr 5, 2014 at 11:13 AM, John M. Dlugosz wrote: > The sources I've learned about thus far are not helping me with this one. > What's "(# #)"? > Haskell is search-hostile. > http://symbolhound.com :p It's an unboxed tuple, a type that is used internally (anything named with a # is GHC internals). You can't work with it normally (you need an extension to use the MagicHash), and it has a number of restrictions since most things require a constructor but an unboxed tuple doesn't have one. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Sat Apr 5 15:28:00 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 05 Apr 2014 10:28:00 -0500 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: Message-ID: On 4/5/2014 10:26 AM, Brandon Allbery wrote: > On Sat, Apr 5, 2014 at 11:13 AM, John M. Dlugosz > wrote: > > The sources I've learned about thus far are not helping me with this one. > What's "(# #)"? > Haskell is search-hostile. > > > http://symbolhound.com :p Hmm, I tried symbolhound, and only found pseudocode and parenthetical remarks that happened to begin with a #, or bullet points. > > It's an unboxed tuple, a type that is used internally (anything named with a # is GHC > internals). You can't work with it normally (you need an extension to use the MagicHash), > and it has a number of restrictions since most things require a constructor but an unboxed > tuple doesn't have one. > > -- > brandon s allbery kf8nh sine nomine associates > allbery.b at gmail.com ballbery at sinenomine.net > > unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > > From cma at bitemyapp.com Sat Apr 5 15:31:37 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Sat, 5 Apr 2014 10:31:37 -0500 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: <1396711227.10143.2.camel@emanuel-laptop> References: <1396711227.10143.2.camel@emanuel-laptop> Message-ID: The user meant unboxed tuples, not a literal string. Try not to give the questioner the benefit of the doubt and search for a better answer than, "there's quotes, it must be a string rather than them trying to contain the syntax they want to express!" Anyway, Haskell isn't search hostile, Google is Haskell hostile. You can use Symbolhound to search for symbols in web pages. Search I used to get your answer: http://symbolhound.com/?q=tuple+%28%23+%23%29+Haskell What is most likely your answer: http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html#unboxed-tuples Further reading: Kinds, reference types, and value types in Haskell. Also, Alan Watts. Double is a reference type of Kind * Double# is an unboxed value type of Kind # Types in Haskell default to *. This is what you want usually. Value types are an after-the-fact optimization that shouldn't be done without the guidance of a well-designed benchmark suite. But yeah, not a string. --- Chris Allen On Sat, Apr 5, 2014 at 10:20 AM, Emanuel Koczwara wrote: > Hi, > > Dnia 2014-04-05, sob o godzinie 10:13 -0500, John M. Dlugosz pisze: > > The sources I've learned about thus far are not helping me with this one. > > What's "(# #)"? > > Haskell is search-hostile. > > > > Can you provide some context? It looks like ordinary string > > "(# #)" :: String > > or > > "(# #)" :: [Char] > > Emanuel > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Sat Apr 5 15:29:27 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 05 Apr 2014 10:29:27 -0500 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: <1396711227.10143.2.camel@emanuel-laptop> References: <1396711227.10143.2.camel@emanuel-laptop> Message-ID: The quotes are in my message, not part of the thing being quoted. E.g. 'What does "+" mean between two numbers?' On 4/5/2014 10:20 AM, Emanuel Koczwara wrote: > Hi, > > Dnia 2014-04-05, sob o godzinie 10:13 -0500, John M. Dlugosz pisze: >> The sources I've learned about thus far are not helping me with this one. >> What's "(# #)"? >> Haskell is search-hostile. >> > > Can you provide some context? It looks like ordinary string > > "(# #)" :: String > > or > > "(# #)" :: [Char] > > Emanuel > > From ngnr63q02 at sneakemail.com Sat Apr 5 15:32:42 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 05 Apr 2014 10:32:42 -0500 Subject: [Haskell-beginners] iterated function value sequence In-Reply-To: References: Message-ID: On 4/4/2014 3:35 PM, Bob Ippolito wrote: > There's a function named iterate in Prelude: > > ?> :info iterate > iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List' > ?> take 8 (iterate (\x -> 2 * x + 1) 0) > [0,1,3,7,15,31,63,127] > Nice. I followed up with that to understand how it's written. Now, how might I do something like that but "forget" previous values to free up memory? ?John From bob at redivi.com Sat Apr 5 15:55:01 2014 From: bob at redivi.com (Bob Ippolito) Date: Sat, 5 Apr 2014 08:55:01 -0700 Subject: [Haskell-beginners] iterated function value sequence In-Reply-To: References: Message-ID: On Saturday, April 5, 2014, John M. Dlugosz wrote: > On 4/4/2014 3:35 PM, Bob Ippolito wrote: > >> There's a function named iterate in Prelude: >> >> ?> :info iterate >> iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List' >> ?> take 8 (iterate (\x -> 2 * x + 1) 0) >> [0,1,3,7,15,31,63,127] >> >> Nice. I followed up with that to understand how it's written. > > Now, how might I do something like that but "forget" previous values to > free up memory? > Garbage Collection does that for you. If all the references are gone, the memory can be freed. If you have a specific use in mind I can show you what that looks like. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Sat Apr 5 16:14:13 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sat, 5 Apr 2014 23:14:13 +0700 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: <1396711227.10143.2.camel@emanuel-laptop> Message-ID: On Sat, Apr 5, 2014 at 10:31 PM, Christopher Allen wrote: > The user meant unboxed tuples, not a literal string. Try not to give the > questioner the benefit of the doubt and search for a better answer than, > "there's quotes, it must be a string rather than them trying to contain the > syntax they want to express!" Hey, we're all here for the learning, and I for one am always discombobulated on discovering how much English I have left to learn. I think Emmanuel was perplexed and understood the question as best as he could. John, on the other hand, could probably have followed the question about the curious brackets by supplying context e.g. "And I found it here like this (# Int, Int #)." And everyone, (me especially!), please use the mailing list wisely to maintain good vibes. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Sat Apr 5 17:11:43 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Sat, 05 Apr 2014 11:11:43 -0600 Subject: [Haskell-beginners] Definition of last: why complicate it? In-Reply-To: <3839543.xC6vtBB0qU@linux-hpeb.site> References: <533EF74C.1040003@ucdavis.edu> <2590905.871pVI0h1a@linux-hpeb.site> <533F2A6D.6010904@ucdavis.edu> <3839543.xC6vtBB0qU@linux-hpeb.site> Message-ID: <5340394F.7080403@ucdavis.edu> Hi Daniel, Thanks so much for the detailed explanations! I got it now. :-D Dimitri Em 04/04/14 17:23, Daniel Fischer escreveu: > On Friday 04 April 2014, 15:55:57, Dimitri DeFigueiredo wrote: >> Hi Daniel, >> >> I really like your translation of ulast' below >> >> ulast' y ys = case ys of >> [] -> y >> z:zs -> ulast' z zs >> >> This makes it really clear there is only one comparison happening. I >> tried looking >> at core with the -O option (ghc 7.6.3), but am having a had time making >> sense of it. > I'll add explanations between the core below. > >> For comparison, could you also translate the inefficient version of plast >> into the case notation you use above? > GHC already did that, I'll put it next to the core at the bottom. > >> >> Thanks, >> >> Dimitri >> >> ==================== Tidy Core ==================== >> Result size of Tidy Core = {terms: 49, types: 58, coercions: 0} >> >> lvl_rgS :: [GHC.Types.Char] >> [GblId] >> lvl_rgS = GHC.CString.unpackCString# "empty list!" >> >> Test.ulast2 :: forall a_b. a_b >> [GblId, Str=DmdType b] >> Test.ulast2 = \ (@ a_b) -> GHC.Err.error @ a_b lvl_rgS > All of the above is uninteresting, apart from some stats at the header line, > we have the error call in case the function is called with an empty list. > > What follows is the code for the worker ulast', named ulast1 in the core. > >> Rec { >> Test.ulast1 [Occ=LoopBreaker] :: forall a_b. a_b -> [a_b] -> a_b > ulast1is a loop-breaker, it cannot be inlined (it is recursive, thus it cannot > be inlined anyway), but that need not interest at the moment. Its type is > > a -> [a] -> a > > but GHC added a suffix to get unique identifiers. You can suppress that with > the flag -dsuppress-uniques > >> [GblId, Arity=2, Caf=NoCafRefs, Str=DmdType LS] > It takes two arguments, doesn't refer to any CAFs, and is lazy in the first > argument, strict in the second. > >> Test.ulast1 = >> \ (@ a_b) (y_aeQ :: a_b) (ds_df8 :: [a_b]) -> > The arguments. First is the type at which it is called, then come > - the last list element we have found so far, y_aeQ, and > - the remaining part of the list, ds_df8. > >> case ds_df8 of _ { > The list argument is evaluated (to weak head normal form), but it is never > referred to later as a whole, hence it's not bound to an identifier, so we > have a wild-card `_` after the `case ... of` > >> [] -> y_aeQ; > Totally Haskell, if the remaining part of the list is empty, return the last > element, otherwise > >> : y1_aeR ys_aeS -> Test.ulast1 @ a_b y1_aeR ys_aeS > remember the next list element and recur. In core, we have unparenthesised > prefix application also of infix operators, so the match that in Haskell looks > > y1_aeR : ys_aeS > > looks a bit different in core, but is recognisable. > >> } >> end Rec } > Now comes the code for the wrapper. That's not recursive, hence we don't have > the "Rec" annotations, and it's not a loop-breaker, it can be inlined. The > type signature is clear, with an explicit forall. > >> Test.ulast :: forall a_aeH. [a_aeH] -> a_aeH >> [GblId, >> Arity=1, >> Str=DmdType S, > It takes one argument and is strict in that argument, next comes unfolding > info, in case it shall be inlined elsewhere, that doesn't need to interest us > now. > >> Unf=Unf{Src=, TopLvl=True, Arity=1, Value=True, >> ConLike=True, WorkFree=True, Expandable=True, >> Guidance=IF_ARGS [30] 50 0}] >> Test.ulast = >> \ (@ a_b) (ds_df6 :: [a_b]) -> > The type at which ulast is called, and the argument it is passed > >> case ds_df6 of _ { > The list is evaluated (to WHNF), > >> [] -> Test.ulast2 @ a_b; > If the list is empty, call error > >> : x_aeN xs_aeO -> Test.ulast1 @ a_b x_aeN xs_aeO > Otherwise call the worker > >> } >> >> lvl1_rgT :: forall a_d. a_d >> [GblId, Str=DmdType b] >> lvl1_rgT = \ (@ a_d) -> GHC.Err.error @ a_d lvl_rgS > Another error call, this time to be called from plast > >> Rec { >> Test.plast [Occ=LoopBreaker] :: forall a_aeI. [a_aeI] -> a_aeI > plast is recursive, a loop-breaker (no inlining possible), and its type > >> [GblId, Arity=1, Str=DmdType S] > it takes one argument, and is strict in it > >> Test.plast = >> \ (@ a_d) (ds_dfc :: [a_d]) -> >> case ds_dfc of _ { > The list is evaluated > >> [] -> lvl1_rgT @ a_d; > If it is empty, call error, > >> : x_aeJ ds1_dfd -> > otherwise, bind the first element and the tail to names > >> case ds1_dfd of wild1_X8 { > and evaluate the tail. The tail may be referenced later, hence the evaluated > tail is bound to a name - wild1_X8. > >> [] -> x_aeJ; > If the tail is empty, return the first (only) element of plast's argument > >> : ipv_sfz ipv1_sfA -> Test.plast @ a_d wild1_X8 > otherwise recur on the tail. > >> } >> } >> end Rec } > The Haskell case-construct corresponding to the core is > > plast :: [a] -> a > plast xs = case xs of > [] -> error "empty list!" > y:ys -> case ys of > [] -> y > z:zs -> plast ys > > From shaegis at gmail.com Sun Apr 6 00:13:29 2014 From: shaegis at gmail.com (S. H. Aegis) Date: Sun, 6 Apr 2014 09:13:29 +0900 Subject: [Haskell-beginners] How can I fix this 'where' error ? Message-ID: Hi. I'm new to Haskell. I try to solve Euler Project for studying Haskell through Googling, reading books. There is a error in where sentence, but I can't fix this. maxEleList :: String -> String -> [Int] maxEleList xs ys = maxOfTwo upperList lowerList where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) Error message is ~/maxPathSum.hs: line 4, column 75: Parse error Error message: Parse error: = Code: maxEleList xs ys = maxOfTwo upperList lowerList where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) > lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) How can I fix this? Thank you for your kind help. S. Chang ---------------- This code is for Euler project: problem 18, and whole code is like this... (Not yet finished...) maxEleList :: String -> String -> [Int] maxEleList xs ys = maxOfTwo upperList lowerList where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) maxOfTwo :: [Int] -> [Int] -> [Int] maxOfTwo [] [] = [] maxOfTwo (x:xs) (y:ys) = max x y : maxOfTwo xs ys turpleSum :: (Int, Int) -> Int turpleSum (x, y) = x + y toIntList :: String -> [Int] toIntList = map (\x -> read x :: Int) . words rowData :: [[Int]] --rowData = reverse $ map (map (\x -> read x :: Int) . words) [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15] rowData = map (map (\x -> read x :: Int) . words) [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15] r1 = "75" r2 = "95 64" r3 = "17 47 82" r4 = "18 35 87 10" r5 = "20 04 82 47 65" r6 = "19 01 23 75 03 34" r7 = "88 02 77 73 07 63 67" r8 = "99 65 04 28 06 16 70 92" r9 = "41 41 26 56 83 40 80 70 33" r10 = "41 48 72 33 47 32 37 16 94 29" r11 = "53 71 44 65 25 43 91 52 97 51 14" r12 = "70 11 33 28 77 73 17 78 39 68 17 57" r13 = "91 71 52 38 17 14 91 43 58 50 27 29 48" r14 = "63 66 04 68 89 53 67 30 73 16 69 87 40 31" r15 = "04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Sun Apr 6 00:18:14 2014 From: bob at redivi.com (Bob Ippolito) Date: Sat, 5 Apr 2014 17:18:14 -0700 Subject: [Haskell-beginners] How can I fix this 'where' error ? In-Reply-To: References: Message-ID: The problem is that you are mixing tabs and spaces. It doesn't realize that the two lines are supposed to be at the same level of indentation. It's best to always use spaces, never tabs. This might be easier to avoid if you put where on its own line, but the best solution is to configure your text editor to always expand tabs to spaces. On Sat, Apr 5, 2014 at 5:13 PM, S. H. Aegis wrote: > Hi. > I'm new to Haskell. I try to solve Euler Project for studying Haskell > through Googling, reading books. > There is a error in where sentence, but I can't fix this. > > maxEleList :: String -> String -> [Int] > maxEleList xs ys = maxOfTwo upperList lowerList > where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) > lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) > > Error message is > ~/maxPathSum.hs: line 4, column 75: > Parse error > Error message: > Parse error: = > Code: > maxEleList xs ys = maxOfTwo upperList lowerList > where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) > > lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) > > > How can I fix this? > Thank you for your kind help. > > S. Chang > > > ---------------- > This code is for Euler project: problem 18, and whole code is like this... > (Not yet finished...) > > maxEleList :: String -> String -> [Int] > maxEleList xs ys = maxOfTwo upperList lowerList > where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) > lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) > maxOfTwo :: [Int] -> [Int] -> [Int] > maxOfTwo [] [] = [] > maxOfTwo (x:xs) (y:ys) = max x y : maxOfTwo xs ys > > turpleSum :: (Int, Int) -> Int > turpleSum (x, y) = x + y > > toIntList :: String -> [Int] > toIntList = map (\x -> read x :: Int) . words > > rowData :: [[Int]] > --rowData = reverse $ map (map (\x -> read x :: Int) . words) [r1, r2, r3, > r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15] > rowData = map (map (\x -> read x :: Int) . words) [r1, r2, r3, r4, r5, r6, > r7, r8, r9, r10, r11, r12, r13, r14, r15] > > r1 = "75" > r2 = "95 64" > r3 = "17 47 82" > r4 = "18 35 87 10" > r5 = "20 04 82 47 65" > r6 = "19 01 23 75 03 34" > r7 = "88 02 77 73 07 63 67" > r8 = "99 65 04 28 06 16 70 92" > r9 = "41 41 26 56 83 40 80 70 33" > r10 = "41 48 72 33 47 32 37 16 94 29" > r11 = "53 71 44 65 25 43 91 52 97 51 14" > r12 = "70 11 33 28 77 73 17 78 39 68 17 57" > r13 = "91 71 52 38 17 14 91 43 58 50 27 29 48" > r14 = "63 66 04 68 89 53 67 30 73 16 69 87 40 31" > r15 = "04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Apr 6 00:38:07 2014 From: shaegis at gmail.com (S. H. Aegis) Date: Sun, 6 Apr 2014 09:38:07 +0900 Subject: [Haskell-beginners] How can I fix this 'where' error ? In-Reply-To: References: Message-ID: It's work ! Thank you so much ! I change my editor settings as you said. Thank you again, and Have a nice day. S. Chang 2014-04-06 9:18 GMT+09:00 Bob Ippolito : > The problem is that you are mixing tabs and spaces. It doesn't realize > that the two lines are supposed to be at the same level of indentation. > It's best to always use spaces, never tabs. > > This might be easier to avoid if you put where on its own line, but the > best solution is to configure your text editor to always expand tabs to > spaces. > > > On Sat, Apr 5, 2014 at 5:13 PM, S. H. Aegis wrote: > >> Hi. >> I'm new to Haskell. I try to solve Euler Project for studying Haskell >> through Googling, reading books. >> There is a error in where sentence, but I can't fix this. >> >> maxEleList :: String -> String -> [Int] >> maxEleList xs ys = maxOfTwo upperList lowerList >> where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) >> lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) >> >> Error message is >> ~/maxPathSum.hs: line 4, column 75: >> Parse error >> Error message: >> Parse error: = >> Code: >> maxEleList xs ys = maxOfTwo upperList lowerList >> where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) >> > lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList >> ys) >> >> >> How can I fix this? >> Thank you for your kind help. >> >> S. Chang >> >> >> ---------------- >> This code is for Euler project: problem 18, and whole code is like >> this... (Not yet finished...) >> >> maxEleList :: String -> String -> [Int] >> maxEleList xs ys = maxOfTwo upperList lowerList >> where upperList = map turpleSum $ zip (toIntList xs) (toIntList ys) >> lowerList = map turpleSum $ zip (toIntList xs) (tail $ toIntList ys) >> maxOfTwo :: [Int] -> [Int] -> [Int] >> maxOfTwo [] [] = [] >> maxOfTwo (x:xs) (y:ys) = max x y : maxOfTwo xs ys >> >> turpleSum :: (Int, Int) -> Int >> turpleSum (x, y) = x + y >> >> toIntList :: String -> [Int] >> toIntList = map (\x -> read x :: Int) . words >> >> rowData :: [[Int]] >> --rowData = reverse $ map (map (\x -> read x :: Int) . words) [r1, r2, >> r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15] >> rowData = map (map (\x -> read x :: Int) . words) [r1, r2, r3, r4, r5, >> r6, r7, r8, r9, r10, r11, r12, r13, r14, r15] >> >> r1 = "75" >> r2 = "95 64" >> r3 = "17 47 82" >> r4 = "18 35 87 10" >> r5 = "20 04 82 47 65" >> r6 = "19 01 23 75 03 34" >> r7 = "88 02 77 73 07 63 67" >> r8 = "99 65 04 28 06 16 70 92" >> r9 = "41 41 26 56 83 40 80 70 33" >> r10 = "41 48 72 33 47 32 37 16 94 29" >> r11 = "53 71 44 65 25 43 91 52 97 51 14" >> r12 = "70 11 33 28 77 73 17 78 39 68 17 57" >> r13 = "91 71 52 38 17 14 91 43 58 50 27 29 48" >> r14 = "63 66 04 68 89 53 67 30 73 16 69 87 40 31" >> r15 = "04 62 98 27 23 09 70 98 73 93 38 53 60 04 23" >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Sok Ha, CHANG Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul. Tel: +82-2-442-7585 -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Sun Apr 6 07:41:41 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 06 Apr 2014 02:41:41 -0500 Subject: [Haskell-beginners] Trying to grasp Monads - IO and delayed actions Message-ID: A spiral approach to learning: you understand, then you learn more, and then you are more confused than ever. I recall that a function in the IO Monad just combines actions to make a big action list, and doesn't actually do the side-effect-laden "work" until it is later triggered, normally because the program is defined to evaluate main. Depending on the time of day and which example I pick, I can sometimes follow what's "really happening" in the definition of >>=. But here are some specific questions: In > What is a for-each loop really? It's something which performs some action based on each > element of a list. So we might imagine a function with the type: > > forM :: (Monad m) => [a] -> (a -> m b) -> m [b] > > (as an added bonus, we'll have it collect the results of each iteration). > > We can write this with sequence and map: > > forM xs f = sequence (map f xs) > > we apply the function to each element of the list to construct the action for that > iteration, and then sequence the actions together into a single computation. > So map by itself produces a [m b]. Why does it need to be turned into m [b]? What does the 'sequence' accomplish, other than restructuring the results that already exist? The reason I asked about (#?#) is because I wanted to see what IO was really doing, to see what the difference was between using >>= initially and then somehow "cranking" it later. lists > instance Monad IO where > {-# INLINE return #-} > {-# INLINE (>>) #-} > {-# INLINE (>>=) #-} > m >> k = m >>= \ _ -> k > return = returnIO > (>>=) = bindIO > fail s = failIO s > > returnIO :: a -> IO a > returnIO x = IO $ \ s -> (# s, x #) > > bindIO :: IO a -> (a -> IO b) -> IO b > bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k a) new_s > > thenIO :: IO a -> IO b -> IO b > thenIO (IO m) k = IO $ \ s -> case m s of (# new_s, _ #) -> unIO k new_s > > unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #)) > unIO (IO a) = a where bindIO is the function of interest. In a chain of commands, getLine might be the 'k' argument to bindIO. Somewhere there's a real machine function called to do the reading from the file, right? From ngnr63q02 at sneakemail.com Sun Apr 6 07:44:34 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 06 Apr 2014 02:44:34 -0500 Subject: [Haskell-beginners] iterated function value sequence In-Reply-To: References: Message-ID: On 4/5/2014 10:55 AM, Bob Ippolito wrote: > Now, how might I do something like that but "forget" previous values to free up memory? > > > Garbage Collection does that for you. If all the references are gone, the memory can be > freed. If you have a specific use in mind I can show you what that looks like. > > Say, zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0 and at some point I print $ zz!!30 How does the GC know that I'll never refer to zz!!n again where n < 30 ? From ngnr63q02 at sneakemail.com Sun Apr 6 07:51:49 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 06 Apr 2014 02:51:49 -0500 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: <1396711227.10143.2.camel@emanuel-laptop> Message-ID: On 4/5/2014 10:31 AM, Christopher Allen wrote: > > Search I used to get your answer: > http://symbolhound.com/?q=tuple+%28%23+%23%29+Haskell Thanks. That is more helpful than "sure it works!". I had tried searches like Since Emanuel was confused by my meta-quoting of syntax in prose, is there some (other) standard that's used in this mailing list when using plain text without markup? It's not just Google, BTW. I can't search PDF files either. The search bar on www.haskell.org itself gives "no results" and other things (I forget what) have even given syntax error! From hsyl20 at gmail.com Sun Apr 6 09:20:24 2014 From: hsyl20 at gmail.com (Sylvain Henry) Date: Sun, 6 Apr 2014 11:20:24 +0200 Subject: [Haskell-beginners] Trying to grasp Monads - IO and delayed actions In-Reply-To: References: Message-ID: [IO b] is a list of functions that perform side-effects. You can remove elements of the list, add new ones, change the order of the elements, etc. All of these operations are pure because you do not execute side-effecting functions: you just manipulate a list. When you use "sequence" on it and get a "IO [b]", you build a single function that calls every function of the previous list in sequence (think ";" in imperative programming languages) and returns their results in a list. As long as you don't call it, no side-effect occurs. The IO monad is used to ensure that side-effecting functions are performed in a deterministic order and are not mixed up with pure code. See my introduction in [1], maybe it can help you understand the motivation and the code in GHC.Base where RealWorld is used explicitly. Cheers Sylvain [1] http://www.sylvain-henry.info/home/data/uploads/talks/shenry-2013-02-05-haskell-intro.pdf(from slide 20) 2014-04-06 9:41 GMT+02:00 John M. Dlugosz : > A spiral approach to learning: you understand, then you learn more, and > then you are more confused than ever. > > I recall that a function in the IO Monad just combines actions to make a > big action list, and doesn't actually do the side-effect-laden "work" until > it is later triggered, normally because the program is defined to evaluate > main. > > Depending on the time of day and which example I pick, I can sometimes > follow what's "really happening" in the definition of >>=. But here are > some specific questions: > > In > > What is a for-each loop really? It's something which performs some action >> based on each >> element of a list. So we might imagine a function with the type: >> >> forM :: (Monad m) => [a] -> (a -> m b) -> m [b] >> >> (as an added bonus, we'll have it collect the results of each iteration). >> >> We can write this with sequence and map: >> >> forM xs f = sequence (map f xs) >> >> we apply the function to each element of the list to construct the action >> for that >> iteration, and then sequence the actions together into a single >> computation. >> >> > So map by itself produces a [m b]. Why does it need to be turned into m > [b]? What does the 'sequence' accomplish, other than restructuring the > results that already exist? > > The reason I asked about (#?#) is because I wanted to see what IO was > really doing, to see what the difference was between using >>= initially > and then somehow "cranking" it later. > > GHC-Base.html#%3E%3E%3D> lists > >> instance Monad IO where >> {-# INLINE return #-} >> {-# INLINE (>>) #-} >> {-# INLINE (>>=) #-} >> m >> k = m >>= \ _ -> k >> return = returnIO >> (>>=) = bindIO >> fail s = failIO s >> >> returnIO :: a -> IO a >> returnIO x = IO $ \ s -> (# s, x #) >> >> bindIO :: IO a -> (a -> IO b) -> IO b >> bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k a) >> new_s >> >> thenIO :: IO a -> IO b -> IO b >> thenIO (IO m) k = IO $ \ s -> case m s of (# new_s, _ #) -> unIO k new_s >> >> unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #)) >> unIO (IO a) = a >> > > where bindIO is the function of interest. In a chain of commands, getLine > might be the 'k' argument to bindIO. Somewhere there's a real machine > function called to do the reading from the file, right? > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Sun Apr 6 09:47:54 2014 From: gesh at gesh.uni.cx (Gesh) Date: Sun, 06 Apr 2014 12:47:54 +0300 Subject: [Haskell-beginners] Trying to grasp Monads - IO and delayed actions In-Reply-To: References: Message-ID: <3885eb23-c828-492e-8d14-718dd5ac873f@email.android.com> On April 6, 2014 10:41:41 AM GMT+03:00, "John M. Dlugosz" wrote: >A spiral approach to learning: you understand, then you learn more, and >then you are more >confused than ever. > Excellent characterization of learning. Mind if I use it? >I recall that a function in the IO Monad just combines actions to make >a big action list, >and doesn't actually do the side-effect-laden "work" until it is later >triggered, normally >because the program is defined to evaluate main. > >Depending on the time of day and which example I pick, I can sometimes >follow what's >"really happening" in the definition of >>=. But here are some >specific questions: > >In > >> What is a for-each loop really? It's something which performs some >action based on each >> element of a list. So we might imagine a function with the type: >> >> forM :: (Monad m) => [a] -> (a -> m b) -> m [b] >> >> (as an added bonus, we'll have it collect the results of each >iteration). >> >> We can write this with sequence and map: >> >> forM xs f = sequence (map f xs) >> >> we apply the function to each element of the list to construct the >action for that >> iteration, and then sequence the actions together into a single >computation. >> > >So map by itself produces a [m b]. Why does it need to be turned into >m [b]? What does >the 'sequence' accomplish, other than restructuring the results that >already exist? > In essence, what a value of type Monad m => [m a] means is that you have a list full of monadic actions. It's a bit like having a list of functions - while you can certainly use the actions and functions, respectively, they haven't been evaluated *yet*. Whereas a value of type Monad m => m [a] represents a monadic action returning some list of values. Thus, sequence is collecting the values computed by each action. This is all made clear by the implementation of sequence: sequence ms = foldr k (return []) ms where k m m' = do { x <- m; xs <- m'; return (x:xs) } Note that lists aren't special, Data.Traversable defines sequence for all traversable values: sequence = unwrapMonad . traverse . WrapMonad >The reason I asked about (#?#) is because I wanted to see what IO was >really doing, to see >what the difference was between using >>= initially and then somehow >"cranking" it later. > > >lists >> instance Monad IO where >> {-# INLINE return #-} >> {-# INLINE (>>) #-} >> {-# INLINE (>>=) #-} >> m >> k = m >>= \ _ -> k >> return = returnIO >> (>>=) = bindIO >> fail s = failIO s >> >> returnIO :: a -> IO a >> returnIO x = IO $ \ s -> (# s, x #) >> >> bindIO :: IO a -> (a -> IO b) -> IO b >> bindIO (IO m) k = IO $ \ s -> case m s of (# new_s, a #) -> unIO (k >a) new_s >> >> thenIO :: IO a -> IO b -> IO b >> thenIO (IO m) k = IO $ \ s -> case m s of (# new_s, _ #) -> unIO k >new_s >> >> unIO :: IO a -> (State# RealWorld -> (# State# RealWorld, a #)) >> unIO (IO a) = a > >where bindIO is the function of interest. In a chain of commands, >getLine might be the >'k' argument to bindIO. Somewhere there's a real machine function >called to do the >reading from the file, right? Yes. I don't know enough about GHC's implementation of the Prelude, but basically, what's going on here is that IO works a bit like State, except it uses unboxed tuples and doesn't have an evalState function. getLine and its ilk are all probably defined using primitive operations. That basically sums up my knowledge of how IO works. Hoping to help, Gesh From ngnr63q02 at sneakemail.com Sun Apr 6 10:04:23 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 06 Apr 2014 05:04:23 -0500 Subject: [Haskell-beginners] Trying to grasp Monads - IO and delayed actions In-Reply-To: <3885eb23-c828-492e-8d14-718dd5ac873f@email.android.com> References: <3885eb23-c828-492e-8d14-718dd5ac873f@email.android.com> Message-ID: On 4/6/2014 4:47 AM, Gesh wrote: > On April 6, 2014 10:41:41 AM GMT+03:00, "John M. Dlugosz" wrote: >> A spiral approach to learning: you understand, then you learn more, and >> then you are more >> confused than ever. >> > Excellent characterization of learning. Mind if I use it? By my guest. See From martin.drautzburg at web.de Sun Apr 6 13:20:14 2014 From: martin.drautzburg at web.de (martin) Date: Sun, 06 Apr 2014 15:20:14 +0200 Subject: [Haskell-beginners] State as function? Message-ID: <5341548E.3020701@web.de> Hello all, in my program, I have to keep track of the State of a Guitar, i.e. where the fingers are on the fretboard and which strings are currently playing. Thus I need to associate some information to each of the 6 strings. I started out with assoc lists and hashmaps. However, I found it diffiult to ensure that all 6 GuitarString have a something attached and found myself messing with Maybes. In the real world, there is no way a GuitarString can have "no state at all", while the HashMap approach would provide this option. Then I thought, hey I really just need a function from GuitarString to something, so why not make that function the state? That was certainly doable, but I still have my doubts whether this is the correct way of doing such things. What is particularly worrying is that a state change will most likely affect a single GuitarString only. So I need to create a new function which answers the new something for that particular GuitarString and calls the old function for strings whose state hasn't changed. So with many state changes, I will get a deep call hierarchy. Though in the real world this is unlikely to cause problems, it makes me worry. What do you think? From daniel.trstenjak at gmail.com Sun Apr 6 14:05:14 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Sun, 6 Apr 2014 16:05:14 +0200 Subject: [Haskell-beginners] State as function? In-Reply-To: <5341548E.3020701@web.de> References: <5341548E.3020701@web.de> Message-ID: <20140406140514.GA2213@machine> Hi Martin, On Sun, Apr 06, 2014 at 03:20:14PM +0200, martin wrote: > I started out with assoc lists and hashmaps. However, I found it > diffiult to ensure that all 6 GuitarString have a something attached > and found myself messing with Maybes. In the real world, there is no > way a GuitarString can have "no state at all", while the HashMap > approach would provide this option. I would start by designing the data structures that represent your possible states. That way you can also ensure that there can't be any non representable states. I really don't now what makes sense in your case, but something like: data StringState = Resting | Oscillating ... data Guitar = Guitar { string1 :: StringState , string2 :: StringState ... } If you have worked out the right data structures, then writing the surrounding code can become quite naturally. Greetings, Daniel From martin.drautzburg at web.de Sun Apr 6 16:28:54 2014 From: martin.drautzburg at web.de (martin) Date: Sun, 06 Apr 2014 18:28:54 +0200 Subject: [Haskell-beginners] State as function? In-Reply-To: <20140406140514.GA2213@machine> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> Message-ID: <534180C6.8080601@web.de> Am 04/06/2014 04:05 PM, schrieb Daniel Trstenjak: > > data StringState = Resting > | Oscillating > ... > > data Guitar = Guitar > { string1 :: StringState > , string2 :: StringState > ... > } Yes this, and also the information on which fret the finger is. The problem with this approach is that I end up with two representations of GuitarString. One is the accessor functions above, but there is also a datatype GuitarString which I need to express things like "put your finger on the nth fret of the mth string". I found myself mapping between these two representations, which is not hard to do, but it impairs clarity. From bob at redivi.com Sun Apr 6 16:41:38 2014 From: bob at redivi.com (Bob Ippolito) Date: Sun, 6 Apr 2014 09:41:38 -0700 Subject: [Haskell-beginners] iterated function value sequence In-Reply-To: References: Message-ID: If zz is top-level, then it doesn't know you'll never refer to it again (the compiler doesn't *have* to keep this around it's hard to know whether or not it will). ?> let zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0 ?> print (zz !! 2) 21 ?> :sprint zz zz = _ : _ : 21 : _ Note also that iterate is best when you use all of the values up to where you want to stop (or at least make sure they get evaluated). If you want to iterate a specific number of times and just get the result, perhaps foldl' is the way to do it. ?> abs $ numerator $ foldl' (\x _ -> x^2-7%4) 0 (replicate 7 ()) 595096023596888261906480183623645954687 See also: https://ghc.haskell.org/trac/ghc/ticket/3474 http://stackoverflow.com/questions/8909997/haskell-repeat-a-function-a-large-number-of-times-without-stackoverflow On Sun, Apr 6, 2014 at 12:44 AM, John M. Dlugosz wrote: > On 4/5/2014 10:55 AM, Bob Ippolito wrote: > >> Now, how might I do something like that but "forget" previous values >> to free up memory? >> >> >> Garbage Collection does that for you. If all the references are gone, the >> memory can be >> freed. If you have a specific use in mind I can show you what that looks >> like. >> >> >> Say, > zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0 > > and at some point I > print $ zz!!30 > > How does the GC know that I'll never refer to zz!!n again where n < 30 ? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Sun Apr 6 17:39:25 2014 From: karl at karlv.net (Karl Voelker) Date: Sun, 06 Apr 2014 10:39:25 -0700 Subject: [Haskell-beginners] State as function? In-Reply-To: <534180C6.8080601@web.de> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> Message-ID: <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> On Sun, Apr 6, 2014, at 09:28 AM, martin wrote: > The problem with this approach is that I end up with two representations > of GuitarString. One is the accessor functions > above, but there is also a datatype GuitarString which I need to express > things like "put your finger on the nth fret of > the mth string". You could try using an array: data StringIndex = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum, Bounded, Read, Show) type Guitar = Array StringIndex StringState http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html The Enum and Bounded constraints on StringIndex let you do fun things like: allStrings = [minBound .. maxBound] -Karl From karl at karlv.net Sun Apr 6 17:50:02 2014 From: karl at karlv.net (Karl Voelker) Date: Sun, 06 Apr 2014 10:50:02 -0700 Subject: [Haskell-beginners] State as function? In-Reply-To: <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> Message-ID: <1396806602.21570.103353977.06CBC81D@webmail.messagingengine.com> On Sun, Apr 6, 2014, at 10:39 AM, Karl Voelker wrote: > data StringIndex = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum, > Bounded, Read, Show) I should have included the Ix class in the deriving list, so you can use it as an Array index. -Karl From poczta at emanuelkoczwara.pl Sun Apr 6 18:42:03 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Sun, 06 Apr 2014 20:42:03 +0200 Subject: [Haskell-beginners] What does "(# #)" mean? In-Reply-To: References: <1396711227.10143.2.camel@emanuel-laptop> Message-ID: <1396809723.9958.4.camel@emanuel-laptop> Hi, > Since Emanuel was confused by my meta-quoting of syntax in prose, is there some (other) > standard that's used in this mailing list when using plain text without markup? My response to your question was to impulsive, sorry about that. Regards, Emanuel From ngnr63q02 at sneakemail.com Mon Apr 7 08:33:47 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 07 Apr 2014 03:33:47 -0500 Subject: [Haskell-beginners] What is a "Monad Comprehension" ? Message-ID: I know about List Comprehensions, but what is a general Monad Comprehension? List Comprehensions are the use of set-builder notation to define a list, so I don't understand how that would generalize. From ky3 at atamo.com Mon Apr 7 08:45:29 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 7 Apr 2014 15:45:29 +0700 Subject: [Haskell-beginners] What is a "Monad Comprehension" ? In-Reply-To: References: Message-ID: On Mon, Apr 7, 2014 at 3:33 PM, John M. Dlugosz wrote: > I know about List Comprehensions, but what is a general Monad > Comprehension? List Comprehensions are the use of set-builder notation to > define a list, so I don't understand how that would generalize. Assuming that you have done a search and that you're still stumped, how can you make the question more specific? Do you think that quoting specific bits of what you have read might help us help you more? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Mon Apr 7 09:06:44 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 07 Apr 2014 04:06:44 -0500 Subject: [Haskell-beginners] Problem combining monads Message-ID: I think I was looking at the page a few paragraphs under the H3 ?The join function?. It mentions ??we wanted to combine several of them into one, be it with <*> or >>=,?? Now I'm actually quite comfortable with Applicative syntax, and am preferring it in places where older texts and examples use monads or fmap etc. (I look forward to an updated standard where Monads are derived from Applicatives.) (Aside: is there a "history" function in GHCi to show the last few commands I typed?) So, I tried a GHCi example starting with: ?>(+7) <$> [1,2,3] ?>[(+7), (*2)] <*> [1,2,3] no sweat. Now use >>= to do the same thing: ?>[(+7), (*2)] >>= [1,2,3] -- nope (I'll spare the error messages, which inspired some of the other things to try, some of which were just to explore the errors themselves) ?>[(+7), (*2)] <<= [1,2,3] -- nope ?>[(+7), (*2)] =<< [1,2,3] -- nope ?>[return (+7), return(*2)] =<< [1,2,3] -- nope (Use a single function as a simpler starting point) ?>(+7) =<< [1,2,3] -- nope ?>return (+7) =<< [1,2,3] -- nope ?>return (+7) >>= [1,2,3] -- nope ?>return [1,2,3] [1,2,3] it :: [Integer] -- just checking ?>[1,2,3] >>= (+7) -- nope ?>[1,2,3] >>= do (+7) -- nope. Error messages are talking about No instance for (Num [b0]) arising from the literal `1' so why is it looking inside the monad at one of the values and thinking that itself should be a list? ?>[1,2,3] >>= [(+7)] -- nope ?>[1,2,3] >>= (+7).return -- nope, but got *two* errors out of that one ?>[1,2,3] >>= liftM(+7) -- nope ?>[1,2,3] >>= liftM(+7) ::[Integer] -- nope ?>[1,2,3] >>= liftM(+7) ::Integer -- nope ?>[1,2,3] >>= liftM((+7) :: Integer -> Integer) -- nope ?>[[1,2,3]] >>= liftM((+7) :: Integer -> Integer) [8,9,10] it :: [Integer] Ah, I'm getting somewhere. But back to the question of why is the element of the list (nondeterminism monad) want its own element to be a list too? ?>return [1,2,3] >>= liftM((+7) :: Integer -> Integer) [8,9,10] it :: [Integer] Same thing. But the list is already a monad, not a bare (scalar) value, so what's going on? ?>[1,2,3] >>= (+7) -- nope ?>[1,2,3] >>= map(+7) -- nope ?>[1,2,3] >>= liftM(+7) -- nope Am I starting to repeat myself? ?>[1,2,3] >>= liftM(+7) ::[Integer] -- nope Give up for the evening. I wonder if that is a record for how many ways it can be incorrect? I'm frustrated, especially since the Applicative syntax worked so easily. But the referenced page and other texts casually say how they are interchangable. Urrrgh! Please enlighten? ?John From edv.karol at gmail.com Mon Apr 7 09:13:59 2014 From: edv.karol at gmail.com (Karol Samborski) Date: Mon, 7 Apr 2014 11:13:59 +0200 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: Hi John! 2014-04-07 11:06 GMT+02:00 John M. Dlugosz : > I think I was looking at the page > a few paragraphs > under the H3 ?The join function?. It mentions ??we wanted to combine > several of them into one, be it with <*> or >>=,?? > > Now I'm actually quite comfortable with Applicative syntax, and am > preferring it in places where older texts and examples use monads or fmap > etc. (I look forward to an updated standard where Monads are derived from > Applicatives.) > > (Aside: is there a "history" function in GHCi to show the last few > commands I typed?) > > So, I tried a GHCi example starting with: > > ?>(+7) <$> [1,2,3] > > ?>[(+7), (*2)] <*> [1,2,3] > > no sweat. Now use >>= to do the same thing: > > ?>[(+7), (*2)] >>= [1,2,3] > -- nope > (I'll spare the error messages, which inspired some of the other things to > try, some of which were just to explore the errors themselves) > > ?>[(+7), (*2)] <<= [1,2,3] > -- nope > > ?>[(+7), (*2)] =<< [1,2,3] > -- nope > > ?>[return (+7), return(*2)] =<< [1,2,3] > -- nope > > (Use a single function as a simpler starting point) > > ?>(+7) =<< [1,2,3] > -- nope > > ?>return (+7) =<< [1,2,3] > -- nope > > ?>return (+7) >>= [1,2,3] > -- nope > > ?>return [1,2,3] > [1,2,3] > it :: [Integer] > -- just checking > > ?>[1,2,3] >>= (+7) > -- nope > ?>[1,2,3] >>= do (+7) > -- nope. Error messages are talking about > No instance for (Num [b0]) arising from the literal `1' > so why is it looking inside the monad at one of the values and thinking that > itself should be a list? > > ?>[1,2,3] >>= [(+7)] > -- nope > > ?>[1,2,3] >>= (+7).return > -- nope, but got *two* errors out of that one You're close. It should be: [1,2,3] >>= return . (+7) Best, Karol From daniel.trstenjak at gmail.com Mon Apr 7 09:20:22 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 7 Apr 2014 11:20:22 +0200 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: <20140407092022.GA4540@machine> Hi John, for some versions you almost did it ;). > ?>return (+7) =<< [1,2,3] return . (+7) =<< [1,2,3] > ?>[1,2,3] >>= (+7).return [1,2,3] >>= return . (+7) It's really helpful to look at the types: > :t return (+7) return (+7) :: (Monad m, Num a) => m (a -> a) > :t return . (+7) return . (+7) :: (Monad m, Num b) => b -> m b Greetings, Daniel From akaberto at gmail.com Mon Apr 7 09:38:46 2014 From: akaberto at gmail.com (akash g) Date: Mon, 7 Apr 2014 15:08:46 +0530 Subject: [Haskell-beginners] What is a "Monad Comprehension" ? In-Reply-To: References: Message-ID: At one point of time, comprehensions were available for all monads. Then it was removed, and has now been added back as a language extension (from the cursory glance that I gave) Basically, monad comprehensions are about using the samesyntactic sugar for different monads (like the do notation) Perhaps, this'll help (if you haven't seen this already) https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions On Mon, Apr 7, 2014 at 2:15 PM, Kim-Ee Yeoh wrote: > > On Mon, Apr 7, 2014 at 3:33 PM, John M. Dlugosz wrote: > >> I know about List Comprehensions, but what is a general Monad >> Comprehension? List Comprehensions are the use of set-builder notation to >> define a list, so I don't understand how that would generalize. > > > Assuming that you have done a search and that you're still stumped, how > can you make the question more specific? > > Do you think that quoting specific bits of what you have read might help > us help you more? > > -- Kim-Ee > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at orlitzky.com Mon Apr 7 13:05:42 2014 From: michael at orlitzky.com (Michael Orlitzky) Date: Mon, 07 Apr 2014 09:05:42 -0400 Subject: [Haskell-beginners] What is a "Monad Comprehension" ? In-Reply-To: References: Message-ID: <5342A2A6.1000809@orlitzky.com> On 04/07/2014 04:33 AM, John M. Dlugosz wrote: > I know about List Comprehensions, but what is a general Monad Comprehension? List > Comprehensions are the use of set-builder notation to define a list, so I don't understand > how that would generalize. The set-builder notation to find the sums of elements from two lists looks like, list_sums = [ x + y | x <- [1,2,3], y <- [4,5,6] ] The way the list monad is defined, this is the same thing as, list_sums = do x <- [1, 2, 3] y <- [4, 5, 6] return (x + y) The set-builder notation is not obviously generalizable, but once you desugar it to do-notation, it's more clear that you could do the same thing with any monad. What actually happens might not be intuitive, but the desugaring will work at least. For the Maybe monad, you could do something like, maybe_sums = [ x + y | x <- Just 1, y <- Just 2 ] which will be desugared into, maybe_sums = do x <- Just 1 y <- Just 2 return (x + y) Any other monad will work the same. From martin.drautzburg at web.de Mon Apr 7 18:43:34 2014 From: martin.drautzburg at web.de (martin) Date: Mon, 07 Apr 2014 20:43:34 +0200 Subject: [Haskell-beginners] State as function? In-Reply-To: <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> Message-ID: <5342F1D6.8010405@web.de> Am 04/06/2014 07:39 PM, schrieb Karl Voelker: > On Sun, Apr 6, 2014, at 09:28 AM, martin wrote: >> The problem with this approach is that I end up with two representations >> of GuitarString. One is the accessor functions >> above, but there is also a datatype GuitarString which I need to express >> things like "put your finger on the nth fret of >> the mth string". > > You could try using an array: > > data StringIndex = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum, > Bounded, Read, Show) > > type Guitar = Array StringIndex StringState > > http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html > > The Enum and Bounded constraints on StringIndex let you do fun things > like: > > allStrings = [minBound .. maxBound] Thanks Karl, this tidies up my code quite a bit. BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the way a guitar is tuned. Way cool. From ky3 at atamo.com Mon Apr 7 19:00:05 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 8 Apr 2014 02:00:05 +0700 Subject: [Haskell-beginners] State as function? In-Reply-To: <5342F1D6.8010405@web.de> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> <5342F1D6.8010405@web.de> Message-ID: On Tue, Apr 8, 2014 at 1:43 AM, martin wrote: > BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the > way a guitar is tuned. Way cool. Nice :) This n-tuple as Array trick is something I missed. Thanks, Karl. Do you know where it's been used? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Tue Apr 8 02:58:42 2014 From: karl at karlv.net (Karl Voelker) Date: Mon, 07 Apr 2014 19:58:42 -0700 Subject: [Haskell-beginners] State as function? In-Reply-To: References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> <5342F1D6.8010405@web.de> Message-ID: <1396925922.5388.103927757.0003335F@webmail.messagingengine.com> On Mon, Apr 7, 2014, at 12:00 PM, Kim-Ee Yeoh wrote: On Tue, Apr 8, 2014 at 1:43 AM, martin <[1]martin.drautzburg at web.de> wrote: BTW: I named the StringIndexes E6 | A | D | G | B | E1 which reflects the way a guitar is tuned. Way cool. Nice :) This n-tuple as Array trick is something I missed. Thanks, Karl. Do you know where it's been used? The last time I used this technique was when I needed to represent the state of a mancala board. I may have seen it done somewhere else, but I don't remember. -Karl References 1. mailto:martin.drautzburg at web.de -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Apr 8 03:33:39 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 8 Apr 2014 10:33:39 +0700 Subject: [Haskell-beginners] State as function? In-Reply-To: <1396925922.5388.103927757.0003335F@webmail.messagingengine.com> References: <5341548E.3020701@web.de> <20140406140514.GA2213@machine> <534180C6.8080601@web.de> <1396805965.19802.103350505.0DDCAED3@webmail.messagingengine.com> <5342F1D6.8010405@web.de> <1396925922.5388.103927757.0003335F@webmail.messagingengine.com> Message-ID: On Tue, Apr 8, 2014 at 9:58 AM, Karl Voelker wrote: > The last time I used this technique was when I needed to represent the > state of a mancala board. I may have seen it done somewhere else, but I > don't remember. > Ah thanks. I'll be sure to stay on the lookout for this. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From nishantgeek at gmail.com Tue Apr 8 05:00:01 2014 From: nishantgeek at gmail.com (Nishant) Date: Tue, 8 Apr 2014 10:30:01 +0530 Subject: [Haskell-beginners] set multiplication Message-ID: hi, I am trying to implement a set multiplication program in haskell. Input : [1,2,3] [4,5] [6,7] Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] I implemented it for constant number of inputs like setMul xs ys zs = [ [x] ++ [y] ++ [z] | x <- xs , y<-ys ,z <- zs] I am not able to generalize this for any number of lists. type signature would be : setMulMany :: [[a]] -> [[a]] Example : Input : [ [1,2,3] , [4,5] , [6,7]] Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] Regards. Nishant -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariano.perez.rodriguez at gmail.com Tue Apr 8 05:28:27 2014 From: mariano.perez.rodriguez at gmail.com (=?UTF-8?Q?Mariano_P=C3=A9rez_Rodr=C3=ADguez?=) Date: Tue, 8 Apr 2014 02:28:27 -0300 Subject: [Haskell-beginners] set multiplication In-Reply-To: References: Message-ID: It's been a while since I last wrote any Haskell code, but try: setMulMany :: [[a]] -> [[a]] setMulMany = foldr mulOne [[]] where mulOne :: [a] -> [[a]] -> [[a]] mulOne xs yss = [ x:ys | x <- xs, ys <- yss] I'm writing this from my phone, so didn't have a chance to test it really... Hope it helps! On Apr 8, 2014 2:00 AM, "Nishant" wrote: > > hi, > > I am trying to implement a set multiplication program in haskell. > > Input : [1,2,3] [4,5] [6,7] > Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] > > > I implemented it for constant number of inputs like > > setMul xs ys zs = [ [x] ++ [y] ++ [z] | x <- xs , y<-ys ,z <- zs] > > I am not able to generalize this for any number of lists. > > type signature would be : > > setMulMany :: [[a]] -> [[a]] > > Example : > > Input : [ [1,2,3] , [4,5] , [6,7]] > Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] > > > > Regards. > Nishant > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Apr 8 05:54:17 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 8 Apr 2014 12:54:17 +0700 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: John, This is one MAJOR hurdle for newcomers to get over and pretty much everyone stumbles and falls into typed combinator enlightenment. Eventually. Let's ask ghci what some of the types are: :t [1,2,3] [1,2,3] :: [Int] :t (+7) (+7) :: Int -> Int :t (<$>) (<$>) :: Functor f => (a -> b) -> f a -> f b So in ((+7) <$>) we have (<$>) :: Functor f => (a -> b) -> f a -> f b applied to (note the _type_ of the first argument: a -> b): (+7) :: Int -> Int Now the type variables a and b must match up, so we have a=b=Int. And now we have ((+7) <$>) :: Functor f => f Int - f Int Which is, in turn, applied to [1,2,3] :: [Int] The argument in ((+7) <$>) has type Functor f => f Int, whereas [1,2,3] is [Int], so what can f be? Answer: []. Yes, [] is a functor, the way Maybe and IO are functors. So f = []. It's unusual but only syntatically in the sense that Haskell says to write Maybe Int but rejects [] Int. You have to write [Int] to mean [] Int. We say that ((+7) <$>) :: Functor f => f Int - f Int is thus specialized to ((+7) <$>) :: [Int] -> [Int] which is why (+7) <$> [1,2,3] typechecks and runs as it should. If you run through the above against the rest of the list of things that do and don't work, I think you'll sew up typed combinators as a fine feather in your cap. Just remember that [] is a Functor and also an Applicative and a Monad. Trying different combinations is so much more satisfying if you lean on the types to lead you to greater good. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From Christian.Maeder at dfki.de Tue Apr 8 07:06:29 2014 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Tue, 08 Apr 2014 09:06:29 +0200 Subject: [Haskell-beginners] set multiplication In-Reply-To: References: Message-ID: <53439FF5.7050001@dfki.de> Some time ago I discovered that the monadic "sequence" function operates quite interestingly on lists. I don't know why you call it multiplication. I was looking for something like combinations: Prelude> sequence [[1,2,3], [4,5], [6,7]] HTH Christian Am 08.04.2014 07:00, schrieb Nishant: > > hi, > > I am trying to implement a set multiplication program in haskell. > > Input : [1,2,3] [4,5] [6,7] > Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] > > > I implemented it for constant number of inputs like > > setMul xs ys zs = [ [x] ++ [y] ++ [z] | x <- xs , y<-ys ,z <- zs] > > I am not able to generalize this for any number of lists. > > type signature would be : > > setMulMany :: [[a]] -> [[a]] > > Example : > > Input : [ [1,2,3] , [4,5] , [6,7]] > Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] > > > Regards. > Nishant > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From ngnr63q02 at sneakemail.com Tue Apr 8 15:21:57 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 08 Apr 2014 10:21:57 -0500 Subject: [Haskell-beginners] What is a "Monad Comprehension" ? In-Reply-To: References: Message-ID: On 4/7/2014 4:38 AM, akash g wrote: > At one point of time, comprehensions were available for all monads. Then it was removed, > and has now been added back as a language extension (from the cursory glance that I gave) > > Basically, monad comprehensions are about using the samesyntactic sugar for different > monads (like the do notation) > > Perhaps, this'll help (if you haven't seen this already) > > https://ghc.haskell.org/trac/ghc/wiki/MonadComprehensions Thanks for the answer, and for the link to a ghc/wiki in general! From ngnr63q02 at sneakemail.com Tue Apr 8 15:32:46 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 08 Apr 2014 10:32:46 -0500 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: On 4/8/2014 12:54 AM, Kim-Ee Yeoh wrote: > John, > > This is one MAJOR hurdle for newcomers to get over and pretty much everyone stumbles and > falls into typed combinator enlightenment. Eventually. > ? > If you run through the above against the rest of the list of things that do and don't > work, I think you'll sew up typed combinators as a fine feather in your cap. Just remember > that [] is a Functor and also an Applicative and a Monad. Thanks. I'll work through that again. ?John From byorgey at seas.upenn.edu Tue Apr 8 17:34:12 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue, 8 Apr 2014 13:34:12 -0400 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: <20140408173412.GA22045@seas.upenn.edu> On Tue, Apr 08, 2014 at 12:54:17PM +0700, Kim-Ee Yeoh wrote: > > Answer: []. Yes, [] is a functor, the way Maybe and IO are functors. So f = > []. It's unusual but only syntatically in the sense that Haskell says to > write Maybe Int but rejects [] Int. You have to write [Int] to mean [] Int. This is not true. In fact, [] Int is perfectly valid Haskell syntax; it's just not very common. -Brent From ky3 at atamo.com Tue Apr 8 19:48:59 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Wed, 9 Apr 2014 02:48:59 +0700 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: <20140408173412.GA22045@seas.upenn.edu> References: <20140408173412.GA22045@seas.upenn.edu> Message-ID: On Wed, Apr 9, 2014 at 12:34 AM, Brent Yorgey wrote: > This is not true. In fact, [] Int is perfectly valid Haskell syntax; I stand corrected. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Tue Apr 8 21:50:30 2014 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Tue, 8 Apr 2014 14:50:30 -0700 Subject: [Haskell-beginners] monad transformers and laziness Message-ID: I wrote some code as part of my effort to do backtracking search of musical structures. Most recently I wrote a function that "pruned" the search tree by looking for illegal combinations of notes. In the interest of efficiency I wanted this algorithm to terminate as soon as it encountered an illegal combination rather than hunt the entire structure for every illegality. This made me think of the laziness of "or": a function like g = x || y || z will not evaluate y and z if x turns out to be true. I was also writing this code within a stack of monad transformers: State, ErrorT, and WriterT. This was primarily for debugging reasons: I wanted to have ErrorT to give good error messages with context, WriterT to log events for later perusal, and State was there for convenience. I called this monad stack "Er." So the first thing I wrote was something like this: -- return True if an illegal combination is found prune :: Music -> Er Bool prune music = do flags <- forM [0..size music-1] (\ix -> checkOneNote music ix) return $ or flags checkOneNote :: Music -> Int -> Er Bool checkOneNote music ix = do tell $ "Checking note " ++ show ix ++ "\n" -- note: were thunks created by following line left unevaluated thanks to laziness of "or flags" above? doSomething music ix I already knew from past experience that mapM (and forM) evaluate every computation within the list so I wasn't really expecting this to be lazy, and it wasn't. Judging from what was "told" to my MonadWriter, I could see it was checking every note and not stopping early at the first illegal note. My first question is: by any chance was this an illusion through my use of "tell"? What I mean, is that was it leaving thunks unevaluated thanks to the "or flags" expressions lazy behavior, or would it have left them unevaluated if I hadn't had the "tell" there? I rewrote this using this function: pruneRecursive :: Music -> Int -> Int -> Er Bool pruneRecursive music maxIx ix | ix == maxIx = return False | do flag <- checkOneNote music maxIx ix if flag then return True else checkOneNote music maxIx (ix+1) According to the writer messages, this was not doing any unnecessary work. Just wondering if it was really necessary to rewrite this. What if I removed the tell messages later, or put them inside 'when' like this when (debugFlag) (tell ..) and then set debugFlag to False for time-critical program runs? Dennis -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Apr 8 22:49:06 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Wed, 9 Apr 2014 05:49:06 +0700 Subject: [Haskell-beginners] How to read: Pearls of Functional Algorithm Design Message-ID: Bruce earlier emailed about trying to read this book but struggled with not having basics like fold laws and list induction. It turns out there's still a lot to gain from the book even if the mathematical content is out of grasp for now. I've written up some tips on how to get started: http://www.atamo.com/blog/how-to-read-pearls-by-richard-bird-1/ The preamble is a bit long but it helps to keep the big picture firmly in mind. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From sickhadas at gmail.com Wed Apr 9 06:03:54 2014 From: sickhadas at gmail.com (Alexej Magura) Date: Wed, 9 Apr 2014 00:03:54 -0600 Subject: [Haskell-beginners] function not working as expected, type issues Message-ID: K, so I have a function that I?m writing that takes a *string* and a *count* and prints the *string* to STDOUT *count* times: displayD :: IO () -> String -> Int displayD string count = do (count > 0) && hPutStr stdout string (count > 0) && displayD string (count - 1) However, when I try to compile the file, I get several errors; here?s a pastebin:?http://pastebin.com/DEsuAvfz What I?m trying to do here is check to see if *count* is greater than 0, and then if it is, then I?d like to print *string* to STDOUT until *count* equals 0. ?I tried doing it via pattern matching, like so: displayD string (count > 0) = do But I haven?t seen any examples of how to do pattern matching in functions using *do*, that is *IO*, so I?m unsure if the above is even anywhere near correct. Still very uncomfortable with declaring function types: I have a hard time figuring out which type is returned and what type is expected as an arg. ?My initial guess is that the *first* type specified is the one returned, but I?m not sure. --? Alexej Magura Sent with Airmail From toad3k at gmail.com Wed Apr 9 06:08:53 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 9 Apr 2014 02:08:53 -0400 Subject: [Haskell-beginners] function not working as expected, type issues In-Reply-To: References: Message-ID: Try using guards: displayD :: IO () -> String -> Int displayD string count = | count > 0 = hPutStr stdout string | otherwise = displayD string (count - 1) Alternatively use if, then, else. On Wed, Apr 9, 2014 at 2:03 AM, Alexej Magura wrote: > K, so I have a function that I'm writing that takes a *string* and a > *count* and prints the *string* to STDOUT *count* times: > > displayD :: IO () -> String -> Int > displayD string count = do > (count > 0) && hPutStr stdout string > (count > 0) && displayD string (count - 1) > > However, when I try to compile the file, I get several errors; here's a > pastebin: http://pastebin.com/DEsuAvfz > > What I'm trying to do here is check to see if *count* is greater than 0, > and then if it is, then I'd like to print *string* to STDOUT until *count* > equals 0. I tried doing it via pattern matching, like so: > > displayD string (count > 0) = do > > But I haven't seen any examples of how to do pattern matching in functions > using *do*, that is *IO*, so I'm unsure if the above is even anywhere near > correct. > > Still very uncomfortable with declaring function types: I have a hard time > figuring out which type is returned and what type is expected as an arg. > My initial guess is that the *first* type specified is the one returned, > but I'm not sure. > > -- > Alexej Magura > Sent with Airmail > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed Apr 9 06:17:12 2014 From: toad3k at gmail.com (David McBride) Date: Wed, 9 Apr 2014 02:17:12 -0400 Subject: [Haskell-beginners] function not working as expected, type issues In-Reply-To: References: Message-ID: I didn't look closely enough at what you wrote, you had several issues. Let me try again: displayD :: String -> Int -> IO () displayD string 0 = return () displayD string count = do hPutStr stdout string displayD string (count - 1) This is one way to do it. You use the arguments themselves. Obviously this won't work with negative counts. displayD :: String -> Int -> IO () displayD string count | count <= 0 = return () | otherwise = do hPutStr stdout string displayD string (count - 1) And as for figuring out types. You should try to load your source file in ghci, and then use the :t and :i commands liberally to inspect functions you are using. You will begin to get the hang of it in no time. On Wed, Apr 9, 2014 at 2:08 AM, David McBride wrote: > Try using guards: > > displayD :: IO () -> String -> Int > displayD string count = > | count > 0 = hPutStr stdout string > | otherwise = displayD string (count - 1) > > Alternatively use if, then, else. > > > > On Wed, Apr 9, 2014 at 2:03 AM, Alexej Magura wrote: > >> K, so I have a function that I'm writing that takes a *string* and a >> *count* and prints the *string* to STDOUT *count* times: >> >> displayD :: IO () -> String -> Int >> displayD string count = do >> (count > 0) && hPutStr stdout string >> (count > 0) && displayD string (count - 1) >> >> However, when I try to compile the file, I get several errors; here's a >> pastebin: http://pastebin.com/DEsuAvfz >> >> What I'm trying to do here is check to see if *count* is greater than 0, >> and then if it is, then I'd like to print *string* to STDOUT until *count* >> equals 0. I tried doing it via pattern matching, like so: >> >> displayD string (count > 0) = do >> >> But I haven't seen any examples of how to do pattern matching in >> functions using *do*, that is *IO*, so I'm unsure if the above is even >> anywhere near correct. >> >> Still very uncomfortable with declaring function types: I have a hard >> time figuring out which type is returned and what type is expected as an >> arg. My initial guess is that the *first* type specified is the one >> returned, but I'm not sure. >> >> -- >> Alexej Magura >> Sent with Airmail >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nadirsampaoli at gmail.com Wed Apr 9 06:20:58 2014 From: nadirsampaoli at gmail.com (Nadir Sampaoli) Date: Wed, 9 Apr 2014 08:20:58 +0200 Subject: [Haskell-beginners] function not working as expected, type issues In-Reply-To: References: Message-ID: Hello Alexej Il 09/apr/2014 08:03 "Alexej Magura" ha scritto: > > K, so I have a function that I?m writing that takes a *string* and a *count* and prints the *string* to STDOUT *count* times: > > displayD :: IO () -> String -> Int > displayD string count = do > (count > 0) && hPutStr stdout string > (count > 0) && displayD string (count - 1) > At glance at least one of your errors comes from your type signature being incorrect. Your function should takes first a String then an Int and should return an IO () because you're printing stuff. So: displayD :: IO () -> String -> Int should become: displayD :: String -> Int -> IO () Regards, Nadir -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Wed Apr 9 08:45:09 2014 From: gesh at gesh.uni.cx (Gesh) Date: Wed, 09 Apr 2014 11:45:09 +0300 Subject: [Haskell-beginners] function not working as expected, type issues In-Reply-To: References: Message-ID: On April 9, 2014 9:03:54 AM GMT+03:00, Alexej Magura wrote: >K, so I have a function that I?m writing that takes a *string* and a >*count* and prints the *string* to STDOUT *count* times: > >displayD :: IO () -> String -> Int >displayD string count = do > (count > 0) && hPutStr stdout string > (count > 0) && displayD string (count - 1) > >However, when I try to compile the file, I get several errors; here?s a >pastebin:?http://pastebin.com/DEsuAvfz > The errors you see here are caused by GHC trying to make sense of what you wrote and failing. This is because the code you wrote isn't well-typed - that is, you're using values of types differing from the types GHC is expecting you to use. More on this below. >What I?m trying to do here is check to see if *count* is greater than >0, and then if it is, then I?d like to print *string* to STDOUT until >*count* equals 0. ?I tried doing it via pattern matching, like so: > Thank you for explaining the problem you're trying to solve instead of Gesh problem you're facing. Most people don't have the decency to do that, leading to hours of pointless discussion. [0] >displayD string (count > 0) = do > >But I haven?t seen any examples of how to do pattern matching in >functions using *do*, that is *IO*, so I?m unsure if the above is even >anywhere near correct. > do-blocks are no different from any other expression in Haskell when it comes to pattern-matching. I'd suggest starting out with a pure (no IO) version of your code, loading it into GHCi and playing around with it, then adding the IO once you feel comfortable with writing pure code. In general, it's considered good practice to separate impure code from pure code. In this case, that would mean creating a function returning the n-time self a concatenation of a string, then printing the result of that function elsewhere. >Still very uncomfortable with declaring function types: I have a hard >time figuring out which type is returned and what type is expected as >an arg. ?My initial guess is that the *first* type specified is the one >returned, but I?m not sure. > I'm curious as to why you thought that. The directionality of the arrows in a type signature is quite clear, at least to me. a -> b is the type of functions *from* a *to* b. This is important to understand, especially once you start treading into higher-order-function-land, using functions such as map :: (a -> b) -> [a] -> [b] (can you guess what it does and how it does it? Can you do that just by looking at the type?). One further note. The errors you saw in your output were caused by this misunderstanding of function types, as well as a misunderstanding of the types do-blocks expect. From your coding style, it appears that you think of && in the way Bash does - as the function: > p && x = if p then x else *nothing* For some suitable *nothing*. However, this is not the case. Firstly, && is just a function that takes two values of type Book and returns True if both of them are True. Second, a function such as the one I described above exists for Monads, by the name of guard. But until you grasp pure Haskell, there is no point in confusing yourself with them yet. Anyway, all of the above means that passing a non-Bool value to && is not acceptable, and since hPutStr stdout string is not a value of type Bool, GHC complains. So, in brief, follow the arrows, values are not automatically cast as in other languages, && is not the guard operator from Bash, and get away from IO for your first couple of attempts with Haskell. Hoping to help, Gesh [0] - http://xyproblem.info/ Alexej, You have several things going on here. From jejones3141 at gmail.com Wed Apr 9 16:31:32 2014 From: jejones3141 at gmail.com (James Jones) Date: Wed, 9 Apr 2014 11:31:32 -0500 Subject: [Haskell-beginners] Problem combining monads In-Reply-To: References: Message-ID: On Mon, Apr 7, 2014 at 4:06 AM, John M. Dlugosz wrote: > (Aside: is there a "history" function in GHCi to show the last > few commands I typed?) > > :hist apparently only works at a breakpoint, but ghci does remember what you typed, and you can look through it with up/down arrow. James -------------- next part -------------- An HTML attachment was scrubbed... URL: From sickhadas at gmail.com Thu Apr 10 04:00:40 2014 From: sickhadas at gmail.com (Alexej Magura) Date: Wed, 9 Apr 2014 22:00:40 -0600 Subject: [Haskell-beginners] function not working as expected, type issues (Gesh) In-Reply-To: References: Message-ID: >an arg. ?My initial guess is that the *first* type specified is the one? >returned, but I?m not sure.? >? I'm curious as to why you thought that. The directionality of the arrows in a type signature is quite clear, at least to me. a -> b is the type of functions *from* a *to* b.? Well, most of the languages that I?ve used are dynamically typed, but the one language that I?m _kinda_ familiar with that _is_ statically typed is C: several of the Haskell tutorials that I?ve been reading describe that: A :: Int means that *A* is _of_ type *Int*, which kind of sounds like A?returns type Int functions such as map :: (a -> b) -> [a] -> [b] (can you guess what it does and how it does it? Can you do that just by looking at the type?).? Map appears to take a lambda function, which takes type *a* returning type *b* (both of which are purely arbitrary), a list of type *a*, and returns a list of type *b*, applying the lambda function to each element in the list of type *a*. From your coding style, it appears that you think of && in the way Bash does - as the function:? > p && x = if p then x else *nothing*? Something like that. Anyway, all of the above means that passing a non-Bool value to && is not acceptable, and since hPutStr stdout string is not a value of type Bool, GHC complains.? Makes sense. -- Alexej Magura Sent with Airmail From sickhadas at gmail.com Thu Apr 10 04:01:45 2014 From: sickhadas at gmail.com (Alexej Magura) Date: Wed, 9 Apr 2014 22:01:45 -0600 Subject: [Haskell-beginners] function not working as expected, type issues (David McBride) In-Reply-To: References: Message-ID: Thanks displayD :: String -> Int -> IO () displayD string 0 = return () ?. is exactly what I was looking for. -- Alexej Magura Sent with Airmail From cma at bitemyapp.com Thu Apr 10 05:41:28 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 10 Apr 2014 00:41:28 -0500 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs Message-ID: Repro'd on GHC 7.8.1 and 7.6.3 Original code is: https://github.com/bitemyapp/bloodhound/ The code only works if I manually (out of band) run insertData (in a REPL) and then run main with the insertData invocation stripped out. If I run main with the insertData invocation included, it throws an exception (head) because the search results are empty. The behavior is as if queryTweet was executing after insertData deleted the index, but before it inserted the new data. The following is a stripped down example: insertData :: IO () insertData = do let encoded = encode exampleTweet _ <- deleteExampleIndex created <- createExampleIndex docCreated <- indexDocument (Server "http://localhost:9200") "twitter" "tweet" exampleTweet "1" print "test" return () el queryTweet :: IO (Either String Tweet) queryTweet = do let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") False) <||> IdentityFilter let search = Search Nothing (Just queryFilter) reply <- searchByIndex testServer "twitter" search let result = eitherDecode (responseBody reply) :: Either String (SearchResult Tweet) let myTweet = fmap (hitSource . head . hits . searchHits) result return myTweet main :: IO () main = do _ <- insertData myTweet <- queryTweet print myTweet further up the call chain, the http call is getting dispatched with http-conduit's gear, the result returned with the expression: withManager $ httpLbs req Included this in case it affects the semantics. Can anybody help? This has stumped me for a couple hours and I couldn't get anything clueful on IRC. Thanks to any that help. --- Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu Apr 10 05:43:52 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 10 Apr 2014 00:43:52 -0500 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: to add to my question adding a delay made it work: main :: IO () main = do _ <- insertData threadDelay 1000000 myTweet <- queryTweet print myTweet On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen wrote: > Repro'd on GHC 7.8.1 and 7.6.3 > > Original code is: https://github.com/bitemyapp/bloodhound/ > > The code only works if I manually (out of band) run insertData (in a REPL) > and then run main with the insertData invocation stripped out. If I run > main with the insertData invocation included, it throws an exception (head) > because the search results are empty. > > The behavior is as if queryTweet was executing after insertData deleted > the index, but before it inserted the new data. > > The following is a stripped down example: > > insertData :: IO () > insertData = do > let encoded = encode exampleTweet > _ <- deleteExampleIndex > created <- createExampleIndex > docCreated <- indexDocument (Server "http://localhost:9200") "twitter" > "tweet" exampleTweet "1" > print "test" > return () > el > queryTweet :: IO (Either String Tweet) > queryTweet = do > let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") False) > <||> IdentityFilter > let search = Search Nothing (Just queryFilter) > reply <- searchByIndex testServer "twitter" search > let result = eitherDecode (responseBody reply) :: Either String > (SearchResult Tweet) > let myTweet = fmap (hitSource . head . hits . searchHits) result > return myTweet > > main :: IO () > main = do > _ <- insertData > myTweet <- queryTweet > print myTweet > > further up the call chain, the http call is getting dispatched with > http-conduit's gear, the result returned with the expression: > > withManager $ httpLbs req > > Included this in case it affects the semantics. > > Can anybody help? This has stumped me for a couple hours and I couldn't > get anything clueful on IRC. > > Thanks to any that help. > > --- Chris > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at snoyman.com Thu Apr 10 06:15:03 2014 From: michael at snoyman.com (Michael Snoyman) Date: Thu, 10 Apr 2014 09:15:03 +0300 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: I can't say I fully understand your code, but it seems like it's doing the following: 1. Send request to server to delete data. 2. Send request to server to add data. 3. Request data from server. You're saying that with a long enough delay between steps 2 and 3, then (3) works, otherwise it fails. It sounds to me like there's some kind of a race condition. I don't know anything about createExampleIndex, but are you certain that it only returns after the data is completely ready for querying? On Thu, Apr 10, 2014 at 8:43 AM, Christopher Allen wrote: > to add to my question adding a delay made it work: > > main :: IO () > main = do > _ <- insertData > threadDelay 1000000 > myTweet <- queryTweet > print myTweet > > > > On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen wrote: > >> Repro'd on GHC 7.8.1 and 7.6.3 >> >> Original code is: https://github.com/bitemyapp/bloodhound/ >> >> The code only works if I manually (out of band) run insertData (in a >> REPL) and then run main with the insertData invocation stripped out. If I >> run main with the insertData invocation included, it throws an exception >> (head) because the search results are empty. >> >> The behavior is as if queryTweet was executing after insertData deleted >> the index, but before it inserted the new data. >> >> The following is a stripped down example: >> >> insertData :: IO () >> insertData = do >> let encoded = encode exampleTweet >> _ <- deleteExampleIndex >> created <- createExampleIndex >> docCreated <- indexDocument (Server "http://localhost:9200") "twitter" >> "tweet" exampleTweet "1" >> print "test" >> return () >> el >> queryTweet :: IO (Either String Tweet) >> queryTweet = do >> let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") False) >> <||> IdentityFilter >> let search = Search Nothing (Just queryFilter) >> reply <- searchByIndex testServer "twitter" search >> let result = eitherDecode (responseBody reply) :: Either String >> (SearchResult Tweet) >> let myTweet = fmap (hitSource . head . hits . searchHits) result >> return myTweet >> >> main :: IO () >> main = do >> _ <- insertData >> myTweet <- queryTweet >> print myTweet >> >> further up the call chain, the http call is getting dispatched with >> http-conduit's gear, the result returned with the expression: >> >> withManager $ httpLbs req >> >> Included this in case it affects the semantics. >> >> Can anybody help? This has stumped me for a couple hours and I couldn't >> get anything clueful on IRC. >> >> Thanks to any that help. >> >> --- Chris >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu Apr 10 06:25:02 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 10 Apr 2014 01:25:02 -0500 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: It's a step removed from that, and `seq`'ing insertData isn't fixing it, only a threadDelay is. The operations insertData is performing are: delete index, create index, add document to index the operation queryTweet is performing is: search index The behavior seems to indicate "search index" is happening before "add document to index" and after "delete index", as the document will not exist when the query is performed. Only adding a threadDelay as reliably worked. Even `seq`'ing insertData doesn't work. Are the requests somehow being performed concurrently? On Thu, Apr 10, 2014 at 1:15 AM, Michael Snoyman wrote: > I can't say I fully understand your code, but it seems like it's doing the > following: > > 1. Send request to server to delete data. > 2. Send request to server to add data. > 3. Request data from server. > > You're saying that with a long enough delay between steps 2 and 3, then > (3) works, otherwise it fails. It sounds to me like there's some kind of a > race condition. I don't know anything about createExampleIndex, but are you > certain that it only returns after the data is completely ready for > querying? > > > On Thu, Apr 10, 2014 at 8:43 AM, Christopher Allen wrote: > >> to add to my question adding a delay made it work: >> >> main :: IO () >> main = do >> _ <- insertData >> threadDelay 1000000 >> myTweet <- queryTweet >> print myTweet >> >> >> >> On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen wrote: >> >>> Repro'd on GHC 7.8.1 and 7.6.3 >>> >>> Original code is: https://github.com/bitemyapp/bloodhound/ >>> >>> The code only works if I manually (out of band) run insertData (in a >>> REPL) and then run main with the insertData invocation stripped out. If I >>> run main with the insertData invocation included, it throws an exception >>> (head) because the search results are empty. >>> >>> The behavior is as if queryTweet was executing after insertData deleted >>> the index, but before it inserted the new data. >>> >>> The following is a stripped down example: >>> >>> insertData :: IO () >>> insertData = do >>> let encoded = encode exampleTweet >>> _ <- deleteExampleIndex >>> created <- createExampleIndex >>> docCreated <- indexDocument (Server "http://localhost:9200") >>> "twitter" "tweet" exampleTweet "1" >>> print "test" >>> return () >>> el >>> queryTweet :: IO (Either String Tweet) >>> queryTweet = do >>> let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") >>> False) >>> <||> IdentityFilter >>> let search = Search Nothing (Just queryFilter) >>> reply <- searchByIndex testServer "twitter" search >>> let result = eitherDecode (responseBody reply) :: Either String >>> (SearchResult Tweet) >>> let myTweet = fmap (hitSource . head . hits . searchHits) result >>> return myTweet >>> >>> main :: IO () >>> main = do >>> _ <- insertData >>> myTweet <- queryTweet >>> print myTweet >>> >>> further up the call chain, the http call is getting dispatched with >>> http-conduit's gear, the result returned with the expression: >>> >>> withManager $ httpLbs req >>> >>> Included this in case it affects the semantics. >>> >>> Can anybody help? This has stumped me for a couple hours and I couldn't >>> get anything clueful on IRC. >>> >>> Thanks to any that help. >>> >>> --- Chris >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu Apr 10 06:25:38 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 10 Apr 2014 01:25:38 -0500 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: the function actually running the requests is: dispatch :: String -> Method -> Maybe L.ByteString -> IO Reply dispatch url method body = do initReq <- parseUrl url let reqBody = RequestBodyLBS $ fromMaybe emptyBody body let req = initReq { method = method , requestBody = reqBody , checkStatus = \_ _ _ -> Nothing} withManager $ httpLbs req rest of the code is here: https://github.com/bitemyapp/bloodhound/ On Thu, Apr 10, 2014 at 1:25 AM, Christopher Allen wrote: > It's a step removed from that, and `seq`'ing insertData isn't fixing it, > only a threadDelay is. > > The operations insertData is performing are: > delete index, create index, add document to index > > the operation queryTweet is performing is: > search index > > The behavior seems to indicate "search index" is happening before "add > document to index" and after "delete index", as the document will not exist > when the query is performed. > > Only adding a threadDelay as reliably worked. Even `seq`'ing insertData > doesn't work. > > Are the requests somehow being performed concurrently? > > > > On Thu, Apr 10, 2014 at 1:15 AM, Michael Snoyman wrote: > >> I can't say I fully understand your code, but it seems like it's doing >> the following: >> >> 1. Send request to server to delete data. >> 2. Send request to server to add data. >> 3. Request data from server. >> >> You're saying that with a long enough delay between steps 2 and 3, then >> (3) works, otherwise it fails. It sounds to me like there's some kind of a >> race condition. I don't know anything about createExampleIndex, but are you >> certain that it only returns after the data is completely ready for >> querying? >> >> >> On Thu, Apr 10, 2014 at 8:43 AM, Christopher Allen wrote: >> >>> to add to my question adding a delay made it work: >>> >>> main :: IO () >>> main = do >>> _ <- insertData >>> threadDelay 1000000 >>> myTweet <- queryTweet >>> print myTweet >>> >>> >>> >>> On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen wrote: >>> >>>> Repro'd on GHC 7.8.1 and 7.6.3 >>>> >>>> Original code is: https://github.com/bitemyapp/bloodhound/ >>>> >>>> The code only works if I manually (out of band) run insertData (in a >>>> REPL) and then run main with the insertData invocation stripped out. If I >>>> run main with the insertData invocation included, it throws an exception >>>> (head) because the search results are empty. >>>> >>>> The behavior is as if queryTweet was executing after insertData deleted >>>> the index, but before it inserted the new data. >>>> >>>> The following is a stripped down example: >>>> >>>> insertData :: IO () >>>> insertData = do >>>> let encoded = encode exampleTweet >>>> _ <- deleteExampleIndex >>>> created <- createExampleIndex >>>> docCreated <- indexDocument (Server "http://localhost:9200") >>>> "twitter" "tweet" exampleTweet "1" >>>> print "test" >>>> return () >>>> el >>>> queryTweet :: IO (Either String Tweet) >>>> queryTweet = do >>>> let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") >>>> False) >>>> <||> IdentityFilter >>>> let search = Search Nothing (Just queryFilter) >>>> reply <- searchByIndex testServer "twitter" search >>>> let result = eitherDecode (responseBody reply) :: Either String >>>> (SearchResult Tweet) >>>> let myTweet = fmap (hitSource . head . hits . searchHits) result >>>> return myTweet >>>> >>>> main :: IO () >>>> main = do >>>> _ <- insertData >>>> myTweet <- queryTweet >>>> print myTweet >>>> >>>> further up the call chain, the http call is getting dispatched with >>>> http-conduit's gear, the result returned with the expression: >>>> >>>> withManager $ httpLbs req >>>> >>>> Included this in case it affects the semantics. >>>> >>>> Can anybody help? This has stumped me for a couple hours and I couldn't >>>> get anything clueful on IRC. >>>> >>>> Thanks to any that help. >>>> >>>> --- Chris >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at snoyman.com Thu Apr 10 06:28:32 2014 From: michael at snoyman.com (Michael Snoyman) Date: Thu, 10 Apr 2014 09:28:32 +0300 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: http-conduit does not perform any kind of automatic concurrency. httpLbs will only return after it has fully read the response body from the server. So unless there's a bug somewhere in http-conduit, it seems that the server has not fully updated its index by the time it gives back a response. On Thu, Apr 10, 2014 at 9:25 AM, Christopher Allen wrote: > the function actually running the requests is: > > dispatch :: String -> Method -> Maybe L.ByteString > -> IO Reply > dispatch url method body = do > initReq <- parseUrl url > let reqBody = RequestBodyLBS $ fromMaybe emptyBody body > let req = initReq { method = method > , requestBody = reqBody > , checkStatus = \_ _ _ -> Nothing} > withManager $ httpLbs req > > rest of the code is here: https://github.com/bitemyapp/bloodhound/ > > > On Thu, Apr 10, 2014 at 1:25 AM, Christopher Allen wrote: > >> It's a step removed from that, and `seq`'ing insertData isn't fixing it, >> only a threadDelay is. >> >> The operations insertData is performing are: >> delete index, create index, add document to index >> >> the operation queryTweet is performing is: >> search index >> >> The behavior seems to indicate "search index" is happening before "add >> document to index" and after "delete index", as the document will not exist >> when the query is performed. >> >> Only adding a threadDelay as reliably worked. Even `seq`'ing insertData >> doesn't work. >> >> Are the requests somehow being performed concurrently? >> >> >> >> On Thu, Apr 10, 2014 at 1:15 AM, Michael Snoyman wrote: >> >>> I can't say I fully understand your code, but it seems like it's doing >>> the following: >>> >>> 1. Send request to server to delete data. >>> 2. Send request to server to add data. >>> 3. Request data from server. >>> >>> You're saying that with a long enough delay between steps 2 and 3, then >>> (3) works, otherwise it fails. It sounds to me like there's some kind of a >>> race condition. I don't know anything about createExampleIndex, but are you >>> certain that it only returns after the data is completely ready for >>> querying? >>> >>> >>> On Thu, Apr 10, 2014 at 8:43 AM, Christopher Allen wrote: >>> >>>> to add to my question adding a delay made it work: >>>> >>>> main :: IO () >>>> main = do >>>> _ <- insertData >>>> threadDelay 1000000 >>>> myTweet <- queryTweet >>>> print myTweet >>>> >>>> >>>> >>>> On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen wrote: >>>> >>>>> Repro'd on GHC 7.8.1 and 7.6.3 >>>>> >>>>> Original code is: https://github.com/bitemyapp/bloodhound/ >>>>> >>>>> The code only works if I manually (out of band) run insertData (in a >>>>> REPL) and then run main with the insertData invocation stripped out. If I >>>>> run main with the insertData invocation included, it throws an exception >>>>> (head) because the search results are empty. >>>>> >>>>> The behavior is as if queryTweet was executing after insertData >>>>> deleted the index, but before it inserted the new data. >>>>> >>>>> The following is a stripped down example: >>>>> >>>>> insertData :: IO () >>>>> insertData = do >>>>> let encoded = encode exampleTweet >>>>> _ <- deleteExampleIndex >>>>> created <- createExampleIndex >>>>> docCreated <- indexDocument (Server "http://localhost:9200") >>>>> "twitter" "tweet" exampleTweet "1" >>>>> print "test" >>>>> return () >>>>> el >>>>> queryTweet :: IO (Either String Tweet) >>>>> queryTweet = do >>>>> let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") >>>>> False) >>>>> <||> IdentityFilter >>>>> let search = Search Nothing (Just queryFilter) >>>>> reply <- searchByIndex testServer "twitter" search >>>>> let result = eitherDecode (responseBody reply) :: Either String >>>>> (SearchResult Tweet) >>>>> let myTweet = fmap (hitSource . head . hits . searchHits) result >>>>> return myTweet >>>>> >>>>> main :: IO () >>>>> main = do >>>>> _ <- insertData >>>>> myTweet <- queryTweet >>>>> print myTweet >>>>> >>>>> further up the call chain, the http call is getting dispatched with >>>>> http-conduit's gear, the result returned with the expression: >>>>> >>>>> withManager $ httpLbs req >>>>> >>>>> Included this in case it affects the semantics. >>>>> >>>>> Can anybody help? This has stumped me for a couple hours and I >>>>> couldn't get anything clueful on IRC. >>>>> >>>>> Thanks to any that help. >>>>> >>>>> --- Chris >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu Apr 10 06:40:07 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 10 Apr 2014 01:40:07 -0500 Subject: [Haskell-beginners] Nonsensical behavior with http-conduit, httpLbs In-Reply-To: References: Message-ID: Yeap, it was elasticsearch. Had to call http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.htmlto force update on indexing of documents. Sorry to bother everybody ;_; Thank you so much for your help. Have a great day :) --- Chris On Thu, Apr 10, 2014 at 1:28 AM, Michael Snoyman wrote: > http-conduit does not perform any kind of automatic concurrency. httpLbs > will only return after it has fully read the response body from the server. > So unless there's a bug somewhere in http-conduit, it seems that the server > has not fully updated its index by the time it gives back a response. > > > On Thu, Apr 10, 2014 at 9:25 AM, Christopher Allen wrote: > >> the function actually running the requests is: >> >> dispatch :: String -> Method -> Maybe L.ByteString >> -> IO Reply >> dispatch url method body = do >> initReq <- parseUrl url >> let reqBody = RequestBodyLBS $ fromMaybe emptyBody body >> let req = initReq { method = method >> , requestBody = reqBody >> , checkStatus = \_ _ _ -> Nothing} >> withManager $ httpLbs req >> >> rest of the code is here: https://github.com/bitemyapp/bloodhound/ >> >> >> On Thu, Apr 10, 2014 at 1:25 AM, Christopher Allen wrote: >> >>> It's a step removed from that, and `seq`'ing insertData isn't fixing it, >>> only a threadDelay is. >>> >>> The operations insertData is performing are: >>> delete index, create index, add document to index >>> >>> the operation queryTweet is performing is: >>> search index >>> >>> The behavior seems to indicate "search index" is happening before "add >>> document to index" and after "delete index", as the document will not exist >>> when the query is performed. >>> >>> Only adding a threadDelay as reliably worked. Even `seq`'ing insertData >>> doesn't work. >>> >>> Are the requests somehow being performed concurrently? >>> >>> >>> >>> On Thu, Apr 10, 2014 at 1:15 AM, Michael Snoyman wrote: >>> >>>> I can't say I fully understand your code, but it seems like it's doing >>>> the following: >>>> >>>> 1. Send request to server to delete data. >>>> 2. Send request to server to add data. >>>> 3. Request data from server. >>>> >>>> You're saying that with a long enough delay between steps 2 and 3, then >>>> (3) works, otherwise it fails. It sounds to me like there's some kind of a >>>> race condition. I don't know anything about createExampleIndex, but are you >>>> certain that it only returns after the data is completely ready for >>>> querying? >>>> >>>> >>>> On Thu, Apr 10, 2014 at 8:43 AM, Christopher Allen wrote: >>>> >>>>> to add to my question adding a delay made it work: >>>>> >>>>> main :: IO () >>>>> main = do >>>>> _ <- insertData >>>>> threadDelay 1000000 >>>>> myTweet <- queryTweet >>>>> print myTweet >>>>> >>>>> >>>>> >>>>> On Thu, Apr 10, 2014 at 12:41 AM, Christopher Allen >>>> > wrote: >>>>> >>>>>> Repro'd on GHC 7.8.1 and 7.6.3 >>>>>> >>>>>> Original code is: https://github.com/bitemyapp/bloodhound/ >>>>>> >>>>>> The code only works if I manually (out of band) run insertData (in a >>>>>> REPL) and then run main with the insertData invocation stripped out. If I >>>>>> run main with the insertData invocation included, it throws an exception >>>>>> (head) because the search results are empty. >>>>>> >>>>>> The behavior is as if queryTweet was executing after insertData >>>>>> deleted the index, but before it inserted the new data. >>>>>> >>>>>> The following is a stripped down example: >>>>>> >>>>>> insertData :: IO () >>>>>> insertData = do >>>>>> let encoded = encode exampleTweet >>>>>> _ <- deleteExampleIndex >>>>>> created <- createExampleIndex >>>>>> docCreated <- indexDocument (Server "http://localhost:9200") >>>>>> "twitter" "tweet" exampleTweet "1" >>>>>> print "test" >>>>>> return () >>>>>> el >>>>>> queryTweet :: IO (Either String Tweet) >>>>>> queryTweet = do >>>>>> let queryFilter = BoolFilter (MustMatch (Term "user" "bitemyapp") >>>>>> False) >>>>>> <||> IdentityFilter >>>>>> let search = Search Nothing (Just queryFilter) >>>>>> reply <- searchByIndex testServer "twitter" search >>>>>> let result = eitherDecode (responseBody reply) :: Either String >>>>>> (SearchResult Tweet) >>>>>> let myTweet = fmap (hitSource . head . hits . searchHits) result >>>>>> return myTweet >>>>>> >>>>>> main :: IO () >>>>>> main = do >>>>>> _ <- insertData >>>>>> myTweet <- queryTweet >>>>>> print myTweet >>>>>> >>>>>> further up the call chain, the http call is getting dispatched with >>>>>> http-conduit's gear, the result returned with the expression: >>>>>> >>>>>> withManager $ httpLbs req >>>>>> >>>>>> Included this in case it affects the semantics. >>>>>> >>>>>> Can anybody help? This has stumped me for a couple hours and I >>>>>> couldn't get anything clueful on IRC. >>>>>> >>>>>> Thanks to any that help. >>>>>> >>>>>> --- Chris >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stijnmuylle at gmail.com Thu Apr 10 07:48:44 2014 From: stijnmuylle at gmail.com (Stijn Muylle) Date: Thu, 10 Apr 2014 09:48:44 +0200 Subject: [Haskell-beginners] Guards problem Message-ID: All, I ran into a small problem using guards, and I don't really understand what I'm doing wrong. I have this code: ---------------------------------- import Data.List.Split type Cell = Bool data Board = Board [[Cell]] deriving Show trueList = True : trueList createBoard :: Int -> Int -> Board createBoard x y = Board (chunksOf x (take (x * y) trueList)) getCellAt :: Int -> Int -> Board -> Cell getCellAt x y (Board b) | x >= (length b) = False | y >= (length (b !! 0)) = False | otherwise = (b !! x) !! y ----------------------------------- When I try to execute this: getCellAt (-1) 0 $ createBoard 3 3 I get a negative index exception. However, as -1 is smaller than 3 (length of the board), I would expect False to be returned. Am I overlooking something? Thanks! Stijn -------------- next part -------------- An HTML attachment was scrubbed... URL: From magnus at therning.org Thu Apr 10 07:58:17 2014 From: magnus at therning.org (Magnus Therning) Date: Thu, 10 Apr 2014 09:58:17 +0200 Subject: [Haskell-beginners] Guards problem In-Reply-To: References: Message-ID: On Thu, Apr 10, 2014 at 9:48 AM, Stijn Muylle wrote: > > All, > > I ran into a small problem using guards, and I don't really understand what > I'm doing wrong. > > I have this code: > > ---------------------------------- > import Data.List.Split > type Cell = Bool > data Board = Board [[Cell]] deriving Show > > trueList = True : trueList > > createBoard :: Int -> Int -> Board > createBoard x y = Board (chunksOf x (take (x * y) trueList)) > > getCellAt :: Int -> Int -> Board -> Cell > getCellAt x y (Board b) > | x >= (length b) = False > | y >= (length (b !! 0)) = False > | otherwise = (b !! x) !! y > > ----------------------------------- > > When I try to execute this: > > getCellAt (-1) 0 $ createBoard 3 3 > > I get a negative index exception. However, as -1 is smaller than 3 (length > of the board), I would expect False to be returned. > > Am I overlooking something? I might be looking at it wrong, but won't your code go through 1. -1(x) >= 3(length b) ---> not true, so check next guard 2. 0(y) >= 3(length $ b!! 0) ---> not true, so check next guard 3. True(otherwise) ---> true /M From dserban01 at gmail.com Thu Apr 10 11:23:00 2014 From: dserban01 at gmail.com (Dan Serban) Date: Thu, 10 Apr 2014 14:23:00 +0300 Subject: [Haskell-beginners] Guards problem In-Reply-To: References: Message-ID: Side note: it's not very clear what you're trying to accomplish, but you may be better served by using array semantics. I sketched a quick and dirty code snippet in a hurry, feel free to clean it up and make it well-typed: https://gist.github.com/dserban/10370136 After loading it in GHCi, you can do: ?: getCellAt (1,1) True ?: getCellAt (-1,1) False From stijnmuylle at gmail.com Thu Apr 10 12:20:17 2014 From: stijnmuylle at gmail.com (Stijn Muylle) Date: Thu, 10 Apr 2014 14:20:17 +0200 Subject: [Haskell-beginners] Guards problem In-Reply-To: References: Message-ID: Found the problem: I had a bug in my implementation (see Marcus' comment). The solution provided by Dan (using a 2d array) seems better, but right now I'm just focussing on making it work ;) On Thu, Apr 10, 2014 at 9:48 AM, Stijn Muylle wrote: > > All, > > I ran into a small problem using guards, and I don't really understand > what I'm doing wrong. > > I have this code: > > ---------------------------------- > import Data.List.Split > type Cell = Bool > data Board = Board [[Cell]] deriving Show > > trueList = True : trueList > > createBoard :: Int -> Int -> Board > createBoard x y = Board (chunksOf x (take (x * y) trueList)) > > getCellAt :: Int -> Int -> Board -> Cell > getCellAt x y (Board b) > | x >= (length b) = False > | y >= (length (b !! 0)) = False > | otherwise = (b !! x) !! y > > ----------------------------------- > > When I try to execute this: > > getCellAt (-1) 0 $ createBoard 3 3 > > I get a negative index exception. However, as -1 is smaller than 3 (length > of the board), I would expect False to be returned. > > Am I overlooking something? > > Thanks! > Stijn > -------------- next part -------------- An HTML attachment was scrubbed... URL: From magnus at therning.org Thu Apr 10 21:19:16 2014 From: magnus at therning.org (Magnus Therning) Date: Thu, 10 Apr 2014 23:19:16 +0200 Subject: [Haskell-beginners] Guards problem In-Reply-To: References: Message-ID: <20140410211916.GA1977@tatooine.lan> On Thu, Apr 10, 2014 at 02:20:17PM +0200, Stijn Muylle wrote: > Found the problem: I had a bug in my implementation (see Marcus' comment). I think that should be "Magnus' comment" ;) /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus Heuristic is an algorithm in a clown suit. It?s less predictable, it?s more fun, and it comes without a 30-day, money-back guarantee. -- Steve McConnell, Code Complete -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From ngnr63q02 at sneakemail.com Fri Apr 11 00:12:25 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Thu, 10 Apr 2014 19:12:25 -0500 Subject: [Haskell-beginners] recursive 'let' ? Message-ID: I understand that the definitions introduced by 'let' can be recursive, even mutually recursive among several names. Why would you want to do that? I saw contrived examples, and wonder why the authors never show a realistic example. let b = f a c c = f a b in ... I see that makes sense in light of lazy evaluation: b is really an alias for a (recursive) function, not a value that needs to find fixed points. Is this used for common idioms and problem-solving approaches in Haskell? --John From karl at karlv.net Fri Apr 11 06:50:19 2014 From: karl at karlv.net (Karl Voelker) Date: Thu, 10 Apr 2014 23:50:19 -0700 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: References: Message-ID: <1397199019.2557.105291685.31667F31@webmail.messagingengine.com> On Thu, Apr 10, 2014, at 05:12 PM, John M. Dlugosz wrote: > I understand that the definitions introduced by 'let' can be recursive, > even mutually > recursive among several names. Why would you want to do that? I saw > contrived examples, > and wonder why the authors never show a realistic example. > > let b = f a c > c = f a b > in ... I am a bit confused by your question. I assume you know about the usefulness of recursion in general, and that your question is only about recursion in the context of a let-binding. But it seems to me that there is nothing particularly special about the let-binding context, as compared to, for example, the top level of a module. So what seems different about it to you? Your code snippet makes me think that perhaps you are specifically interested in definitions of non-function values. Is that so? -Karl From magnus at therning.org Fri Apr 11 15:28:15 2014 From: magnus at therning.org (Magnus Therning) Date: Fri, 11 Apr 2014 17:28:15 +0200 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: References: Message-ID: <20140411152815.GA975@tatooine.lan> On Thu, Apr 10, 2014 at 07:12:25PM -0500, John M. Dlugosz wrote: > I understand that the definitions introduced by 'let' can be > recursive, even mutually recursive among several names. Why would > you want to do that? I saw contrived examples, and wonder why the > authors never show a realistic example. > > let b = f a c > c = f a b > in ... > > I see that makes sense in light of lazy evaluation: b is really an > alias for a (recursive) function, not a value that needs to find > fixed points. > > Is this used for common idioms and problem-solving approaches in > Haskell? I can't really point to any idiomatic use, or problem-solving approaches, but it is a terribly useful feature since one of the effects is that the order of introducing functions/values isn't significant. So you are free to write and structure your code in the manner that makes most sense to you. Having just ventured back into OCaml and dipping my toes in F# I immediately ran into errors caused by their non-recursive `let`, and the requirement to introduce values/types before use. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus The results point out the fragility of programmer expertise: advanced programmers have strong expectations about what programs should look like, and when those expectations are violated--in seemingly innocuous ways--their performance drops drastically. -- Elliot Soloway and Kate Ehrlich -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From ngnr63q02 at sneakemail.com Fri Apr 11 16:25:27 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 11 Apr 2014 11:25:27 -0500 Subject: [Haskell-beginners] Windows x64 ? Message-ID: The Haskell Platform has a single download link for "Windows" which seems to be 32-bit only. But GHC is available for Win x64. Their site dissuades downloading their installation and recommends getting Haskell Platform instead. So, is there an easy way to get 64-bit versions of compiler or compiled executables so produced? From kd6ck at virginia.edu Fri Apr 11 16:50:45 2014 From: kd6ck at virginia.edu (ke dou) Date: Fri, 11 Apr 2014 12:50:45 -0400 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass Message-ID: Hi, I want to define a typeclass that can convert my own types like MyBool, MyInt, MyOption to according Haskell types -- Bool, Int, Maybe. Currently I can convert the first two types, but for MyOption, I don't how to do, since it is a polymophic type. Here is my toy program: -------------------------------------------------------------- {-# LANGUAGE TypeFamilies #-} module Coercible where import qualified Prelude data MyBool = MyTrue | MyFalse data MyInt = Zero | One | Two data MyOption a = Some a | None class Coercible a where type Return a :: * toHaskell :: a -> (Return a) instance Coercible MyBool where type Return MyBool = Prelude.Bool toHaskell MyTrue = Prelude.True toHaskell MyFalse = Prelude.False instance Coercible MyInt where type Return MyInt = Prelude.Int toHaskell Zero = 0 toHaskell One = 1 toHaskell Two = 2 instance Coercible (MyOption a) where type Return (MyOption a) = Prelude.Maybe a toHaskell (Some a) = Prelude.Just a toHaskell None = Prelude.Nothing -------------------------------------------------------------------------- The problem occurs when I am trying to use "toHaskell (Some MyBool)", the error message is "Not in scope: data constructor `MyBool'". Any hints will be appreciated !! Best, --Ke -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Fri Apr 11 17:05:03 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 11 Apr 2014 10:05:03 -0700 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass In-Reply-To: References: Message-ID: On Fri, Apr 11, 2014 at 9:50 AM, ke dou wrote: > Hi, > > I want to define a typeclass that can convert my own types like MyBool, > MyInt, MyOption to according Haskell types -- Bool, Int, Maybe. > > Currently I can convert the first two types, but for MyOption, I don't how > to do, since it is a polymophic type. > > Here is my toy program: > -------------------------------------------------------------- > {-# LANGUAGE TypeFamilies #-} > > module Coercible where > > import qualified Prelude > > data MyBool = MyTrue | MyFalse > data MyInt = Zero | One | Two > data MyOption a = > Some a > | None > > class Coercible a where > type Return a :: * > toHaskell :: a -> (Return a) > > instance Coercible MyBool where > type Return MyBool = Prelude.Bool > toHaskell MyTrue = Prelude.True > toHaskell MyFalse = Prelude.False > > instance Coercible MyInt where > type Return MyInt = Prelude.Int > toHaskell Zero = 0 > toHaskell One = 1 > toHaskell Two = 2 > > instance Coercible (MyOption a) where > type Return (MyOption a) = Prelude.Maybe a > toHaskell (Some a) = Prelude.Just a > toHaskell None = Prelude.Nothing > -------------------------------------------------------------------------- > The problem occurs when I am trying to use "toHaskell (Some MyBool)", the > error message is "Not in scope: data constructor `MyBool'". > Any hints will be appreciated !! > `Some MyBool` isn't valid Haskell. MyBool is the name of a type, there's no binding or constructor with that name. `toHaskell (Some MyTrue)` evaluates to `Just MyTrue` as expected. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kd6ck at virginia.edu Fri Apr 11 17:13:35 2014 From: kd6ck at virginia.edu (ke dou) Date: Fri, 11 Apr 2014 13:13:35 -0400 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass In-Reply-To: References: Message-ID: Thank you, Bob. I also noticed that, but don't know how to fix it. Any suggestions? --Ke On Fri, Apr 11, 2014 at 1:05 PM, Bob Ippolito wrote: > On Fri, Apr 11, 2014 at 9:50 AM, ke dou wrote: > >> Hi, >> >> I want to define a typeclass that can convert my own types like MyBool, >> MyInt, MyOption to according Haskell types -- Bool, Int, Maybe. >> >> Currently I can convert the first two types, but for MyOption, I don't >> how to do, since it is a polymophic type. >> >> Here is my toy program: >> -------------------------------------------------------------- >> {-# LANGUAGE TypeFamilies #-} >> >> module Coercible where >> >> import qualified Prelude >> >> data MyBool = MyTrue | MyFalse >> data MyInt = Zero | One | Two >> data MyOption a = >> Some a >> | None >> >> class Coercible a where >> type Return a :: * >> toHaskell :: a -> (Return a) >> >> instance Coercible MyBool where >> type Return MyBool = Prelude.Bool >> toHaskell MyTrue = Prelude.True >> toHaskell MyFalse = Prelude.False >> >> instance Coercible MyInt where >> type Return MyInt = Prelude.Int >> toHaskell Zero = 0 >> toHaskell One = 1 >> toHaskell Two = 2 >> >> instance Coercible (MyOption a) where >> type Return (MyOption a) = Prelude.Maybe a >> toHaskell (Some a) = Prelude.Just a >> toHaskell None = Prelude.Nothing >> -------------------------------------------------------------------------- >> The problem occurs when I am trying to use "toHaskell (Some MyBool)", the >> error message is "Not in scope: data constructor `MyBool'". >> Any hints will be appreciated !! >> > > `Some MyBool` isn't valid Haskell. MyBool is the name of a type, there's > no binding or constructor with that name. `toHaskell (Some MyTrue)` > evaluates to `Just MyTrue` as expected. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Fri Apr 11 17:22:29 2014 From: toad3k at gmail.com (David McBride) Date: Fri, 11 Apr 2014 13:22:29 -0400 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass In-Reply-To: References: Message-ID: There is nothing to fix. It is doing exactly as you intended. You are confusing types vs values. Integer is a type, 1, 2, and 3 are values. toHaskell takes a type (MyOption a) and converts it to a type (Maybe a). But in your actual code you will use values ie. toHaskell (Some 'a' :: MyOption Char) and get back a (Just 'a' :: Maybe Char). On Fri, Apr 11, 2014 at 1:13 PM, ke dou wrote: > Thank you, Bob. > I also noticed that, but don't know how to fix it. > Any suggestions? > > --Ke > > > On Fri, Apr 11, 2014 at 1:05 PM, Bob Ippolito wrote: > >> On Fri, Apr 11, 2014 at 9:50 AM, ke dou wrote: >> >>> Hi, >>> >>> I want to define a typeclass that can convert my own types like MyBool, >>> MyInt, MyOption to according Haskell types -- Bool, Int, Maybe. >>> >>> Currently I can convert the first two types, but for MyOption, I don't >>> how to do, since it is a polymophic type. >>> >>> Here is my toy program: >>> -------------------------------------------------------------- >>> {-# LANGUAGE TypeFamilies #-} >>> >>> module Coercible where >>> >>> import qualified Prelude >>> >>> data MyBool = MyTrue | MyFalse >>> data MyInt = Zero | One | Two >>> data MyOption a = >>> Some a >>> | None >>> >>> class Coercible a where >>> type Return a :: * >>> toHaskell :: a -> (Return a) >>> >>> instance Coercible MyBool where >>> type Return MyBool = Prelude.Bool >>> toHaskell MyTrue = Prelude.True >>> toHaskell MyFalse = Prelude.False >>> >>> instance Coercible MyInt where >>> type Return MyInt = Prelude.Int >>> toHaskell Zero = 0 >>> toHaskell One = 1 >>> toHaskell Two = 2 >>> >>> instance Coercible (MyOption a) where >>> type Return (MyOption a) = Prelude.Maybe a >>> toHaskell (Some a) = Prelude.Just a >>> toHaskell None = Prelude.Nothing >>> >>> -------------------------------------------------------------------------- >>> The problem occurs when I am trying to use "toHaskell (Some MyBool)", >>> the error message is "Not in scope: data constructor `MyBool'". >>> Any hints will be appreciated !! >>> >> >> `Some MyBool` isn't valid Haskell. MyBool is the name of a type, there's >> no binding or constructor with that name. `toHaskell (Some MyTrue)` >> evaluates to `Just MyTrue` as expected. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From magnus at therning.org Fri Apr 11 18:43:18 2014 From: magnus at therning.org (Magnus Therning) Date: Fri, 11 Apr 2014 20:43:18 +0200 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass In-Reply-To: References: Message-ID: <20140411184318.GB2086@tatooine.lan> On Fri, Apr 11, 2014 at 12:50:45PM -0400, ke dou wrote: > instance Coercible (MyOption a) where > type Return (MyOption a) = Prelude.Maybe a > toHaskell (Some a) = Prelude.Just a > toHaskell None = Prelude.Nothing The basic issue has been solved in another thread, but wouldn't you want this one to be something like this: instance Coercible a => Coercible (MyOption a) where type Return (MyOption a) = Prelude.Maybe (Return a) toHaskell (Some a) = Prelude.Just (toHaskell a) toHaskell None = Prelude.Nothing /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus What gets measured, gets done. -- Tom Peters -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From kd6ck at virginia.edu Fri Apr 11 20:15:48 2014 From: kd6ck at virginia.edu (ke dou) Date: Fri, 11 Apr 2014 16:15:48 -0400 Subject: [Haskell-beginners] Convert my own types to the Haskell types using typeclass In-Reply-To: <20140411184318.GB2086@tatooine.lan> References: <20140411184318.GB2086@tatooine.lan> Message-ID: Thank you very much! That is exactly what I want. Best, Ke On Fri, Apr 11, 2014 at 2:43 PM, Magnus Therning wrote: > On Fri, Apr 11, 2014 at 12:50:45PM -0400, ke dou wrote: > > instance Coercible (MyOption a) where > > type Return (MyOption a) = Prelude.Maybe a > > toHaskell (Some a) = Prelude.Just a > > toHaskell None = Prelude.Nothing > > The basic issue has been solved in another thread, but wouldn't you > want this one to be something like this: > > instance Coercible a => Coercible (MyOption a) where > type Return (MyOption a) = Prelude.Maybe (Return a) > toHaskell (Some a) = Prelude.Just (toHaskell a) > toHaskell None = Prelude.Nothing > > /M > > -- > Magnus Therning OpenPGP: 0xAB4DFBA4 > email: magnus at therning.org jabber: magnus at therning.org > twitter: magthe http://therning.org/magnus > > What gets measured, gets done. > -- Tom Peters > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Sat Apr 12 13:29:09 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 12 Apr 2014 08:29:09 -0500 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO Message-ID: This works: main = do myargs <- getArgs mapM_ (\s -> putStrLn s ) myargs and imagine that the "body" will be substantial rather than just a putStrLn. My gut instinct is that the code ought to be arranged as: and Meanwhile, there is no need to name the result of getArgs into myargs. So, getArgs is of type IO [String], and I want to apply that in the manner of a list. Without the Monad wrappers, plain map ( blah ) strings could be ( blah ) <$> strings, and in this particular case I don't see a reversed-arg version, although there is one for <*> (as <**>). But, for monad stuff in general there are reversed arrows for (most?) everything, and that's where I'm heading. So the first question is, how do I do the equivalent map-as-nondeterministic-apply when the strings is further wrapped in IO, as is the function being applied. getArgs >>= mapM_ (\s -> putStrLn s ) does double-duty of moving the argument from last place to the left, as it makes use of eta reduction. Because I have two things going on (list applicative and IO monad) I'm losing the slickness of using applicative syntax. Is there a nice way to make these work together? And more generally, how would you write such a construct? I'm naturally biased with my knowledge in other languages, so maybe there's a completely different "normal" way of approaching this? Thanks, -John From ngnr63q02 at sneakemail.com Sun Apr 13 04:02:19 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 12 Apr 2014 23:02:19 -0500 Subject: [Haskell-beginners] what is "Gesh"? (Re: function not working as expected, type issues) In-Reply-To: References: Message-ID: On 4/9/2014 3:45 AM, Gesh wrote: > Thank you for explaining the problem you're trying to solve instead of Gesh > problem you're facing. Is that some jargon I'm missing? Hmm, I see that's your name and email address too. From magnus at therning.org Sun Apr 13 06:48:35 2014 From: magnus at therning.org (Magnus Therning) Date: Sun, 13 Apr 2014 08:48:35 +0200 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: Message-ID: <20140413064835.GC1008@tatooine.lan> On Sat, Apr 12, 2014 at 08:29:09AM -0500, John M. Dlugosz wrote: > This works: > > main = do > myargs <- getArgs > mapM_ (\s -> putStrLn s ) myargs > > and imagine that the "body" will be substantial rather than just a putStrLn. > My gut instinct is that the code ought to be arranged as: > > and > ... > ... > > > > Meanwhile, there is no need to name the result of getArgs into myargs. > > So, getArgs is of type IO [String], and I want to apply that in the > manner of a list. Without the Monad wrappers, plain > map ( blah ) strings > could be ( blah ) <$> strings, and in this particular case I don't > see a reversed-arg version, although there is one for <*> (as <**>). > But, for monad stuff in general there are reversed arrows for > (most?) everything, and that's where I'm heading. > > So the first question is, how do I do the equivalent > map-as-nondeterministic-apply when the strings is further wrapped in > IO, as is the function being applied. > > getArgs >>= mapM_ (\s -> putStrLn s ) > > does double-duty of moving the argument from last place to the left, > as it makes use of eta reduction. Because I have two things going > on (list applicative and IO monad) I'm losing the slickness of using > applicative syntax. Is there a nice way to make these work > together? > > And more generally, how would you write such a construct? I'm > naturally biased with my knowledge in other languages, so maybe > there's a completely different "normal" way of approaching this? I'm not entirely sure I get what you are asking for, but I'll take a stab and just let me know if I'm completely off the mark. If all you want is keep the 'string generator' on the right-hand side, then you have (=<<): mapM_ putStrLn =<< getArgs Personally I often like keeping the 'string generator' on the left (i.e. using (>>=) because when the expression to be mapped grows it allows this structuring of the code: getArgs >>= mapM_ $ \ s -> do ... /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus Perl is another example of filling a tiny, short-term need, and then being a real problem in the longer term. -- Alan Kay -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From etouzery at gmail.com Sun Apr 13 08:21:12 2014 From: etouzery at gmail.com (Emmanuel Touzery) Date: Sun, 13 Apr 2014 10:21:12 +0200 Subject: [Haskell-beginners] parsing exif file: binary, binary-strict, or cereal? Message-ID: Hello, I could not find a pure haskell library to parse EXIF so I wrote one (pretty basic so far): https://github.com/emmanueltouzery/hsexif I wrote it with binary-strict. I also considered binary, but I would like the library not to throw an exception if the file that it's given is not a JPEG or is a JPEG without EXIF, but rather return an Either. I didn't manage to do that with binary (I mean I could offer an IO monad wrapper with try* but to do that even if you give me a lazy bytestring, when it could be done only with pure code, is annoying). It annoys me to load the full JPEG in memory just to parse its EXIF though. Then again with binary I'd have to be pretty careful with strictness annotations and so on, although parsing the EXIF is going to be fast and it won't contain that much data. Still, I want the lazy bytestring holding the JPEG file to be released ASAP. It'd be a shame to have it lying in memory, together with holding the JPEG file open until the library caller actually makes use of the EXIF contents... And then I realized that binary-strict was last updated in 2010, it seems cereal is recommended for strict binary file parsing nowadays. So, what should I do? Leave it as it is? Port to cereal? Port to binary? Otherwise I did write this because I'd like to make a program to parse EXIF file which will have to run also on windows. The haskell exif parsing library that I could find uses the C library libexif, which is going to get complicated to get running on windows. Plus it was fun :-) Thanks, Emmanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Sun Apr 13 13:40:04 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Sun, 13 Apr 2014 09:40:04 -0400 Subject: [Haskell-beginners] what is "Gesh"? (Re: function not working as expected, type issues) In-Reply-To: References: Message-ID: On Sun, Apr 13, 2014 at 12:02 AM, John M. Dlugosz wrote: > On 4/9/2014 3:45 AM, Gesh wrote: > >> Thank you for explaining the problem you're trying to solve instead of >> Gesh >> problem you're facing. >> > > Is that some jargon I'm missing? Hmm, I see that's your name and email > address too. > I think it's just different mailers misinterpreting and badly wrapping different quoting styles. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Sun Apr 13 13:55:37 2014 From: gesh at gesh.uni.cx (Gesh) Date: Sun, 13 Apr 2014 16:55:37 +0300 Subject: [Haskell-beginners] what is "Gesh"? (Re: function not working as expected, type issues) In-Reply-To: References: Message-ID: On April 13, 2014 7:02:19 AM GMT+03:00, "John M. Dlugosz" wrote: >On 4/9/2014 3:45 AM, Gesh wrote: >> Thank you for explaining the problem you're trying to solve instead >of Gesh >> problem you're facing. > >Is that some jargon I'm missing? Hmm, I see that's your name and email >address too. > > >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners Sorry, my autocorrect must have put that in instead of "the". And it's the nickname I use, not jargon. Sorry. From gale at sefer.org Sun Apr 13 14:30:48 2014 From: gale at sefer.org (Yitzchak Gale) Date: Sun, 13 Apr 2014 17:30:48 +0300 Subject: [Haskell-beginners] parsing exif file: binary, binary-strict, or cereal? In-Reply-To: References: Message-ID: Emmanuel Touzery wrote: > I could not find a pure haskell library to parse EXIF so I wrote one > (pretty basic so far): > https://github.com/emmanueltouzery/hsexif Very nice! Why not upload it to hackage to make it easier for others to share and collaborate? > I wrote it with binary-strict. I also considered binary, but I would like > the library not to throw an exception if the file that it's given is not a > JPEG or is a JPEG without EXIF, but rather return an Either. I didn't manage > to do that with binary Perhaps the function Data.Binary.Get.runGetOrFail is what you are looking for? Or perhaps the incremental strict interface? > And then I realized that binary-strict was last updated in 2010, it seems > cereal is recommended for strict binary file parsing nowadays. > So, what should I do? Leave it as it is? Port to cereal? Port to binary? The binary-strict library is no longer maintained, since binary now also provides a strict interface. Both binary and cereal are good choices. Probably porting to binary would be easiest if you've already written it for binary-strict, assuming runGetOrFail or the incremental interface does what you want. Regards, Yitz From etouzery at gmail.com Sun Apr 13 15:08:27 2014 From: etouzery at gmail.com (Emmanuel Touzery) Date: Sun, 13 Apr 2014 17:08:27 +0200 Subject: [Haskell-beginners] parsing exif file: binary, binary-strict, or cereal? In-Reply-To: References: Message-ID: On Sun, Apr 13, 2014 at 4:30 PM, Yitzchak Gale wrote: > Emmanuel Touzery wrote: > > I could not find a pure haskell library to parse EXIF so I wrote one > > (pretty basic so far): > > https://github.com/emmanueltouzery/hsexif > > Very nice! Why not upload it to hackage to make it easier > for others to share and collaborate? > Yes, that's the plan as soon as I get the basics right and that I write some short documentation. Hopefully very quickly. > > > I wrote it with binary-strict. I also considered binary, but I would like > > the library not to throw an exception if the file that it's given is not > a > > JPEG or is a JPEG without EXIF, but rather return an Either. I didn't > manage > > to do that with binary > > Perhaps the function Data.Binary.Get.runGetOrFail is what > you are looking for? Or perhaps the incremental strict interface? > Ah yes... Actually I was a bit mislead because I found an haskell wiki stating that with binary it was impossible to catch the exceptions except in the IO monad and I took that at face value. I think runGetOrFail is what I want, I'll test it, thank you! > > > And then I realized that binary-strict was last updated in 2010, it > seems > > cereal is recommended for strict binary file parsing nowadays. > > So, what should I do? Leave it as it is? Port to cereal? Port to binary? > > The binary-strict library is no longer maintained, since binary now also > provides a strict interface. Both binary and cereal are good choices. > > Probably porting to binary would be easiest if you've already written > it for binary-strict, assuming runGetOrFail or the incremental interface > does what you want. > Ok, then I'll try the non-strict binary first then. Most JPG files will be only a couple of megabytes and it wouldn't be THAT bad loading them entirely in memory but then, it seems a bit of a shame. With lazy though I'll have to work a bit on my strictness annotations I think. Thanks for the hints! Hopefully I can port it to lazy binary and publish it on hackage soon enough... Emmanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From etouzery at gmail.com Sun Apr 13 15:18:29 2014 From: etouzery at gmail.com (Emmanuel Touzery) Date: Sun, 13 Apr 2014 17:18:29 +0200 Subject: [Haskell-beginners] parsing exif file: binary, binary-strict, or cereal? In-Reply-To: References: Message-ID: well, it went fast :-) already ported. regarding strictness, i hope just doing that is enough: data IfEntry = IfEntry { entryTag :: !Word16, entryFormat :: !Word16, entryNoComponents :: !Word32, entryContents :: !Word32 } deriving Show Though from past experience, I'm far from sure. I'll add comments soon and then push it to hackage. emmanuel On Sun, Apr 13, 2014 at 5:08 PM, Emmanuel Touzery wrote: > > > > On Sun, Apr 13, 2014 at 4:30 PM, Yitzchak Gale wrote: > >> Emmanuel Touzery wrote: >> > I could not find a pure haskell library to parse EXIF so I wrote one >> > (pretty basic so far): >> > https://github.com/emmanueltouzery/hsexif >> >> Very nice! Why not upload it to hackage to make it easier >> for others to share and collaborate? >> > > > Yes, that's the plan as soon as I get the basics right and that I write > some short documentation. Hopefully very quickly. > > >> >> > I wrote it with binary-strict. I also considered binary, but I would >> like >> > the library not to throw an exception if the file that it's given is >> not a >> > JPEG or is a JPEG without EXIF, but rather return an Either. I didn't >> manage >> > to do that with binary >> >> Perhaps the function Data.Binary.Get.runGetOrFail is what >> you are looking for? Or perhaps the incremental strict interface? >> > > Ah yes... Actually I was a bit mislead because I found an haskell wiki > stating that with binary it was impossible to catch the exceptions except > in the IO monad and I took that at face value. I think runGetOrFail is what > I want, I'll test it, thank you! > > >> >> > And then I realized that binary-strict was last updated in 2010, it >> seems >> > cereal is recommended for strict binary file parsing nowadays. >> > So, what should I do? Leave it as it is? Port to cereal? Port to >> binary? >> >> The binary-strict library is no longer maintained, since binary now also >> provides a strict interface. Both binary and cereal are good choices. >> >> Probably porting to binary would be easiest if you've already written >> it for binary-strict, assuming runGetOrFail or the incremental interface >> does what you want. >> > > Ok, then I'll try the non-strict binary first then. Most JPG files will be > only a couple of megabytes and it wouldn't be THAT bad loading them > entirely in memory but then, it seems a bit of a shame. With lazy though > I'll have to work a bit on my strictness annotations I think. > > Thanks for the hints! Hopefully I can port it to lazy binary and publish > it on hackage soon enough... > > Emmanuel > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Mon Apr 14 06:03:20 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Mon, 14 Apr 2014 00:03:20 -0600 Subject: [Haskell-beginners] How do I use Guards in record syntax? Message-ID: <534B7A28.1020303@ucdavis.edu> I'm having some trouble understanding where I can or cannot use guards inside record syntax. I'm writing a simple conversion routine, but I am not able to write it without inserting an extra let. Do I need a let expression here? Am I missing something? -------------- data OldTrade = OldTrade { oldprice :: Double , oldamount :: Double , oldbuysell :: String -- "buy", "sell" or "" } deriving( Eq, Show) data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show) data Trade = Trade { price :: Double , amount :: Double , buysell :: BuyOrSell } deriving( Eq, Show) convert :: OldTrade -> Trade convert ot = Trade { price = oldprice ot, amount = oldamount ot, buysell = let x | oldbuysell ot == "buy" = Buy | oldbuysell ot == "sell" = Sell | otherwise = Unknown in x } -- how do I eliminate the 'let' expression here? -- I wanted to write something like: -- -- buysell | oldbuysell ot == "buy" = Buy -- | oldbuysell ot == "sell" = Sell -- | otherwise = Unknown -------------- Thanks! Dimitri From bob at redivi.com Mon Apr 14 07:35:58 2014 From: bob at redivi.com (Bob Ippolito) Date: Mon, 14 Apr 2014 00:35:58 -0700 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: <534B7A28.1020303@ucdavis.edu> References: <534B7A28.1020303@ucdavis.edu> Message-ID: You can't use guards in record syntax, but you could use `case` here instead of `let`, or just write a function of type `String -> BuyOrSell` and use that. On Sun, Apr 13, 2014 at 11:03 PM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > > I'm having some trouble understanding where I can or cannot use guards > inside record syntax. I'm writing a simple conversion routine, but I am not > able to write it without inserting an extra let. Do I need a let expression > here? Am I missing something? > > -------------- > data OldTrade = OldTrade { > oldprice :: Double , > oldamount :: Double , > oldbuysell :: String -- "buy", "sell" or "" > } deriving( Eq, Show) > > > data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show) > > data Trade = Trade { > price :: Double , > amount :: Double , > buysell :: BuyOrSell > } deriving( Eq, Show) > > convert :: OldTrade -> Trade > > convert ot = Trade { price = oldprice ot, > amount = oldamount ot, > buysell = let x | oldbuysell ot == "buy" = Buy > | oldbuysell ot == "sell" = Sell > | otherwise = Unknown > in x > } > > -- how do I eliminate the 'let' expression here? > -- I wanted to write something like: > -- > -- buysell | oldbuysell ot == "buy" = Buy > -- | oldbuysell ot == "sell" = Sell > -- | otherwise = Unknown > > -------------- > > Thanks! > > Dimitri > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.john.wheeler at gmail.com Mon Apr 14 09:34:47 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Mon, 14 Apr 2014 04:34:47 -0500 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: <20140413064835.GC1008@tatooine.lan> References: <20140413064835.GC1008@tatooine.lan> Message-ID: @John It looks like you are looking for `forM_` from Control.Monad: http://hackage.haskell.org/package/base-4.7.0.0/docs/Control-Monad.html#v:forM_ That way you can write code that looks like: main = do myargs <- getArgs forM_ myargs $ \s -> do putStrLn s putStrLn $ "Second string" ++ s This is just `mapM_` with the its two parameters fliped. `forM_` is defined in a an idiomatic fashion, with the `flip`(prelude function) function. Definition of forM_ http://hackage.haskell.org/package/base-4.7.0.0/docs/src/Control-Monad.html#forM_ Magnus solution has been used so often by some people that some have created idioms around it. om f m x = m >>= flip f x main = do om forM_ getArgs $ \s -> do putStrLn s putStrLn $ "Second string: " ++ s You can read the discussion around om: http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/ Hope that helps. Patrick On Sun, Apr 13, 2014 at 1:48 AM, Magnus Therning wrote: > On Sat, Apr 12, 2014 at 08:29:09AM -0500, John M. Dlugosz wrote: > > This works: > > > > main = do > > myargs <- getArgs > > mapM_ (\s -> putStrLn s ) myargs > > > > and imagine that the "body" will be substantial rather than just a > putStrLn. > > My gut instinct is that the code ought to be arranged as: > > > > and > > > ... > > ... > > > > > > > Meanwhile, there is no need to name the result of getArgs into myargs. > > > > So, getArgs is of type IO [String], and I want to apply that in the > > manner of a list. Without the Monad wrappers, plain > > map ( blah ) strings > > could be ( blah ) <$> strings, and in this particular case I don't > > see a reversed-arg version, although there is one for <*> (as <**>). > > But, for monad stuff in general there are reversed arrows for > > (most?) everything, and that's where I'm heading. > > > > So the first question is, how do I do the equivalent > > map-as-nondeterministic-apply when the strings is further wrapped in > > IO, as is the function being applied. > > > > getArgs >>= mapM_ (\s -> putStrLn s ) > > > > does double-duty of moving the argument from last place to the left, > > as it makes use of eta reduction. Because I have two things going > > on (list applicative and IO monad) I'm losing the slickness of using > > applicative syntax. Is there a nice way to make these work > > together? > > > > And more generally, how would you write such a construct? I'm > > naturally biased with my knowledge in other languages, so maybe > > there's a completely different "normal" way of approaching this? > > I'm not entirely sure I get what you are asking for, but I'll take a > stab and just let me know if I'm completely off the mark. > > If all you want is keep the 'string generator' on the right-hand side, > then you have (=<<): > > mapM_ putStrLn =<< getArgs > > Personally I often like keeping the 'string generator' on the left > (i.e. using (>>=) because when the expression to be mapped grows it > allows this structuring of the code: > > getArgs >>= mapM_ $ \ s -> do > ... > > /M > > -- > Magnus Therning OpenPGP: 0xAB4DFBA4 > email: magnus at therning.org jabber: magnus at therning.org > twitter: magthe http://therning.org/magnus > > Perl is another example of filling a tiny, short-term need, and then > being a real problem in the longer term. > -- Alan Kay > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.john.wheeler at gmail.com Mon Apr 14 10:09:20 2014 From: patrick.john.wheeler at gmail.com (Patrick Wheeler) Date: Mon, 14 Apr 2014 05:09:20 -0500 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: <20140411152815.GA975@tatooine.lan> References: <20140411152815.GA975@tatooine.lan> Message-ID: @John Top level values have the attributes that you describe, recursion, mutual recursion are possible. So by using `let` and `where`, with out knowing any additional rules or information, we can define values and functions which can be locally scoped. Reducing the number of top level values by using locally scoped vales often makes it easier to think about a problem. You concern seems to be focused on the mutual recursion aspect however. Why it is it useful in general? It allows for problems to be broken down in to sub problems and solved separately. With out mutually recursion or something similar if you had a problem that need to recurse in two different ways, each which some times depended on the other you would have to write it all in one large function. An example where this is used in serious cod would be pipes and conduit. Both handle stream processing of data. Mutual recursion between (>>~) and (+>>) http://hackage.haskell.org/package/pipes-4.1.1/docs/src/Pipes-Core.html#%3E%3E~ Look at the goRight and goLeft functions that are locally scoped with `where` in either the `pipe` or `pipeL` functions. https://hackage.haskell.org/package/conduit-1.1.0.1/docs/src/Data-Conduit-Internal.html#Pipe So can help as a tool to break some problems in to subproblems and it is used in serious code in Haskell Patrick On Fri, Apr 11, 2014 at 10:28 AM, Magnus Therning wrote: > On Thu, Apr 10, 2014 at 07:12:25PM -0500, John M. Dlugosz wrote: > > I understand that the definitions introduced by 'let' can be > > recursive, even mutually recursive among several names. Why would > > you want to do that? I saw contrived examples, and wonder why the > > authors never show a realistic example. > > > > let b = f a c > > c = f a b > > in ... > > > > I see that makes sense in light of lazy evaluation: b is really an > > alias for a (recursive) function, not a value that needs to find > > fixed points. > > > > Is this used for common idioms and problem-solving approaches in > > Haskell? > > I can't really point to any idiomatic use, or problem-solving > approaches, but it is a terribly useful feature since one of the > effects is that the order of introducing functions/values isn't > significant. So you are free to write and structure your code in the > manner that makes most sense to you. > > Having just ventured back into OCaml and dipping my toes in F# I > immediately ran into errors caused by their non-recursive `let`, and > the requirement to introduce values/types before use. > > /M > > -- > Magnus Therning OpenPGP: 0xAB4DFBA4 > email: magnus at therning.org jabber: magnus at therning.org > twitter: magthe http://therning.org/magnus > > The results point out the fragility of programmer expertise: advanced > programmers have strong expectations about what programs should look like, > and when those expectations are violated--in seemingly innocuous > ways--their performance drops drastically. > -- Elliot Soloway and Kate Ehrlich > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Patrick Wheeler Patrick.John.Wheeler at gmail.com Patrick.J.Wheeler at rice.edu Patrick.Wheeler at colorado.edu -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Mon Apr 14 11:22:13 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 14 Apr 2014 18:22:13 +0700 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: <534B7A28.1020303@ucdavis.edu> References: <534B7A28.1020303@ucdavis.edu> Message-ID: On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > oldbuysell :: String -- "buy", "sell" or "" Why is this String when you've defined the perfectly cromulent BuyOrSell datatype? How can you be sure there won't be some accidental buggy code that sets the field to "buyy"? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From amindfv at gmail.com Mon Apr 14 12:22:41 2014 From: amindfv at gmail.com (Tom Murphy) Date: Mon, 14 Apr 2014 05:22:41 -0700 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: References: <534B7A28.1020303@ucdavis.edu> Message-ID: The closest thing to guards that'll work is multi-way-if (you'll have to put "{-# LANGUAGE MultiWayIf #-}" at the top of your source file) e.g. buysell = if | oldbuysell ot == "buy" = Buy | oldbuysell ot == "sell" = Sell | otherwise = Unknown but a 'case' is slightly shorter here: buysell = case oldbuysell ot of "buy" -> Buy "sell" -> Sell _ -> Unknown Tom On Mon, Apr 14, 2014 at 4:22 AM, Kim-Ee Yeoh wrote: > > On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo < > defigueiredo at ucdavis.edu> wrote: > >> oldbuysell :: String -- "buy", "sell" or "" > > > Why is this String when you've defined the perfectly cromulent BuyOrSell > datatype? > > How can you be sure there won't be some accidental buggy code that sets > the field to "buyy"? > > -- Kim-Ee > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Mon Apr 14 15:04:50 2014 From: gesh at gesh.uni.cx (Gesh) Date: Mon, 14 Apr 2014 18:04:50 +0300 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: <534B7A28.1020303@ucdavis.edu> References: <534B7A28.1020303@ucdavis.edu> Message-ID: On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo wrote: > >I'm having some trouble understanding where I can or cannot use guards >inside record syntax. I'm writing a simple conversion routine, but I am > >not able to write it without inserting an extra let. Do I need a let >expression here? Am I missing something? > >-------------- >data OldTrade = OldTrade { > oldprice :: Double , > oldamount :: Double , > oldbuysell :: String -- "buy", "sell" or "" > } deriving( Eq, Show) > > >data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show) > >data Trade = Trade { > price :: Double , > amount :: Double , > buysell :: BuyOrSell > } deriving( Eq, Show) > >convert :: OldTrade -> Trade > >convert ot = Trade { price = oldprice ot, > amount = oldamount ot, > buysell = let x | oldbuysell ot == "buy" = Buy > | oldbuysell ot == "sell" = Sell > | otherwise = Unknown > in x > } > >-- how do I eliminate the 'let' expression here? >-- I wanted to write something like: >-- >-- buysell | oldbuysell ot == "buy" = Buy >-- | oldbuysell ot == "sell" = Sell >-- | otherwise = Unknown > >-------------- > >Thanks! > >Dimitri > > > > >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners Note that this has nothing to do with record syntax specifically. Rather, what you're asking is how to write a multi-way if expression. Your way is to introduce a local binding using a let statement, which allows you to use pattern guards as you did. Usually, bowever, you'd use a case statement to avoid the binding. However, you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this thread. Or you could float out the binding to a where clause, except that doesn't seem to be what you're looking for. Hoping to help, Gesh From ngnr63q02 at sneakemail.com Mon Apr 14 15:18:56 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 14 Apr 2014 10:18:56 -0500 Subject: [Haskell-beginners] what is "Gesh"? (Re: function not working as expected, type issues) In-Reply-To: References: Message-ID: On 4/13/2014 8:55 AM, Gesh wrote: > Sorry, my autocorrect must have put that in instead of "the". And it's the nickname I use, not jargon. > Sorry. > Sorry to bother you. From ngnr63q02 at sneakemail.com Mon Apr 14 15:24:36 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 14 Apr 2014 10:24:36 -0500 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: References: <20140411152815.GA975@tatooine.lan> Message-ID: I don't mind recursive *functions*. It's recursive definition of single computed values that looked odd to me. Lazy evaluation makes all the difference. Looking at b= f a c c = f a b I was thinking, "how can it figure out what b and c need to be?" because I'm used to this meaning that it needs to come up with an actual value right now. From ngnr63q02 at sneakemail.com Mon Apr 14 15:41:20 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 14 Apr 2014 10:41:20 -0500 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: <20140413064835.GC1008@tatooine.lan> Message-ID: On 4/14/2014 4:34 AM, Patrick Wheeler wrote: > @John > That way you can write code that looks like: > > main = do > myargs <- getArgs > forM_ myargs $ \s -> do > putStrLn s > putStrLn $ "Second string" ++ s That still introduces a throw-away name and extracts the args on another line. Is there a way to do something like: main = do forM_ (dereference getArgs) $ \s -> do ... or mforM_ getArgs $ \s -> do ... > You can read the discussion around om: > http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/ > Thanks, I will. I guess it bothers me that "wrapped" values in the IO monad can't be independently used in situ, but need to be given a name on another line first. Isn't the likes of "lift" and (even nicer) <*> supposed to address this idea? I can't quite see why that doesn't work here. (And that begs the question of why getArgs needs to be monadic in the first place. It doesn't change its value; it's a strict constant at run-time, and not knowing it at compile time is my problem how?) ?John From allbery.b at gmail.com Mon Apr 14 15:47:40 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 14 Apr 2014 11:47:40 -0400 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: <20140413064835.GC1008@tatooine.lan> Message-ID: On Mon, Apr 14, 2014 at 11:41 AM, John M. Dlugosz wrote: > (And that begs the question of why getArgs needs to be monadic in the > first place. It doesn't change its value; it's a strict constant at > run-time, and not knowing it at compile time is my problem how?) But it is the *compiler's* problem. "Constant" means "known at compile time" to the compiler. Although I wonder if you'd be happier with getArgs >>= mapM_ (do ...). -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From jzdudek at gmail.com Mon Apr 14 17:53:40 2014 From: jzdudek at gmail.com (Jacek Dudek) Date: Mon, 14 Apr 2014 13:53:40 -0400 Subject: [Haskell-beginners] set multiplication In-Reply-To: <53439FF5.7050001@dfki.de> References: <53439FF5.7050001@dfki.de> Message-ID: Well it looks I was beat to the answer by Mariano, but here's my solution anyway. It's a little more expository, hope that helps. Note that we both decided to use a right fold in our solutions. {- Define a binary operation. Note the type signature. When used in the fold operation below, the 2nd argument denotes the set product of the first couple of lists (counting from the right-hand side) that were in your initial list. -} f :: [a] -> [[a]] -> [[a]] f [] _ = [] f (x : xs) yss = map (x :) yss ++ f xs yss {- Now for the product, we use a right fold operation to get the product of the first two lists (counting from the right), then distribute the elements of the next list over the result, then the elements of the next list, and so on. -} setProd :: [[a]] -> [[a]] setProd [] = [] setProd xss = foldr f (map (: []) (last xss)) (init xss) test = [[1,2,3],[4,5],[6,7]] On 4/8/14, Christian Maeder wrote: > Some time ago I discovered that the monadic "sequence" function operates > quite interestingly on lists. I don't know why you call it > multiplication. I was looking for something like combinations: > > Prelude> sequence [[1,2,3], [4,5], [6,7]] > > HTH Christian > > Am 08.04.2014 07:00, schrieb Nishant: >> >> hi, >> >> I am trying to implement a set multiplication program in haskell. >> >> Input : [1,2,3] [4,5] [6,7] >> Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] >> >> >> I implemented it for constant number of inputs like >> >> setMul xs ys zs = [ [x] ++ [y] ++ [z] | x <- xs , y<-ys ,z <- zs] >> >> I am not able to generalize this for any number of lists. >> >> type signature would be : >> >> setMulMany :: [[a]] -> [[a]] >> >> Example : >> >> Input : [ [1,2,3] , [4,5] , [6,7]] >> Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...] >> >> >> Regards. >> Nishant >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From defigueiredo at ucdavis.edu Mon Apr 14 18:36:08 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Mon, 14 Apr 2014 12:36:08 -0600 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: References: <534B7A28.1020303@ucdavis.edu> Message-ID: <534C2A98.2060105@ucdavis.edu> Thanks everyone :-) I think the "case of" is what I was looking for. I keep thinking of using "case of" as in pattern matching to find the "Shape" of the result of an expression and of using guards to evaluate predicates. Forgetting that True and False are constructors themselves. I just have to change that mindset. Cheers, Dimitri Em 14/04/14 09:04, Gesh escreveu: > On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo wrote: >> I'm having some trouble understanding where I can or cannot use guards >> inside record syntax. I'm writing a simple conversion routine, but I am >> >> not able to write it without inserting an extra let. Do I need a let >> expression here? Am I missing something? >> >> -------------- >> data OldTrade = OldTrade { >> oldprice :: Double , >> oldamount :: Double , >> oldbuysell :: String -- "buy", "sell" or "" >> } deriving( Eq, Show) >> >> >> data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show) >> >> data Trade = Trade { >> price :: Double , >> amount :: Double , >> buysell :: BuyOrSell >> } deriving( Eq, Show) >> >> convert :: OldTrade -> Trade >> >> convert ot = Trade { price = oldprice ot, >> amount = oldamount ot, >> buysell = let x | oldbuysell ot == "buy" = Buy >> | oldbuysell ot == "sell" = Sell >> | otherwise = Unknown >> in x >> } >> >> -- how do I eliminate the 'let' expression here? >> -- I wanted to write something like: >> -- >> -- buysell | oldbuysell ot == "buy" = Buy >> -- | oldbuysell ot == "sell" = Sell >> -- | otherwise = Unknown >> >> -------------- >> >> Thanks! >> >> Dimitri >> >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > Note that this has nothing to do with record syntax specifically. Rather, what you're asking is how to write a multi-way if expression. Your way is to introduce a local binding using a let statement, which allows you to use pattern guards as you did. > Usually, bowever, you'd use a case statement to avoid the binding. However, you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this thread. Or you could float out the binding to a where clause, except that doesn't seem to be what you're looking for. > Hoping to help, > Gesh > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From ngnr63q02 at sneakemail.com Mon Apr 14 18:37:04 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 14 Apr 2014 13:37:04 -0500 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: <20140413064835.GC1008@tatooine.lan> Message-ID: On 4/14/2014 10:47 AM, Brandon Allbery wrote: > > > But it is the *compiler's* problem. "Constant" means "known at compile time" to the compiler. > Pure means pure at run-time over the life of the executable. Just because the compiler doesn't know what it is (had it cared to evaluate it at compile time) doesn't mean it's not pure. > Although I wonder if you'd be happier with getArgs >>= mapM_ (do ...). That's what I ended up with when I stopped fiddling with that particular line. From allbery.b at gmail.com Mon Apr 14 18:42:08 2014 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 14 Apr 2014 14:42:08 -0400 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: <20140413064835.GC1008@tatooine.lan> Message-ID: On Mon, Apr 14, 2014 at 2:37 PM, John M. Dlugosz wrote: > On 4/14/2014 10:47 AM, Brandon Allbery wrote: > >> But it is the *compiler's* problem. "Constant" means "known at compile >> time" to the compiler. >> > > Pure means pure at run-time over the life of the executable. Just because > the compiler doesn't To you. To the compiler, and to me actually, if it is not known at compile time then it cannot be pure; it may not vary over an individual run of the program but it can vary over the lifetime of the program itself. If I cannot know it *statically* --- at compile time, since it is the compiler that needs to know that it is static --- then it is not pure. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Mon Apr 14 18:44:03 2014 From: bob at redivi.com (Bob Ippolito) Date: Mon, 14 Apr 2014 11:44:03 -0700 Subject: [Haskell-beginners] How do I use Guards in record syntax? In-Reply-To: <534C2A98.2060105@ucdavis.edu> References: <534B7A28.1020303@ucdavis.edu> <534C2A98.2060105@ucdavis.edu> Message-ID: You wouldn't even need to match True and False, you could match the String you're looking for. convert ot = Trade { price = oldprice ot, amount = oldamount ot, buysell = case oldbuysell ot of "buy" -> Buy "sell" -> Sell _ -> Unknown } On Mon, Apr 14, 2014 at 11:36 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > Thanks everyone :-) > > I think the "case of" is what I was looking for. I keep thinking of using > "case of" as in pattern matching to find the "Shape" of the result of an > expression and of using guards to evaluate predicates. Forgetting that True > and False are constructors themselves. I just have to change that mindset. > > Cheers, > > Dimitri > > > Em 14/04/14 09:04, Gesh escreveu: > > On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo < >> defigueiredo at ucdavis.edu> wrote: >> >>> I'm having some trouble understanding where I can or cannot use guards >>> inside record syntax. I'm writing a simple conversion routine, but I am >>> >>> not able to write it without inserting an extra let. Do I need a let >>> expression here? Am I missing something? >>> >>> -------------- >>> data OldTrade = OldTrade { >>> oldprice :: Double , >>> oldamount :: Double , >>> oldbuysell :: String -- "buy", "sell" or "" >>> } deriving( Eq, Show) >>> >>> >>> data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show) >>> >>> data Trade = Trade { >>> price :: Double , >>> amount :: Double , >>> buysell :: BuyOrSell >>> } deriving( Eq, Show) >>> >>> convert :: OldTrade -> Trade >>> >>> convert ot = Trade { price = oldprice ot, >>> amount = oldamount ot, >>> buysell = let x | oldbuysell ot == "buy" = Buy >>> | oldbuysell ot == "sell" = Sell >>> | otherwise = Unknown >>> in x >>> } >>> >>> -- how do I eliminate the 'let' expression here? >>> -- I wanted to write something like: >>> -- >>> -- buysell | oldbuysell ot == "buy" = Buy >>> -- | oldbuysell ot == "sell" = Sell >>> -- | otherwise = Unknown >>> >>> -------------- >>> >>> Thanks! >>> >>> Dimitri >>> >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >> Note that this has nothing to do with record syntax specifically. Rather, >> what you're asking is how to write a multi-way if expression. Your way is >> to introduce a local binding using a let statement, which allows you to use >> pattern guards as you did. >> Usually, bowever, you'd use a case statement to avoid the binding. >> However, you could use the MultiWayIf LANGUAGE pragma, as suggested >> elsewhere in this thread. Or you could float out the binding to a where >> clause, except that doesn't seem to be what you're looking for. >> Hoping to help, >> Gesh >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike at fpcomplete.com Mon Apr 14 19:20:27 2014 From: mike at fpcomplete.com (Mike Meyer) Date: Mon, 14 Apr 2014 14:20:27 -0500 Subject: [Haskell-beginners] Why getArgs is in the IO monad Message-ID: > > > From: "John M. Dlugosz" > (And that begs the question of why getArgs needs to be monadic in the > first place. It > doesn't change its value; it's a strict constant at run-time, and not > knowing it at > compile time is my problem how?) > Actually, the value of the arguments can be changed - at least on some platforms. They are writable from C, if nothing else. What should getArgs do if some ffi changes the arguments before it's called? Mike -------------- next part -------------- An HTML attachment was scrubbed... URL: From davidleothomas at gmail.com Mon Apr 14 19:30:40 2014 From: davidleothomas at gmail.com (David Thomas) Date: Mon, 14 Apr 2014 12:30:40 -0700 Subject: [Haskell-beginners] general structuring of "foreach" logic in IO In-Reply-To: References: <20140413064835.GC1008@tatooine.lan> Message-ID: getArgs is not pure in the presence of withArgs. This is actually what we want; testing/exercising code that uses getArgs would be a pain otherwise. On Mon, Apr 14, 2014 at 11:42 AM, Brandon Allbery wrote: > On Mon, Apr 14, 2014 at 2:37 PM, John M. Dlugosz > wrote: >> >> On 4/14/2014 10:47 AM, Brandon Allbery wrote: >>> >>> But it is the *compiler's* problem. "Constant" means "known at compile >>> time" to the compiler. >> >> >> Pure means pure at run-time over the life of the executable. Just because >> the compiler doesn't > > > To you. To the compiler, and to me actually, if it is not known at compile > time then it cannot be pure; it may not vary over an individual run of the > program but it can vary over the lifetime of the program itself. If I cannot > know it *statically* --- at compile time, since it is the compiler that > needs to know that it is static --- then it is not pure. > > -- > brandon s allbery kf8nh sine nomine associates > allbery.b at gmail.com ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From michael at orlitzky.com Mon Apr 14 19:41:03 2014 From: michael at orlitzky.com (Michael Orlitzky) Date: Mon, 14 Apr 2014 15:41:03 -0400 Subject: [Haskell-beginners] Why getArgs is in the IO monad In-Reply-To: References: Message-ID: <534C39CF.4020300@orlitzky.com> On 04/14/2014 03:20 PM, Mike Meyer wrote: > > From: "John M. Dlugosz" > > (And that begs the question of why getArgs needs to be monadic in > the first place. It > doesn't change its value; it's a strict constant at run-time, and > not knowing it at > compile time is my problem how?) > > > Actually, the value of the arguments can be changed - at least on some > platforms. They are writable from C, if nothing else. What should > getArgs do if some ffi changes the arguments before it's called? > You can do it right from within Haskell: Prelude> import System.Environment Prelude System.Environment> getArgs [] Prelude System.Environment> withArgs ["foo"] $ getArgs ["foo"] It's useful if you have a command-line interface and the user mistypes something; all you have to do is continue as if they passed "--help" instead of a separate code path. From nrujac at gmail.com Mon Apr 14 20:04:32 2014 From: nrujac at gmail.com (Arjun Comar) Date: Mon, 14 Apr 2014 16:04:32 -0400 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: References: <20140411152815.GA975@tatooine.lan> Message-ID: John, That's classic mutual recursion. In your example, the values of b and c depend entirely on f. For example what if f = const a Now b and c are obviously a and its not ambiguous. Similarly, mutually recursive functions can compute values for b and c. Do you have a similar issue with a definition like: b = f a b or is that fine? If so, why? If not, why not? On Mon, Apr 14, 2014 at 11:24 AM, John M. Dlugosz wrote: > I don't mind recursive *functions*. It's recursive definition of single > computed values that looked odd to me. Lazy evaluation makes all the > difference. Looking at > b= f a c > > c = f a b > I was thinking, "how can it figure out what b and c need to be?" because > I'm used to this meaning that it needs to come up with an actual value > right now. > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Apr 15 00:57:19 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 15 Apr 2014 07:57:19 +0700 Subject: [Haskell-beginners] recursive 'let' ? In-Reply-To: References: <20140411152815.GA975@tatooine.lan> Message-ID: On Mon, Apr 14, 2014 at 11:24 AM, John M. Dlugosz wrote: > I don't mind recursive *functions*. It's recursive definition of single >> computed values that looked odd to me. Lazy evaluation makes all the >> difference. Looking at >> b= f a c >> >> c = f a b >> I was thinking, "how can it figure out what b and c need to be?" because >> I'm used to this meaning that it needs to come up with an actual value >> right now. >> >> To amplify Arjun's point, b = f a c c = f a b is equivalent to b = f a (f a b) c = f a (f a c) and now the mutual recursion that's troubling John vanishes. They become plain ol' self-recursive functions. I don't think mutual recursion is harder to make sense of than self-recursion. I think self-recursion appears easier than mutual recursion, but that's deceptive. It's actually just as hard! > >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ari.brandeis.king at gmail.com Thu Apr 17 01:33:49 2014 From: ari.brandeis.king at gmail.com (Ari King) Date: Wed, 16 Apr 2014 21:33:49 -0400 Subject: [Haskell-beginners] Learning Haskell: Web Project Message-ID: Hi, I'm just starting out with Haskell and I thought a good way to learn/practice would be to re-implement a small web app. The web app I plan on re-implementing is made up of a restful (java) api with a angularjs client. The api and client is served by jetty. I'd appreciate suggestions on frameworks and/or tools that make the most sense as I start out. I was thinking of using scotty + warp + persistence for the api/serving of static content. As for tooling, I was thinking of vim with ghc-mod & syntastic. Thanks. -Ari -------------- next part -------------- An HTML attachment was scrubbed... URL: From magnus at therning.org Thu Apr 17 07:03:52 2014 From: magnus at therning.org (Magnus Therning) Date: Thu, 17 Apr 2014 09:03:52 +0200 Subject: [Haskell-beginners] Learning Haskell: Web Project In-Reply-To: References: Message-ID: On Thu, Apr 17, 2014 at 3:33 AM, Ari King wrote: > I was thinking of using scotty + warp + persistence for the api/serving of > static content. As for tooling, I was thinking of vim with ghc-mod & > syntastic. Add hasktags (or some other tag generator found on hackage) and you have the same setup I use. I'd be very interested in how you find it, and any adjustments you make to the tool setup during the way. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus From gilbertomelfe at gmail.com Fri Apr 18 15:12:05 2014 From: gilbertomelfe at gmail.com (Gilberto Melfe) Date: Fri, 18 Apr 2014 16:12:05 +0100 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! Message-ID: Hi there to you all! I've been reading through the first chapter of "Learn You a Haskell" and I'd like to ask the community a few questions. Any help would be appreciated... -- ---------- ---------- ---------- ---------- ---------- All the standard Prelude functions that throw out an error when fed the []! head maximum ... Are there safe versions anywhere, or do we have to define them ourselves? -- ---------- ---------- ---------- ---------- ---------- This one is really important! I understand that for the definition of product to work as it is: product [] must be equal to 1 But what if we want to add a product to something else??? Shouldn't the result be Nothing? (I guess we would have to guard against this! But we must guard against the parameter of product being the empty list, anyway. Otherwise we risk adding 1 when there is nothing do multiply) (The same question arises with the functions and and or, and their boolean results, I think! Right?) -- ---------- ---------- ---------- ---------- ---------- -- ---------- Start Quote Names can't be enumerated. What comes after "John"? I don't know. -- ---------- End Quote "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings? -- ---------- ---------- ---------- ---------- ---------- -- ---------- Start Quote To make a list with all the numbers from 20 to 1, you can't just do [20..1], you have to do [20,19..1]. -- ---------- End Quote Why is this? If the first was greater than the second it would just subtract! Right? -- ---------- ---------- ---------- ---------- ---------- -- ---------- Start Quote Watch out when using floating point numbers in ranges! Because they are not completely precise (by definition), their use in ranges can yield some pretty funky results. ghci> [0.1, 0.3 .. 1] [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] -- ---------- End Quote Can anyone explain me why it works for the first few values, and not "completely"? -- ---------- ---------- ---------- ---------- ---------- Any thoughts? Thank You Very Much in advance! Gilberto -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Fri Apr 18 19:38:32 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Fri, 18 Apr 2014 21:38:32 +0200 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! In-Reply-To: References: Message-ID: Am 18.04.2014 21:26 schrieb "Gilberto Melfe" : > -- ---------- Start Quote > > Names can't be enumerated. What comes after "John"? I don't know. > > -- ---------- End Quote > > "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings? Following your theory the next "name" would be "Joho", is that a name or just random letters? That's why you don't have predecessor or successor functions on String. > -- ---------- Start Quote > > To make a list with all the numbers from 20 to 1, you can't just do [20..1], you have to do [20,19..1]. > > -- ---------- End Quote > > Why is this? If the first was greater than the second it would just subtract! Right? I would be with you, but that's not how it is designed. We can't change the given behavior. > -- ---------- Start Quote > > Watch out when using floating point numbers in ranges! Because they are not completely precise (by definition), their use in ranges can yield some pretty funky results. > > ghci> [0.1, 0.3 .. 1] > [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] > > -- ---------- End Quote > > Can anyone explain me why it works for the first few values, and not "completely"? To explain that we had to go into how floating point numbers are represented internally in the PCs memory, I'm to tired to do that right now. But problems comparable to this one exist on every language that has floating point numbers. HTH Norbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Fri Apr 18 19:42:33 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 18 Apr 2014 12:42:33 -0700 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! In-Reply-To: References: Message-ID: On Fri, Apr 18, 2014 at 8:12 AM, Gilberto Melfe wrote: > Hi there to you all! > > I've been reading through the first chapter of "Learn You a Haskell" and > I'd like to ask the community a few questions. > > Any help would be appreciated... > > -- ---------- ---------- ---------- ---------- ---------- > > All the standard Prelude functions that throw out an error when fed the []! > > head > maximum > ... > > Are there safe versions anywhere, or do we have to define them ourselves? > Not so many that ship with GHC or Haskell Platform, but you can install safe: https://hackage.haskell.org/package/safe Some of these you can work around, for example you can get a safe version of `head` just by using Data.Maybe.listToMaybe > -- ---------- ---------- ---------- ---------- ---------- > > This one is really important! > > I understand that for the definition of product to work as it is: > product [] must be equal to 1 > > But what if we want to add a product to something else??? > Shouldn't the result be Nothing? > (I guess we would have to guard against this! But we must guard against > the parameter of product being the empty list, anyway. Otherwise we risk > adding 1 when there is nothing do multiply) > > (The same question arises with the functions and and or, and their boolean > results, I think! Right?) > There's a precedent in mathematics for behaving like this. 0! and n^0 are both equal to 1 for example. It sounds like perhaps you're trying to do something strange with products of lists, and there might be a better way but it's hard to suggest something without a concrete example. > > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > Names can't be enumerated. What comes after "John"? I don't know. > > -- ---------- End Quote > > "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to > difficult or impossible to create a function that enumerates all possible > strings? > This is not hard to implement, but you don't know which of those strings are names. > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > To make a list with all the numbers from 20 to 1, you can't just do > [20..1], you have to do [20,19..1]. > > -- ---------- End Quote > > Why is this? If the first was greater than the second it would just > subtract! Right? > [a..b] is syntax sugar for enumFromTo and the definition of that function just doesn't behave in that way. [a, b .. c] is syntax sugar for enumFromThenTo which does. A reason for it to behave like this would be that it's often desired to have the behavior that it does. Consider enumerating every index in a list `xs` except for the first, you could write this as `[1 .. length xs - 1]` with the current syntax, but that sort of thing would yield surprising results if it sometimes went backwards. > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > Watch out when using floating point numbers in ranges! Because they are > not completely precise (by definition), their use in ranges can yield some > pretty funky results. > > ghci> [0.1, 0.3 .. 1] > [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] > > -- ---------- End Quote > > Can anyone explain me why it works for the first few values, and not > "completely"? > It doesn't "work" for any of the values, it's just an artifact of how they're rendered. 0.1 can't be exactly represented in binary floating point, so the error compounds. Double probably shouldn't be enumerable in the first place, but that's a decision we have to live with. The reason that the end result is so surprising is that 1.0999999999999999 is less than 1 + 0.1 and for whatever reason the way Enum is defined for Double checks to see if the result is > to+(then-from) rather than <= to. -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Fri Apr 18 19:46:08 2014 From: toad3k at gmail.com (David McBride) Date: Fri, 18 Apr 2014 15:46:08 -0400 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! In-Reply-To: References: Message-ID: > Are there safe versions anywhere, or do we have to define them ourselves? There are some in the safe package. Also some prelude replacements like basic-prelude or classy-prelude that have their own renditions versions of some functions. > (The same question arises with the functions and and or, and their boolean results, I think! Right?) Product works for empty lists because 1 is a sensible answer and returning maybe is cumbersome, so it does the sensible thing. There are many times where I use any or all and I want it to work for empty lists rather than having to make my own version. This same goes for sum. > "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or impossible to create a function that enumerates all possible strings? It is possible, but what should the successor to "john" be? Should it be "johN" or "joho"? If there is more than one way to do a thing, it is better to leave the instance off and allow the user to specify his order in his own application. You can see such api design decisions come to the forefront in some modules like Data.Monoid where it could interpret integers as monoids over sums, products, or a multitude of other ways. > To make a list with all the numbers from 20 to 1, you can't just do [20..1], you have to do [20,19..1]. It is that way because [20..1] desugars to enumFromTo 20 1, which chose to do it that way. Personally I'd be all for the change, but people have come to depend on the way it is now, and so it is possible that code would break if they changed it. > Can anyone explain me why it works for the first few values, and not "completely"? The problem with enumerating floats is that floats don't actually represent whole numbers well. On the hardware level there is a lot of fudging involved. A lot of people think that having an enum instance for float is a bad thing, but it is kept around because it can be useful sometimes. I actually agree with those people. On Fri, Apr 18, 2014 at 11:12 AM, Gilberto Melfe wrote: > Hi there to you all! > > I've been reading through the first chapter of "Learn You a Haskell" and > I'd like to ask the community a few questions. > > Any help would be appreciated... > > -- ---------- ---------- ---------- ---------- ---------- > > All the standard Prelude functions that throw out an error when fed the []! > > head > maximum > ... > > Are there safe versions anywhere, or do we have to define them ourselves? > > -- ---------- ---------- ---------- ---------- ---------- > > This one is really important! > > I understand that for the definition of product to work as it is: > product [] must be equal to 1 > > But what if we want to add a product to something else??? > Shouldn't the result be Nothing? > (I guess we would have to guard against this! But we must guard against > the parameter of product being the empty list, anyway. Otherwise we risk > adding 1 when there is nothing do multiply) > > (The same question arises with the functions and and or, and their boolean > results, I think! Right?) > > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > Names can't be enumerated. What comes after "John"? I don't know. > > -- ---------- End Quote > > "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to > difficult or impossible to create a function that enumerates all possible > strings? > > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > To make a list with all the numbers from 20 to 1, you can't just do > [20..1], you have to do [20,19..1]. > > -- ---------- End Quote > > Why is this? If the first was greater than the second it would just > subtract! Right? > > -- ---------- ---------- ---------- ---------- ---------- > > -- ---------- Start Quote > > Watch out when using floating point numbers in ranges! Because they are > not completely precise (by definition), their use in ranges can yield some > pretty funky results. > > ghci> [0.1, 0.3 .. 1] > [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] > > -- ---------- End Quote > > Can anyone explain me why it works for the first few values, and not > "completely"? > > -- ---------- ---------- ---------- ---------- ---------- > > Any thoughts? > > Thank You Very Much in advance! > > Gilberto > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Fri Apr 18 19:54:48 2014 From: bob at redivi.com (Bob Ippolito) Date: Fri, 18 Apr 2014 12:54:48 -0700 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! In-Reply-To: References: Message-ID: On Fri, Apr 18, 2014 at 12:42 PM, Bob Ippolito wrote: > > > > On Fri, Apr 18, 2014 at 8:12 AM, Gilberto Melfe wrote: > >> Hi there to you all! >> >> I've been reading through the first chapter of "Learn You a Haskell" and >> I'd like to ask the community a few questions. >> >> Any help would be appreciated... >> >> -- ---------- ---------- ---------- ---------- ---------- >> >> All the standard Prelude functions that throw out an error when fed the >> []! >> >> head >> maximum >> ... >> >> Are there safe versions anywhere, or do we have to define them ourselves? >> > > Not so many that ship with GHC or Haskell Platform, but you can install > safe: > https://hackage.haskell.org/package/safe > > Some of these you can work around, for example you can get a safe version > of `head` just by using Data.Maybe.listToMaybe > > >> -- ---------- ---------- ---------- ---------- ---------- >> >> This one is really important! >> >> I understand that for the definition of product to work as it is: >> product [] must be equal to 1 >> >> But what if we want to add a product to something else??? >> Shouldn't the result be Nothing? >> (I guess we would have to guard against this! But we must guard against >> the parameter of product being the empty list, anyway. Otherwise we risk >> adding 1 when there is nothing do multiply) >> >> (The same question arises with the functions and and or, and their >> boolean results, I think! Right?) >> > > There's a precedent in mathematics for behaving like this. 0! and n^0 are > both equal to 1 for example. It sounds like perhaps you're trying to do > something strange with products of lists, and there might be a better way > but it's hard to suggest something without a concrete example. > > >> >> -- ---------- ---------- ---------- ---------- ---------- >> >> -- ---------- Start Quote >> >> Names can't be enumerated. What comes after "John"? I don't know. >> >> -- ---------- End Quote >> >> "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to >> difficult or impossible to create a function that enumerates all possible >> strings? >> > > This is not hard to implement, but you don't know which of those strings > are names. > > >> -- ---------- ---------- ---------- ---------- ---------- >> >> -- ---------- Start Quote >> >> To make a list with all the numbers from 20 to 1, you can't just do >> [20..1], you have to do [20,19..1]. >> >> -- ---------- End Quote >> >> Why is this? If the first was greater than the second it would just >> subtract! Right? >> > > [a..b] is syntax sugar for enumFromTo and the definition of that function > just doesn't behave in that way. [a, b .. c] is syntax sugar for > enumFromThenTo which does. A reason for it to behave like this would be > that it's often desired to have the behavior that it does. Consider > enumerating every index in a list `xs` except for the first, you could > write this as `[1 .. length xs - 1]` with the current syntax, but that sort > of thing would yield surprising results if it sometimes went backwards. > > >> -- ---------- ---------- ---------- ---------- ---------- >> >> -- ---------- Start Quote >> >> Watch out when using floating point numbers in ranges! Because they are >> not completely precise (by definition), their use in ranges can yield some >> pretty funky results. >> >> ghci> [0.1, 0.3 .. 1] >> [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] >> >> -- ---------- End Quote >> >> Can anyone explain me why it works for the first few values, and not >> "completely"? >> > > It doesn't "work" for any of the values, it's just an artifact of how > they're rendered. 0.1 can't be exactly represented in binary floating > point, so the error compounds. Double probably shouldn't be enumerable in > the first place, but that's a decision we have to live with. The reason > that the end result is so surprising is that 1.0999999999999999 is less > than 1 + 0.1 and for whatever reason the way Enum is defined for Double > checks to see if the result is > to+(then-from) rather than <= to. > To clarify, this is what is happening: ?> takeWhile (< 1 + 0.1) $ iterate (+0.1) 0.1 [0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999,1.0999999999999999] -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Fri Apr 18 20:54:32 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri, 18 Apr 2014 16:54:32 -0400 Subject: [Haskell-beginners] Learn You a Haskell! I have a few questions! In-Reply-To: References: Message-ID: <20140418205432.GA10138@seas.upenn.edu> On Fri, Apr 18, 2014 at 12:54:48PM -0700, Bob Ippolito wrote: > >> > >> ghci> [0.1, 0.3 .. 1] > >> [0.1,0.3,0.5,0.7,0.8999999999999999,1.0999999999999999] > >> > >> -- ---------- End Quote > >> > >> Can anyone explain me why it works for the first few values, and not > >> "completely"? > >> > > > > It doesn't "work" for any of the values, it's just an artifact of how > > they're rendered. 0.1 can't be exactly represented in binary floating > > point, so the error compounds. Double probably shouldn't be enumerable in > > the first place, but that's a decision we have to live with. The reason > > that the end result is so surprising is that 1.0999999999999999 is less > > than 1 + 0.1 and for whatever reason the way Enum is defined for Double > > checks to see if the result is > to+(then-from) rather than <= to. > > > > To clarify, this is what is happening: > > ?> takeWhile (< 1 + 0.1) $ iterate (+0.1) 0.1 > > [0.1,0.2,0.30000000000000004,0.4,0.5,0.6,0.7,0.7999999999999999,0.8999999999999999,0.9999999999999999,1.0999999999999999] Actually, the check is whether the result is > to + ((then - from)/2). Notice that the original example is stepping by 0.2, not 0.1. -Brent From ngnr63q02 at sneakemail.com Fri Apr 18 22:51:19 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 18 Apr 2014 17:51:19 -0500 Subject: [Haskell-beginners] Auto-increment String In-Reply-To: References: Message-ID: On 4/18/2014 2:38 PM, Norbert Melzer wrote: > > "a" to "z" then "aa" to "zz" then "aaa" to "zzz" and so on! Is it to difficult or > impossible to create a function that enumerates all possible strings? > > Following your theory the next "name" would be "Joho", is that a name or just random > letters? That's why you don't have predecessor or successor functions on String. That's how it works in Perl. perl -e "$x='John'; ++$x; print $x; " produces "Joho" It would be an interesting beginner's exercise to implement that in Haskell (re: http://perldoc.perl.org/perlop.html#Auto-increment-and-Auto-decrement ) A foldr would be good, I suppose, but complicated by the need to return the carry state as well as the accumulated tail to the next iteration. From henry.lockyer at ntlworld.com Sat Apr 19 17:31:02 2014 From: henry.lockyer at ntlworld.com (Henry Lockyer) Date: Sat, 19 Apr 2014 18:31:02 +0100 Subject: [Haskell-beginners] Platform for Mac OS 10.6 Message-ID: <6D9647F4-EAFE-4566-9A8D-9534F51E16AD@ntlworld.com> Hello people - trying to get back into Haskell again after previous flirtation which was long enough ago to forget almost everything, it seems. I appear to be lost in a mirror maze in trying to work out again how to bring my platform more up to date on Mac OS 10.6.8 (Snow Leopard). The macos link for download at http://www.haskell.org/platform/mac.html however does not seem to work. I do not get a working installer package just an "Unknown" 204mb file. ? Sniffing around via GHC pages I find this page: http://www.haskell.org/ghc/distribution_packages where it says "For Mac OS X we provide a native system installer, available on the download page for each release. We recommend using these installers " However the 'download page' link just takes you circularly back to http://www.haskell.org/ghc/download ? This page http://www.haskell.org/platform/index.html says the the current platform version is 2013.2.0 From my own past notes I should currently have platform 2011.4.0.0 I do definitely have GHC 7.0.4-x86_64 I also ran 'cabal update' as I dimly remembered that I should do this, and it indicated there was another version of cabal install available by doing a "cabal install cabal-install" but this complains: "$ cabal install cabal-install Resolving dependencies... cabal: cannot configure process-1.2.0.0. It requires base >=4.4 && <4.8 For the dependency on base >=4.4 && <4.8 there are these packages: . . ." I have base 4.3.1.0 (as indicated by GHC-PKG LIST). I thought refreshing the overall package level would be the most straightforward way to update to a consistent state as at least the first step. Hopefully I'm just having a bad day... I have two main motivations here (1) general refresh (2) get latest Open-GL stuff - in case that affects the answer. Can anyone recommend/remind/help with how best to approach this? Many thanks/ Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Sat Apr 19 17:39:34 2014 From: karl at karlv.net (Karl Voelker) Date: Sat, 19 Apr 2014 10:39:34 -0700 Subject: [Haskell-beginners] Platform for Mac OS 10.6 In-Reply-To: <6D9647F4-EAFE-4566-9A8D-9534F51E16AD@ntlworld.com> References: <6D9647F4-EAFE-4566-9A8D-9534F51E16AD@ntlworld.com> Message-ID: <1397929174.24606.108209073.5E729273@webmail.messagingengine.com> On Sat, Apr 19, 2014, at 10:31 AM, Henry Lockyer wrote: The macos link for download at [1]http://www.haskell.org/platform/mac.html however does not seem to work. I do not get a working installer package just an "Unknown" 204mb file. ? It's possible that your download was corrupted or did not finish. If you have a way to calculate the sha1 sum, you can compare it to the one on that webpage. (For example, you could use sha1sum from the md5sha1sum Homebrew package.) Or, if your connection is reasonably fast, just try downloading it again. -Karl References 1. http://www.haskell.org/platform/mac.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From hengchu.zhang at gmail.com Sat Apr 19 20:08:31 2014 From: hengchu.zhang at gmail.com (hengchu Zhang) Date: Sat, 19 Apr 2014 16:08:31 -0400 Subject: [Haskell-beginners] failed to install HTTP-4000.2.4 when executing `cabal install cabal-install` Message-ID: Hi Friends, When I tried to update cabal-install today by doing `cabal install cabal-install`, I got the following message due to failure in compiling Network/HTTP/Auth.hs, here is a screenshot of the process ? I am using Mac OS 10.8.4, and I installed haskell from homebrew. Does this mean something might have went wrong when I installed haskell? I have Xcode 5.1.1 and the latest command line tools installed. Does anyone know what might be going wrong? Thank you so much for your help! Best, Hengchu -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Screen Shot 2014-04-19 at 4.06.19 PM.png Type: image/png Size: 114996 bytes Desc: not available URL: From daniel.is.fischer at googlemail.com Sat Apr 19 20:30:35 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Sat, 19 Apr 2014 22:30:35 +0200 Subject: [Haskell-beginners] failed to install HTTP-4000.2.4 when executing `cabal install cabal-install` In-Reply-To: References: Message-ID: <20577565.Ik9lgDK0ug@linux-hpeb.site> On Saturday 19 April 2014, 16:08:31, hengchu Zhang wrote: > ? > I am using Mac OS 10.8.4, and I installed haskell from homebrew. Does this > mean something might have went wrong when I installed haskell? No, it means that the HTTP package has no upper bound on its dependency on the network package, and the type of Network.URI.relativeTo changed in network-2.4.0.0 versus network-2.3.* You can try either cabal install --constraint="HTTP >= 4000.2.5" cabal-install or cabal install --constraint="network < 2.4" cabal-install Either way, check with the --dry-run flag first whether cabal reports any problems. From ngnr63q02 at sneakemail.com Sun Apr 20 03:25:28 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sat, 19 Apr 2014 22:25:28 -0500 Subject: [Haskell-beginners] graphical output? Message-ID: How might I (easily) use Haskell to draw simple 2D graphics? Outputting a SVG file is OK for simple static results, but I want to animate. (Windows platform is strongly preferred.) From byorgey at seas.upenn.edu Sun Apr 20 03:57:17 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sat, 19 Apr 2014 23:57:17 -0400 Subject: [Haskell-beginners] graphical output? In-Reply-To: References: Message-ID: <20140420035717.GA20878@seas.upenn.edu> On Sat, Apr 19, 2014 at 10:25:28PM -0500, John M. Dlugosz wrote: > How might I (easily) use Haskell to draw simple 2D graphics? > Outputting a SVG file is OK for simple static results, but I want to animate. > (Windows platform is strongly preferred.) I suggest checking out 'gloss': http://hackage.haskell.org/package/gloss It's based on OpenGL, which comes with the Haskell Platform, and has a simple interface for making animations. -Brent From ngnr63q02 at sneakemail.com Sun Apr 20 05:19:11 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 20 Apr 2014 00:19:11 -0500 Subject: [Haskell-beginners] graphical output? In-Reply-To: <20140420035717.GA20878@seas.upenn.edu> References: <20140420035717.GA20878@seas.upenn.edu> Message-ID: On 4/19/2014 10:57 PM, Brent Yorgey wrote: > On Sat, Apr 19, 2014 at 10:25:28PM -0500, John M. Dlugosz wrote: >> How might I (easily) use Haskell to draw simple 2D graphics? >> Outputting a SVG file is OK for simple static results, but I want to animate. >> (Windows platform is strongly preferred.) > > I suggest checking out 'gloss': > > http://hackage.haskell.org/package/gloss > > It's based on OpenGL, which comes with the Haskell Platform, and has a > simple interface for making animations. > > -Brent > Hmm, is there any tutorial or friendly guide to Gloss? From winobes at gmail.com Sun Apr 20 06:31:14 2014 From: winobes at gmail.com (Bill Noble) Date: Sun, 20 Apr 2014 08:31:14 +0200 Subject: [Haskell-beginners] graphical output? In-Reply-To: References: <20140420035717.GA20878@seas.upenn.edu> Message-ID: On Sun, Apr 20, 2014 at 7:19 AM, John M. Dlugosz wrote: > On 4/19/2014 10:57 PM, Brent Yorgey wrote: > >> On Sat, Apr 19, 2014 at 10:25:28PM -0500, John M. Dlugosz wrote: >> >>> How might I (easily) use Haskell to draw simple 2D graphics? >>> Outputting a SVG file is OK for simple static results, but I want to >>> animate. >>> (Windows platform is strongly preferred.) >>> >> >> I suggest checking out 'gloss': >> >> http://hackage.haskell.org/package/gloss >> >> It's based on OpenGL, which comes with the Haskell Platform, and has a >> simple interface for making animations. >> >> -Brent >> >> > Hmm, is there any tutorial or friendly guide to Gloss? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Not exactly a guide, but you might get some mileage out of > cabal install gloss-examples (note for some reason I had to install llvm first). -------------- next part -------------- An HTML attachment was scrubbed... URL: From henry.lockyer at ntlworld.com Sun Apr 20 10:24:26 2014 From: henry.lockyer at ntlworld.com (Henry Lockyer) Date: Sun, 20 Apr 2014 11:24:26 +0100 Subject: [Haskell-beginners] Platform for Mac OS 10.6 In-Reply-To: <1397929174.24606.108209073.5E729273@webmail.messagingengine.com> References: <6D9647F4-EAFE-4566-9A8D-9534F51E16AD@ntlworld.com> <1397929174.24606.108209073.5E729273@webmail.messagingengine.com> Message-ID: Thanks Karl I did try the download several times. 'Unknown' and 'Unknown-1' are 2 separate downloads. $ openssl sha1 /Users/henrylockyer/Downloads/Unknown-1 SHA1(/Users/henrylockyer/Downloads/Unknown-1)= 89e6fb747816af69acabc5c04cee103257855614 $ openssl sha1 /Users/henrylockyer/Downloads/Unknown SHA1(/Users/henrylockyer/Downloads/Unknown)= 89e6fb747816af69acabc5c04cee103257855614 Both have the same checkum as published on the download page... SHA-1: 89e6fb747816af69acabc5c04cee103257855614 /Henry On 19 Apr 2014, at 18:39, Karl Voelker wrote: > On Sat, Apr 19, 2014, at 10:31 AM, Henry Lockyer wrote: >> The macos link for download at >> http://www.haskell.org/platform/mac.html >> however does not seem to work. I do not get a working installer package just an "Unknown" 204mb file. >> ? > > It's possible that your download was corrupted or did not finish. If you have a way to calculate the sha1 sum, you can compare it to the one on that webpage. (For example, you could use sha1sum from the md5sha1sum Homebrew package.) Or, if your connection is reasonably fast, just try downloading it again. > > -Karl > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From henry.lockyer at ntlworld.com Sun Apr 20 10:42:46 2014 From: henry.lockyer at ntlworld.com (Henry Lockyer) Date: Sun, 20 Apr 2014 11:42:46 +0100 Subject: [Haskell-beginners] Platform for Mac OS 10.6 In-Reply-To: References: <6D9647F4-EAFE-4566-9A8D-9534F51E16AD@ntlworld.com> <1397929174.24606.108209073.5E729273@webmail.messagingengine.com> Message-ID: Mmm. clicking the download link does not work but ctrl-click and 'download linked file...' to the desktop now seems to have worked and it is recognised as a package... Monkeys at typewriters/ H On 20 Apr 2014, at 11:24, Henry Lockyer wrote: > Thanks Karl > I did try the download several times. > 'Unknown' and 'Unknown-1' are 2 separate downloads. > $ openssl sha1 /Users/henrylockyer/Downloads/Unknown-1 > SHA1(/Users/henrylockyer/Downloads/Unknown-1)= 89e6fb747816af69acabc5c04cee103257855614 > $ openssl sha1 /Users/henrylockyer/Downloads/Unknown > SHA1(/Users/henrylockyer/Downloads/Unknown)= 89e6fb747816af69acabc5c04cee103257855614 > Both have the same checkum as published on the download page... > SHA-1: 89e6fb747816af69acabc5c04cee103257855614 > /Henry > > > > On 19 Apr 2014, at 18:39, Karl Voelker wrote: > >> On Sat, Apr 19, 2014, at 10:31 AM, Henry Lockyer wrote: >>> The macos link for download at >>> http://www.haskell.org/platform/mac.html >>> however does not seem to work. I do not get a working installer package just an "Unknown" 204mb file. >>> ? >> >> It's possible that your download was corrupted or did not finish. If you have a way to calculate the sha1 sum, you can compare it to the one on that webpage. (For example, you could use sha1sum from the md5sha1sum Homebrew package.) Or, if your connection is reasonably fast, just try downloading it again. >> >> -Karl >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From freitasraffa at gmail.com Sun Apr 20 12:35:52 2014 From: freitasraffa at gmail.com (raffa f) Date: Sun, 20 Apr 2014 09:35:52 -0300 Subject: [Haskell-beginners] type problems with my little code Message-ID: hi everyone, i'm a complete beginner on programming and i've been solving the 99 haskell problems and i've come into a situation. just like all my problems with haskell, it's about types. i wrote a little thing that takes a list and puts all the equal characters together, like [1,1,3,2,3] will be [ [1,1], [3,3], [2] ]. here it is: pack xs = pack' xs where pack' :: [a] -> [[a]] pack' [] = [] pack' (x:y) = extra x xs ++ pack' y extra x xs = [sum' (map (remover x) (map ((==) x) xs))] remover :: a -> Bool -> [a] remover y x = if x then [y] else [] sum' :: [[a]] -> [a] sum' [] = [] sum' (x:xs) = x ++ sum' xs i know, i know, this code is probably terrible and i'm sure there are more clever ways to do this...but i wanna understand what's wrong. the "extra" function works perfectly, it takes a variable and then looks at how many times it's presented on a list and outputs a list with a list of that variable the amount of times that it was presented on the original list. however, pack does not work quite right. first of all, it'll use extra on repeated characters, creating repeated lists, but that doesn't matter because i have a function that fixes that issue and i'll use it after i figure out what's wrong with pack. now, the real problem is with types. here's what haskell says when i try to load it: noventa.hs:77:32: Couldn't match type `a' with `a1' `a' is a rigid type variable bound by the inferred type of pack :: [a] -> [[a]] at noventa.hs:73:1 `a1' is a rigid type variable bound by the type signature for pack' :: [a1] -> [[a1]] at noventa.hs:75:19 Expected type: [a1] Actual type: [a] In the second argument of `extra', namely `xs' In the first argument of `(++)', namely `extra x xs' In the expression: extra x xs ++ pack' y Failed, modules loaded: none. i don't understand this at all! if i replace [a] with String and [[a]] with [String], it works! but i want pack to work for lists of numbers too... types are so confusing. can anyone help me? -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Apr 20 12:57:58 2014 From: shaegis at gmail.com (S. H. Aegis) Date: Sun, 20 Apr 2014 21:57:58 +0900 Subject: [Haskell-beginners] About scope range: outside of the do-block Message-ID: I'm trying to solve problem 22 of Euler Project. ( http://projecteuler.net/problem=22) I got file contents using " names <- readFile "names.txt" " in "do-block". When I try to use 'names' outside of "do-block", I get following error message; namesScores.hs:30:35: Not in scope: `names' Failed, modules loaded: none. Is there any other ways to use variables outside of the "do-block"? Sorry for taking your time. Thank you. My code is... ------------------------------------------------------------- import Data.List import Data.List.Split import Data.Char import Data.Maybe main :: IO() main = do names <- readFile "names.txt" print $ sum $ map (\x -> getPosition x * getScore x) names getPosition :: String -> Int getPosition x = fromMaybe 0 (elemIndex x sortedNamesList) + 1 getScore :: String -> Int getScore xs = sum $ map (\x -> ord x - 64) xs sortedNamesList :: [String] sortedNamesList = sort $ nameList names nameList :: String -> [String] nameList = split (dropDelims . dropBlanks $ oneOf ",\"") Error message is... ------------------------------------------------------------------ Prelude> :l namesScores.hs [1 of 1] Compiling Main ( namesScores.hs, interpreted ) namesScores.hs:31:35: Not in scope: `names' Failed, modules loaded: none. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vandijk.roel at gmail.com Sun Apr 20 13:09:42 2014 From: vandijk.roel at gmail.com (Roel van Dijk) Date: Sun, 20 Apr 2014 15:09:42 +0200 Subject: [Haskell-beginners] About scope range: outside of the do-block In-Reply-To: References: Message-ID: On 20 April 2014 14:57, S. H. Aegis wrote: > > Is there any other ways to use variables outside of the "do-block"? > You can pass the list of names as an argument where it is needed. > import Data.List > import Data.List.Split > import Data.Char > import Data.Maybe > > main :: IO () > main = do > names <- readFile "names.txt" > print $ sum $ map (\x -> getPosition names x * getScore x) names > > getPosition :: [String] -> String -> Int > getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) + 1 > > getScore :: String -> Int > getScore xs = sum $ map (\x -> ord x - 64) xs > > sortedNamesList :: [String] -> [String] > sortedNamesList names = sort $ nameList names > > nameList :: String -> [String] > nameList = split (dropDelims . dropBlanks $ oneOf ",\"") > -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Sun Apr 20 13:17:10 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Sun, 20 Apr 2014 15:17:10 +0200 Subject: [Haskell-beginners] About scope range: outside of the do-block In-Reply-To: References: Message-ID: Am 20.04.2014 14:58 schrieb "S. H. Aegis" : > namesScores.hs:31:35: Not in scope: `names' > Failed, modules loaded: none. The code you posted has only 21 lines while your error message denotes an error in line 31, so there is something missing. -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Apr 20 13:38:05 2014 From: shaegis at gmail.com (S. H. Aegis) Date: Sun, 20 Apr 2014 22:38:05 +0900 Subject: [Haskell-beginners] About scope range: outside of the do-block In-Reply-To: References: Message-ID: Oh, I'm sorry. I delete some of commented lines. Sorry for confusing. Error occurred in sortedNamesList = sort $ nameList names Thank you. 2014-04-20 22:17 GMT+09:00 Norbert Melzer : > > Am 20.04.2014 14:58 schrieb "S. H. Aegis" : > > > namesScores.hs:31:35: Not in scope: `names' > > Failed, modules loaded: none. > > The code you posted has only 21 lines while your error message denotes an > error in line 31, so there is something missing. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Sok Ha, CHANG Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul. Tel: +82-2-442-7585 -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sun Apr 20 13:47:10 2014 From: shaegis at gmail.com (S. H. Aegis) Date: Sun, 20 Apr 2014 22:47:10 +0900 Subject: [Haskell-beginners] About scope range: outside of the do-block In-Reply-To: References: Message-ID: It works ! Thank you so much ! Have a nice day~ 2014-04-20 22:09 GMT+09:00 Roel van Dijk : > On 20 April 2014 14:57, S. H. Aegis wrote: >> >> Is there any other ways to use variables outside of the "do-block"? >> > > You can pass the list of names as an argument where it is needed. > > >> import Data.List >> import Data.List.Split >> import Data.Char >> import Data.Maybe >> >> main :: IO () >> main = do >> names <- readFile "names.txt" >> print $ sum $ map (\x -> getPosition names x * getScore x) names >> >> getPosition :: [String] -> String -> Int >> getPosition names x = fromMaybe 0 (elemIndex x $ sortedNamesList names) + >> 1 >> >> getScore :: String -> Int >> getScore xs = sum $ map (\x -> ord x - 64) xs >> >> sortedNamesList :: [String] -> [String] >> sortedNamesList names = sort $ nameList names >> >> nameList :: String -> [String] >> nameList = split (dropDelims . dropBlanks $ oneOf ",\"") >> > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Sok Ha, CHANG Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul. Tel: +82-2-442-7585 -------------- next part -------------- An HTML attachment was scrubbed... URL: From gesh at gesh.uni.cx Sun Apr 20 14:01:33 2014 From: gesh at gesh.uni.cx (Gesh) Date: Sun, 20 Apr 2014 17:01:33 +0300 Subject: [Haskell-beginners] type problems with my little code In-Reply-To: References: Message-ID: <0796df9e-f38f-4716-8588-11c676616200@email.android.com> On April 20, 2014 3:35:52 PM GMT+03:00, raffa f wrote: >hi everyone, i'm a complete beginner on programming and i've been >solving >the 99 haskell problems and i've come into a situation. just like all >my >problems with haskell, it's about types. i wrote a little thing that >takes >a list and puts all the equal characters together, like [1,1,3,2,3] >will be >[ [1,1], [3,3], [2] ]. here it is: > >pack xs = pack' xs > > where > > pack' :: [a] -> [[a]] > > pack' [] = [] > > pack' (x:y) = extra x xs ++ pack' y > > > >extra x xs = [sum' (map (remover x) (map ((==) x) xs))] > > > >remover :: a -> Bool -> [a] > >remover y x = if x then [y] else [] > > > >sum' :: [[a]] -> [a] > >sum' [] = [] > >sum' (x:xs) = x ++ sum' xs > > >i know, i know, this code is probably terrible and i'm sure there are >more >clever ways to do this...but i wanna understand what's wrong. the >"extra" >function works perfectly, it takes a variable and then looks at how >many >times it's presented on a list and outputs a list with a list of that >variable the amount of times that it was presented on the original >list. > >however, pack does not work quite right. first of all, it'll use extra >on >repeated characters, creating repeated lists, but that doesn't matter >because i have a function that fixes that issue and i'll use it after i >figure out what's wrong with pack. now, the real problem is with types. >here's what haskell says when i try to load it: > >noventa.hs:77:32: > Couldn't match type `a' with `a1' > `a' is a rigid type variable bound by > the inferred type of pack :: [a] -> [[a]] at noventa.hs:73:1 > `a1' is a rigid type variable bound by > the type signature for pack' :: [a1] -> [[a1]] at >noventa.hs:75:19 > Expected type: [a1] > Actual type: [a] > In the second argument of `extra', namely `xs' > In the first argument of `(++)', namely `extra x xs' > In the expression: extra x xs ++ pack' y >Failed, modules loaded: none. > > >i don't understand this at all! if i replace [a] with String and [[a]] >with >[String], it works! but i want pack to work for lists of numbers too... >types are so confusing. can anyone help me? > > >------------------------------------------------------------------------ > >_______________________________________________ >Beginners mailing list >Beginners at haskell.org >http://www.haskell.org/mailman/listinfo/beginners I'm a bit fuzzy on this part of GHC's type checker, so if someone could correct me, that'd be awesome. If I understand correctly, what's happening here is that the type signature you gave pack' implicitly told GHC that pack' can work with any list, independently of the type of xs. However, this is incorrect, as the call to extra forces the type of the input to pack' to be a list with the same type of elements as those of xs. Thus, GHC complains that you told it pack' accepts more stuff than it actually does. This can be fixed in one of a few ways - this list might not be exhaustive, though. - Keep the type of pack' as it currently is, but fix its implementation to actually have that type. Since our problem was caused by passing xs to extra restricting the type, you'd need to replace xs in that expression by something else, e.g. x:y - Fix the type of pack' to what you actually meant. This can be done either by removing the signature, allowing GHC to fill in the correct type, or by turning on ScopedTypeVariables, writing pack's signature and making sure you use the same type variable for the elements of the parameters of pack and pack'. Besides all of the above, there are a few more comments I'd like to make on your code. First, note that sum' = concat Next, extra x xs = [filter (==x) xs] Then, noting that [x] ++ xs = x:xs, you obtain that pack' (x:xs) = filter (==x) xs : pack' xs In addition, the type signature you gave pack' lacks an Eq a constraint, which is needed as you are testing the elements of the parameter for equality. Finally, pack doesn't do what you want it to do, as it will replace every element of its parameter by a list containing as many occurrences of that element as there are occurrences of that element in the parameter. Can you think of a way to solve this problem? Good luck learning! I hope you'll find this journey interesting and beautiful. Gesh From gesh at gesh.uni.cx Sun Apr 20 14:06:30 2014 From: gesh at gesh.uni.cx (Gesh) Date: Sun, 20 Apr 2014 17:06:30 +0300 Subject: [Haskell-beginners] About scope range: outside of the do-block In-Reply-To: References: Message-ID: <2e419344-380f-4a22-853b-c33a5f6ece4d@email.android.com> On April 20, 2014 4:38:05 PM GMT+03:00, "S. H. Aegis" wrote: >Oh, I'm sorry. >I delete some of commented lines. >Sorry for confusing. > >Error occurred in >sortedNamesList = sort $ nameList names > >Thank you. > > >2014-04-20 22:17 GMT+09:00 Norbert Melzer : > >> >> Am 20.04.2014 14:58 schrieb "S. H. Aegis" : >> >> > namesScores.hs:31:35: Not in scope: `names' >> > Failed, modules loaded: none. >> >> The code you posted has only 21 lines while your error message >denotes an >> error in line 31, so there is something missing. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> In general, variables defined locally in one scope are not floated to external scopes. Thus, your question is analogous to asking: Given the following code: foo = ... where bar = ... baz = ... Can I call bar from baz? The answer being no, you can't, unless you pass bar as a parameter to baz (or use a Reader monad, which is equivalent). Hoping to help, Gesh From ky3 at atamo.com Sun Apr 20 14:24:44 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sun, 20 Apr 2014 21:24:44 +0700 Subject: [Haskell-beginners] type problems with my little code In-Reply-To: References: Message-ID: First of all, Raffa, welcome to the list. I hope your stay is friendly and that you'll derive benefit from it over the long-term. This line that you wrote pack' (x:y) = extra x xs ++ pack' y suggests that you might want to review the different between list-consing, which is the (:) function, and list-append, which is (++). Especially for Haskell, you'll need to pay close attention to how the type signatures differ. These concepts are very, very old and easily google-able. A solid foundation in these fundamentals will make tackling the rest of the 99 problems so much easier. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Sun Apr 20 14:56:50 2014 From: toad3k at gmail.com (David McBride) Date: Sun, 20 Apr 2014 10:56:50 -0400 Subject: [Haskell-beginners] type problems with my little code In-Reply-To: References: Message-ID: You are running into a quirk in the type system. The haskell standard says that sub expressions in where clauses that have type annotations with variables in them (pack'), the variables are not the same as the variables in the toplevel expression (pack). When you wrote this code, haskell inferred a type of pack :: (Eq a1) => [a1] -> [[a1]], and then pack' is correctly the type pack' :: [a] -> [[a]], but that a1 and that a are different types as far as ghc is concerned. But based on how they are used, ghc says no, they should be the same type, ERROR FIX IT. Even if you specify a type for pack that uses "a", the problem still persists because they are still considered different a's. There are two ways to fix this. First one is just remove the type info from pack'. Just remove that line and ghc will infer all the types correctly and you can get on with your day. The other way is to use the ScopedTypeVariables extension, which everyone generally loves and uses when they need it. It has a niggle where it requires a forall for each type variable in the overall type, but from that point on it works the way you would expect. {-# LANGUAGE ScopedTypeVariables #-} pack :: forall a. (Eq a) => [a] -> [[a]] pack xs = pack' xs where pack' :: [a] -> [[a]] pack' [] = [] pack' (x:y) = extra x xs ++ pack' y On Sun, Apr 20, 2014 at 8:35 AM, raffa f wrote: > hi everyone, i'm a complete beginner on programming and i've been solving > the 99 haskell problems and i've come into a situation. just like all my > problems with haskell, it's about types. i wrote a little thing that takes > a list and puts all the equal characters together, like [1,1,3,2,3] will be > [ [1,1], [3,3], [2] ]. here it is: > > pack xs = pack' xs > > where > > pack' :: [a] -> [[a]] > > pack' [] = [] > > pack' (x:y) = extra x xs ++ pack' y > > > > extra x xs = [sum' (map (remover x) (map ((==) x) xs))] > > > > remover :: a -> Bool -> [a] > > remover y x = if x then [y] else [] > > > > sum' :: [[a]] -> [a] > > sum' [] = [] > > sum' (x:xs) = x ++ sum' xs > > > i know, i know, this code is probably terrible and i'm sure there are more > clever ways to do this...but i wanna understand what's wrong. the "extra" > function works perfectly, it takes a variable and then looks at how many > times it's presented on a list and outputs a list with a list of that > variable the amount of times that it was presented on the original list. > > however, pack does not work quite right. first of all, it'll use extra on > repeated characters, creating repeated lists, but that doesn't matter > because i have a function that fixes that issue and i'll use it after i > figure out what's wrong with pack. now, the real problem is with types. > here's what haskell says when i try to load it: > > noventa.hs:77:32: > Couldn't match type `a' with `a1' > `a' is a rigid type variable bound by > the inferred type of pack :: [a] -> [[a]] at noventa.hs:73:1 > `a1' is a rigid type variable bound by > the type signature for pack' :: [a1] -> [[a1]] at > noventa.hs:75:19 > Expected type: [a1] > Actual type: [a] > In the second argument of `extra', namely `xs' > In the first argument of `(++)', namely `extra x xs' > In the expression: extra x xs ++ pack' y > Failed, modules loaded: none. > > > i don't understand this at all! if i replace [a] with String and [[a]] > with [String], it works! but i want pack to work for lists of numbers > too... types are so confusing. can anyone help me? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Mon Apr 21 04:11:18 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 20 Apr 2014 23:11:18 -0500 Subject: [Haskell-beginners] graphical output? In-Reply-To: References: <20140420035717.GA20878@seas.upenn.edu> Message-ID: On 4/20/2014 1:31 AM, Bill Noble wrote: > (note for some reason I had to install llvm first). > I had already tried that, and ran into the same problem. From ngnr63q02 at sneakemail.com Mon Apr 21 04:12:15 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 20 Apr 2014 23:12:15 -0500 Subject: [Haskell-beginners] Where does cabal store its junk? Message-ID: When I use cabal to ?install? a package, where does it end up on my disk? I can't seem to find it. From karl at karlv.net Mon Apr 21 04:53:32 2014 From: karl at karlv.net (Karl Voelker) Date: Sun, 20 Apr 2014 21:53:32 -0700 Subject: [Haskell-beginners] Where does cabal store its junk? In-Reply-To: References: Message-ID: <1398056012.4183.108527929.186D231D@webmail.messagingengine.com> On Sun, Apr 20, 2014, at 09:12 PM, John M. Dlugosz wrote: > When I use cabal to ?install? a package, where does it end up on my disk? > I can't seem to > find it. A package can be installed into your user package database or the global package database, and the locations of both depend on your operating system (and possibly other factors). If you run "ghc-pkg list", in addition to listing all your installed packages, it will also show you the locations of both package databases. -Karl From ngnr63q02 at sneakemail.com Mon Apr 21 05:14:20 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 21 Apr 2014 00:14:20 -0500 Subject: [Haskell-beginners] Where does cabal store its junk? In-Reply-To: <1398056012.4183.108527929.186D231D@webmail.messagingengine.com> References: <1398056012.4183.108527929.186D231D@webmail.messagingengine.com> Message-ID: On 4/20/2014 11:53 PM, Karl Voelker wrote: > On Sun, Apr 20, 2014, at 09:12 PM, John M. Dlugosz wrote: >> When I use cabal to ?install? a package, where does it end up on my disk? >> If you run "ghc-pkg list", in > addition to listing all your installed packages, it will also show you > the locations of both package databases. > Thanks. When I looked at the help for cabal, I did not see any option for showing actual file names. From nishantgeek at gmail.com Mon Apr 21 08:06:19 2014 From: nishantgeek at gmail.com (Nishant) Date: Mon, 21 Apr 2014 13:36:19 +0530 Subject: [Haskell-beginners] Error while using indices and elem Message-ID: Hi, Do I need to define indices and elem for Graph a w here explicitly ? import Data.Array data Graph a w = Array a [(a,w)] -- array (1,3) [(1,[(2,3),(3,2)])] --Given a vertex list out adjacents getAdjVertices :: (Ix a, Eq a) => a -> (Graph a w) -> [a] getAdjVertices x arr | not (x `elem` (indices arr)) = error "No such Vertex" | otherwise = extractV (arr ! 2) where extractV [] = [] extractV (x:xs) = (fst x):extractV (xs) GHCI gives : Prelude> :l programs\graph.hs [1 of 1] Compiling Main ( programs\graph.hs, interpreted ) programs\graph.hs:16:47: Couldn't match expected type `Array a e0' with actual type `Graph a w' In the first argument of `indices', namely `arr' In the second argument of `elem', namely `(indices arr)' In the first argument of `not', namely `(x `elem` (indices arr))' programs\graph.hs:17:46: Couldn't match expected type `Array Integer [(a, b0)]' with actual type `Graph a w' In the first argument of `(!)', namely `arr' In the first argument of `extractV', namely `(arr ! 2)' In the expression: extractV (arr ! 2) Failed, modules loaded: none. Prelude> Regards. Nishant -------------- next part -------------- An HTML attachment was scrubbed... URL: From dserban01 at gmail.com Mon Apr 21 08:59:47 2014 From: dserban01 at gmail.com (Dan Serban) Date: Mon, 21 Apr 2014 11:59:47 +0300 Subject: [Haskell-beginners] Error while using indices and elem In-Reply-To: References: Message-ID: Hello Nishant, It's not clear whether you're implementing graph functionality as a device for learning Haskell, or whether you need it as a kind of a library to support future programs. If it's the latter, I want to point you to the Data.Graph module in the containers package: http://hackage.haskell.org/package/containers-0.2.0.1/docs/Data-Graph.html From chaddai.fouche at gmail.com Mon Apr 21 09:26:34 2014 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Mon, 21 Apr 2014 11:26:34 +0200 Subject: [Haskell-beginners] Error while using indices and elem In-Reply-To: References: Message-ID: On Mon, Apr 21, 2014 at 10:06 AM, Nishant wrote: > > Hi, > > Do I need to define indices and elem for Graph a w here explicitly ? > > > > import Data.Array > > data Graph a w = Array a [(a,w)] > -- array (1,3) [(1,[(2,3),(3,2)])] > > > You probably meant to make "Graph a w" a type synonym but you used "data" thus it is a type constructor with one data constructor named Array which takes two parameters : an "a" and a list of pairs of "a" and "w". You wanted to write : > type Graph a w = Array a [(a,w)] which should work better (indices will work on it like on an Array from Data.Array). -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: From freitasraffa at gmail.com Mon Apr 21 14:02:48 2014 From: freitasraffa at gmail.com (raffa f) Date: Mon, 21 Apr 2014 11:02:48 -0300 Subject: [Haskell-beginners] type problems with my little code Message-ID: thanks for everyone that replied! you all sound really nice. removing the type info from pack' worked, i hadn't thought of that because i believed that when pattern matching you always had to write the type info first. the ScopedTypeVariables thing sounds really interesting too, and i'm gonna read more about it and maybe i'll understand more how types work. and gesh, thanks for the comments on the rest of my code, it's always nice learning new and useful functions. and i did realize that pack repeats the same list for every element in the list, but i had written just before that a function that takes care of repeated elements in a list, so now my final code just applies that function to pack. i don't know how efficient that is, but it works! -------------- next part -------------- An HTML attachment was scrubbed... URL: From kc1956 at gmail.com Mon Apr 21 17:50:34 2014 From: kc1956 at gmail.com (KC) Date: Mon, 21 Apr 2014 10:50:34 -0700 Subject: [Haskell-beginners] How do I return Nothing from a function? Message-ID: -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey -------------- next part -------------- An HTML attachment was scrubbed... URL: From kc1956 at gmail.com Mon Apr 21 17:53:31 2014 From: kc1956 at gmail.com (KC) Date: Mon, 21 Apr 2014 10:53:31 -0700 Subject: [Haskell-beginners] How do I return Nothing from a function? Message-ID: How do I return Nothing from a function? When there is no edge in the graph this code fails. *** Exception: Graph.hs:65:9-26: Irrefutable pattern failed for pattern (Data.Maybe.Just w) *Main> :t weight weight :: (Ix t1, Ix t2) => t1 -> t2 -> Array (t1, t2) (Maybe t) -> t weight x y g = w where (Just w) = g!(x,y) Thank you for your time. -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Mon Apr 21 18:23:44 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Mon, 21 Apr 2014 20:23:44 +0200 Subject: [Haskell-beginners] How do I return Nothing from a function? In-Reply-To: References: Message-ID: You can't return Nothing, because your function doesn't return a maybe. If you make it a maybe, you just need to return the value of g without further processing. Am 21.04.2014 19:53 schrieb "KC" : > How do I return Nothing from a function? > > When there is no edge in the graph this code fails. > *** Exception: Graph.hs:65:9-26: Irrefutable pattern failed for pattern > (Data.Maybe.Just w) > > > *Main> :t weight > weight > :: (Ix t1, Ix t2) => t1 -> t2 -> Array (t1, t2) (Maybe t) -> t > > weight x y g = w > where > (Just w) = g!(x,y) > > > Thank you for your time. > > -- > > -- > > Sent from an expensive device which will be obsolete in a few months! :D > Casey > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dedgrant at gmail.com Mon Apr 21 18:25:24 2014 From: dedgrant at gmail.com (Darren Grant) Date: Mon, 21 Apr 2014 11:25:24 -0700 Subject: [Haskell-beginners] How do I return Nothing from a function? In-Reply-To: References: Message-ID: If the result of weight is (Maybe t) instead of the then the result of the array lookup can be passed out directly for the caller to check: weight x y g = g!(x, y) Is this what you mean? Cheers, Darren On Apr 21, 2014 10:53 AM, "KC" wrote: > How do I return Nothing from a function? > > When there is no edge in the graph this code fails. > *** Exception: Graph.hs:65:9-26: Irrefutable pattern failed for pattern > (Data.Maybe.Just w) > > > *Main> :t weight > weight > :: (Ix t1, Ix t2) => t1 -> t2 -> Array (t1, t2) (Maybe t) -> t > > weight x y g = w > where > (Just w) = g!(x,y) > > > Thank you for your time. > > -- > > -- > > Sent from an expensive device which will be obsolete in a few months! :D > Casey > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From orclev at gmail.com Mon Apr 21 18:30:50 2014 From: orclev at gmail.com (Kyle Murphy) Date: Mon, 21 Apr 2014 14:30:50 -0400 Subject: [Haskell-beginners] How do I return Nothing from a function? In-Reply-To: References: Message-ID: Your problem is that you're explicitly saying that weight can NEVER fail to return a value, so in the case that you run into a Nothing value you need to return some default value instead. Alternatively you could change the signature of weight to instead return a Maybe t type instead. If it's the former, you need something like: weight x y g = case g!(x,y) of Just w -> w Nothing -> 0 -- default weight here or more idiomatically: weight x y g = maybe 0 id (g!(x,y)) Not sure if you need the parens around g!(x,y) in that expression, but I put them in just in case. -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Mon, Apr 21, 2014 at 1:53 PM, KC wrote: > How do I return Nothing from a function? > > When there is no edge in the graph this code fails. > *** Exception: Graph.hs:65:9-26: Irrefutable pattern failed for pattern > (Data.Maybe.Just w) > > > *Main> :t weight > weight > :: (Ix t1, Ix t2) => t1 -> t2 -> Array (t1, t2) (Maybe t) -> t > > weight x y g = w > where > (Just w) = g!(x,y) > > > Thank you for your time. > > > -- > > -- > > Sent from an expensive device which will be obsolete in a few months! :D > Casey > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From poczta at emanuelkoczwara.pl Mon Apr 21 19:08:52 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Mon, 21 Apr 2014 21:08:52 +0200 Subject: [Haskell-beginners] Haddock and XML Message-ID: <1398107332.5327.24.camel@emanuel-laptop> Hi, I want to include some XML in documentation produced by haddock. Here is my simple test case: http://lpaste.net/103047 I generate the docs like this: haddock -h Doctest.hs Can I set some command line options or maybe there are some formatting options (like '@' in my example) to get this XML rendered correctly? Emanuel -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 5185 bytes Desc: not available URL: From fuuzetsu at fuuzetsu.co.uk Mon Apr 21 19:11:21 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Mon, 21 Apr 2014 21:11:21 +0200 Subject: [Haskell-beginners] Haddock and XML In-Reply-To: <1398107332.5327.24.camel@emanuel-laptop> References: <1398107332.5327.24.camel@emanuel-laptop> Message-ID: <53556D59.5050809@fuuzetsu.co.uk> On 04/21/2014 09:08 PM, Emanuel Koczwara wrote: > Hi, > > I want to include some XML in documentation produced by haddock. Here is > my simple test case: > > http://lpaste.net/103047 > > I generate the docs like this: > > haddock -h Doctest.hs > > Can I set some command line options or maybe there are some formatting > options (like '@' in my example) to get this XML rendered correctly? > > Emanuel > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Hi, Birdtrack (?>? in front of a line) syntax takes everything verbatim so it should do what you want. I'm not in an environment to test this at the moment so let me know how it goes. Thanks -- Mateusz K. From poczta at emanuelkoczwara.pl Mon Apr 21 19:23:10 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Mon, 21 Apr 2014 21:23:10 +0200 Subject: [Haskell-beginners] Haddock and XML In-Reply-To: <53556D59.5050809@fuuzetsu.co.uk> References: <1398107332.5327.24.camel@emanuel-laptop> <53556D59.5050809@fuuzetsu.co.uk> Message-ID: <1398108190.5327.29.camel@emanuel-laptop> Hi, > Birdtrack (?>? in front of a line) syntax takes everything verbatim so > it should do what you want. I'm not in an environment to test this at > the moment so let me know how it goes. From documentation (www.haskell.org/haddock/doc/html/ch03s08.html): 3.8.2. Special characters The following characters have special meanings in documentation comments: \, /, ', `, ", @, <. To insert a literal occurrence of one of these special characters, precede it with a backslash (\). Additionally, the character > has a special meaning at the beginning of a line, and the following characters have special meanings at the beginning of a paragraph: *, -. These characters can also be escaped using \. Furthermore, the character sequence >>> has a special meaning at the beginning of a line. To escape it, just prefix the characters in the sequence with a backslash. Updated test case: http://lpaste.net/103051 Haddock error: http://lpaste.net/103052 Emanuel -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 5185 bytes Desc: not available URL: From fuuzetsu at fuuzetsu.co.uk Mon Apr 21 19:48:51 2014 From: fuuzetsu at fuuzetsu.co.uk (Mateusz Kowalczyk) Date: Mon, 21 Apr 2014 21:48:51 +0200 Subject: [Haskell-beginners] Haddock and XML In-Reply-To: <1398108190.5327.29.camel@emanuel-laptop> References: <1398107332.5327.24.camel@emanuel-laptop> <53556D59.5050809@fuuzetsu.co.uk> <1398108190.5327.29.camel@emanuel-laptop> Message-ID: <53557623.7010907@fuuzetsu.co.uk> On 04/21/2014 09:23 PM, Emanuel Koczwara wrote: > Hi, > >> Birdtrack (?>? in front of a line) syntax takes everything verbatim so >> it should do what you want. I'm not in an environment to test this at >> the moment so let me know how it goes. > > > From documentation (www.haskell.org/haddock/doc/html/ch03s08.html): > > 3.8.2. Special characters > > The following characters have special meanings in documentation > comments: \, /, ', `, ", @, <. To insert a literal occurrence of one of > these special characters, precede it with a backslash (\). > > Additionally, the character > has a special meaning at the beginning of > a line, and the following characters have special meanings at the > beginning of a paragraph: *, -. These characters can also be escaped > using \. > > Furthermore, the character sequence >>> has a special meaning at the > beginning of a line. To escape it, just prefix the characters in the > sequence with a backslash. > I'm pretty well aware of the docs considering I now co-maintain Haddock ;) > Updated test case: http://lpaste.net/103051 > Haddock error: http://lpaste.net/103052 Ok, firstly, code blocks are a paragraph-level entity: you'll need to have an empty line before the code block (so just add an empty comment line on line 2 of the comment. I forgot to mention this but I now see it. If it still doesn't know let me know. Looking at that error, you're probably using Haddock 2.13.x and what is most likely happening is that it sees < and tries to parse a URL. In Haddock 2.14.x (GHC 7.8.x required) this error would be gone (and the fact that you need a newline would quickly become apparent). If that *still* doesn't work, you can escape the ? Emanuel > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Mateusz K. From poczta at emanuelkoczwara.pl Mon Apr 21 20:20:41 2014 From: poczta at emanuelkoczwara.pl (Emanuel Koczwara) Date: Mon, 21 Apr 2014 22:20:41 +0200 Subject: [Haskell-beginners] Haddock and XML In-Reply-To: <53557623.7010907@fuuzetsu.co.uk> References: <1398107332.5327.24.camel@emanuel-laptop> <53556D59.5050809@fuuzetsu.co.uk> <1398108190.5327.29.camel@emanuel-laptop> <53557623.7010907@fuuzetsu.co.uk> Message-ID: <1398111641.5327.34.camel@emanuel-laptop> Hi, Thanks, it works now. Emanuel -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/x-pkcs7-signature Size: 5185 bytes Desc: not available URL: From ngnr63q02 at sneakemail.com Tue Apr 22 08:00:20 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 22 Apr 2014 03:00:20 -0500 Subject: [Haskell-beginners] How would you write... Message-ID: chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 [1..10] ] The above is not right, as the comprehension syntax doesn't see the input range buried in an argument. What's the right way to express pap1 = translate x y $ color red $ Circle r where (x,y,r) = pappus 100 1 where 1 is [1..10] and I get a list of results? (pap1 does work as expected) (1) how can the list comprehension syntax manage it, (2) what's the recommended way to express that (not necessarily via list comprehension syntax) ? ?John From timmelzer at gmail.com Tue Apr 22 08:17:48 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Tue, 22 Apr 2014 10:17:48 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: What Is the the type of your pappus function? Am 22.04.2014 10:01 schrieb "John M. Dlugosz" : > chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 > [1..10] ] > > The above is not right, as the comprehension syntax doesn't see the input > range buried in an argument. > > What's the right way to express > > pap1 = translate x y $ color red $ Circle r > where (x,y,r) = pappus 100 1 > > where 1 is [1..10] and I get a list of results? > (pap1 does work as expected) > > (1) how can the list comprehension syntax manage it, > (2) what's the recommended way to express that (not necessarily via list > comprehension syntax) ? > > ?John > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Tue Apr 22 08:37:20 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 22 Apr 2014 03:37:20 -0500 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On 4/22/2014 3:17 AM, Norbert Melzer wrote: > What Is the the type of your pappus function? I've not specified a type sig, so it figures out some particular subdomain of numeric types. Whatever that is, it takes two numbers and returns a tuple of three floating-point numbers pappus r n = (x-0.5,y,rn) where blah= n**2*(1-r)**2+r x = (r*(1+r)) / (2*blah) y = (n*r*(1-r)) / blah rn = ((1-r)*r)/(2*blah) From daniel.is.fischer at googlemail.com Tue Apr 22 08:39:05 2014 From: daniel.is.fischer at googlemail.com (Daniel Fischer) Date: Tue, 22 Apr 2014 10:39:05 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: <15367557.p2foigP7Jz@linux-hpeb.site> On Tuesday 22 April 2014, 03:00:20, John M. Dlugosz wrote: > chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 > [1..10] ] > > The above is not right, as the comprehension syntax doesn't see the input > range buried in an argument. > > What's the right way to express > > pap1 = translate x y $ color red $ Circle r > where (x,y,r) = pappus 100 1 > > where 1 is [1..10] and I get a list of results? > (pap1 does work as expected) > > (1) how can the list comprehension syntax manage it, If I guess your intentions right: chain1 = [ translate x y $ color red $ Circle r | (x,y,z) <- map (pappus 100) [1 .. 10]] From raabe at froglogic.com Tue Apr 22 08:47:30 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Tue, 22 Apr 2014 10:47:30 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On 2014-04-22 10:00, John M. Dlugosz wrote: > chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 > [1..10] ] > > The above is not right, as the comprehension syntax doesn't see the input > range buried in an argument. [..] > (1) how can the list comprehension syntax manage it, I suspect chain1 = [ translate x y $ color red $ Circle r | i <- [1..10], (x,y,r) <- pappus 100 i ] would be one way to do what you want. -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From timmelzer at gmail.com Tue Apr 22 08:49:15 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Tue, 22 Apr 2014 10:49:15 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: This one doesn't work, since pappus doesn't return a list Am 22.04.2014 10:47 schrieb "Frerich Raabe" : > On 2014-04-22 10:00, John M. Dlugosz wrote: > >> chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 >> [1..10] ] >> >> The above is not right, as the comprehension syntax doesn't see the input >> range buried in an argument. >> > > [..] > > (1) how can the list comprehension syntax manage it, >> > > I suspect > > chain1 = [ translate x y $ color red $ Circle r | i <- [1..10], (x,y,r) > <- pappus 100 i ] > > would be one way to do what you want. > > -- > Frerich Raabe - raabe at froglogic.com > www.froglogic.com - Multi-Platform GUI Testing > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Tue Apr 22 08:59:20 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Tue, 22 Apr 2014 10:59:20 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On 2014-04-22 10:49, Norbert Melzer wrote: > This one doesn't work, since pappus doesn't return a list Ah, right, I forgot to adjust that part. How about chain1 = [ translate x y $ color red $ Circle r | i <- [1..10], let (x,y,r) = pappus 100 i ] ...though I'm not too fond of 'let' declarations in list comprehensions. -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From timmelzer at gmail.com Tue Apr 22 09:08:02 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Tue, 22 Apr 2014 11:08:02 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: That's why I think Daniel Fischers solution is the one. Am 22.04.2014 10:59 schrieb "Frerich Raabe" : > On 2014-04-22 10:49, Norbert Melzer wrote: > >> This one doesn't work, since pappus doesn't return a list >> > > Ah, right, I forgot to adjust that part. How about > > chain1 = [ translate x y $ color red $ Circle r | i <- [1..10], let > (x,y,r) = pappus 100 i ] > > ...though I'm not too fond of 'let' declarations in list comprehensions. > > -- > Frerich Raabe - raabe at froglogic.com > www.froglogic.com - Multi-Platform GUI Testing > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Apr 22 13:58:47 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 22 Apr 2014 20:58:47 +0700 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On Tue, Apr 22, 2014 at 3:00 PM, John M. Dlugosz wrote: > chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 > [1..10] ] What is translate? What is color? What is Circle? What is pappus? None of this is plain haskell. John, if you make your readers guess at undefined names, they'll go away and hangout somewhere friendlier! -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From abcz2.uprola at gmail.com Tue Apr 22 16:30:31 2014 From: abcz2.uprola at gmail.com (Daniel Hlynskyi) Date: Tue, 22 Apr 2014 19:30:31 +0300 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- [pappus 100 i | i <- [1..10]] ] better chain1 = [transform $ pappus 100 i | i <- [1..10]] where transform (x,y,r) = translate x y $ color red $ Circle r 2014-04-22 16:58 GMT+03:00 Kim-Ee Yeoh : > > On Tue, Apr 22, 2014 at 3:00 PM, John M. Dlugosz > wrote: > >> chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 >> [1..10] ] > > > What is translate? What is color? What is Circle? What is pappus? > > None of this is plain haskell. > > John, if you make your readers guess at undefined names, they'll go away > and hangout somewhere friendlier! > > -- Kim-Ee > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From abcz2.uprola at gmail.com Tue Apr 22 16:55:19 2014 From: abcz2.uprola at gmail.com (Daniel Hlynskyi) Date: Tue, 22 Apr 2014 19:55:19 +0300 Subject: [Haskell-beginners] graphical output? In-Reply-To: References: <20140420035717.GA20878@seas.upenn.edu> Message-ID: another example for gloss https://github.com/plaimi/bweakfwu works under windows 2014-04-21 7:11 GMT+03:00 John M. Dlugosz : > On 4/20/2014 1:31 AM, Bill Noble wrote: > > (note for some reason I had to install llvm first). >> >> I had already tried that, and ran into the same problem. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Tue Apr 22 18:30:15 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 22 Apr 2014 13:30:15 -0500 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On 4/22/2014 3:47 AM, Frerich Raabe wrote: > > chain1 = [ translate x y $ color red $ Circle r | i <- [1..10], (x,y,r) <- pappus 100 i ] > > would be one way to do what you want. > Yes, that's along the lines of what I was thinking???the 1..10 needs to be bound by itself to trigger the looping behavior, but didn't know how to then continue to generate (x,y,r). I thought the comma there was followed by a "guard", which constrains which values of i are taken or skipped. Ah, you have a <- there too. Hmm, but normally a second <- is another iteration that's done first/loops faster. So what's the syntax here? I must be getting things mixed up. ?John From ngnr63q02 at sneakemail.com Tue Apr 22 18:37:50 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 22 Apr 2014 13:37:50 -0500 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On 4/22/2014 8:58 AM, Kim-Ee Yeoh wrote: > > On Tue, Apr 22, 2014 at 3:00 PM, John M. Dlugosz > wrote: > > chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus 100 [1..10] ] > > > What is translate? What is color? What is Circle? What is pappus? > > None of this is plain haskell. > > John, if you make your readers guess at undefined names, they'll go away and hangout > somewhere friendlier! > > -- Kim-Ee > > Sorry ? I thought showing a form that did work would be enough. The important part is that I have a input = map foo [0..10] and a bar (t1,t2,t3) = baz where baz returns a tuple, and the result of bar is the guts of another map. I didn't realize that using real words from a library instead of foo and bar was considered unfriendly! From timmelzer at gmail.com Tue Apr 22 18:45:43 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Tue, 22 Apr 2014 20:45:43 +0200 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: Its considered unfriendly to not give important information. The minimum information would have been the typesignature of every function you use. Am 22.04.2014 20:38 schrieb "John M. Dlugosz" : > On 4/22/2014 8:58 AM, Kim-Ee Yeoh wrote: > >> >> On Tue, Apr 22, 2014 at 3:00 PM, John M. Dlugosz < >> ngnr63q02 at sneakemail.com >> > wrote: >> >> chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus >> 100 [1..10] ] >> >> >> What is translate? What is color? What is Circle? What is pappus? >> >> None of this is plain haskell. >> >> John, if you make your readers guess at undefined names, they'll go away >> and hangout >> somewhere friendlier! >> >> -- Kim-Ee >> >> >> > > Sorry ? I thought showing a form that did work would be enough. The > important part is that I have a input = map foo [0..10] and a bar > (t1,t2,t3) = baz where baz returns a tuple, and the result of bar is the > guts of another map. > > I didn't realize that using real words from a library instead of foo and > bar was considered unfriendly! > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Apr 22 18:47:12 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 22 Apr 2014 14:47:12 -0400 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: There is nothing wrong with using real functions from a library, but you never said what library it was or even enough info to guess. Was it diagrams? Gloss? FOV? Or something you wrote? If the library doesn't matter, as it didn't in this case, you can just specify the types like so data Color = Color translate :: Int -> Int -> () translate = undefined color :: Color -> () color = undefined red :: Color red = undefined so that at least your snippet compiles for others. On Tue, Apr 22, 2014 at 2:37 PM, John M. Dlugosz wrote: > On 4/22/2014 8:58 AM, Kim-Ee Yeoh wrote: > >> >> On Tue, Apr 22, 2014 at 3:00 PM, John M. Dlugosz < >> ngnr63q02 at sneakemail.com >> > wrote: >> >> chain1 = [ translate x y $ color red $ Circle r | (x,y,r) <- pappus >> 100 [1..10] ] >> >> >> What is translate? What is color? What is Circle? What is pappus? >> >> None of this is plain haskell. >> >> John, if you make your readers guess at undefined names, they'll go away >> and hangout >> somewhere friendlier! >> >> -- Kim-Ee >> >> >> > > Sorry ? I thought showing a form that did work would be enough. The > important part is that I have a input = map foo [0..10] and a bar > (t1,t2,t3) = baz where baz returns a tuple, and the result of bar is the > guts of another map. > > I didn't realize that using real words from a library instead of foo and > bar was considered unfriendly! > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Tue Apr 22 18:52:04 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 22 Apr 2014 13:52:04 -0500 Subject: [Haskell-beginners] How to build a package? Message-ID: I used cabal unpack to get the source of a package to read. Now I want to make a change. How do I tell Haskell Platform to make that (the same way cabal knows how to) and do so with the provided files rather than downloading into temp. I got a package to work before using Leksah, but I'd like to do it directly since the cabal file is present and works just fine. A related issue: what should I do about the local copy of a module name that is also installed? Can I make my project use that instead, without having to globally change the name in all the files? From ky3 at atamo.com Wed Apr 23 03:09:56 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Wed, 23 Apr 2014 10:09:56 +0700 Subject: [Haskell-beginners] How would you write... In-Reply-To: References: Message-ID: On Wed, Apr 23, 2014 at 1:37 AM, John M. Dlugosz wrote: > I didn't realize that using real words from a library instead of foo and > bar was considered unfriendly! As everyone else has chimed in, signatures + succinct description is a minimum. Haskell usage is very, very diverse. You can't assume that everyone knows the specifics of a library you're using. E.g. graphics usage is scattered all over the place. Many (most?) haskellers hardly meddle with graphics in the slightest. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Wed Apr 23 08:23:33 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 23 Apr 2014 02:23:33 -0600 Subject: [Haskell-beginners] why do classes require the type variable in type signatures? Message-ID: <53577885.1080001@ucdavis.edu> Hello All, Why does this compile class Special a where isSpecial :: a -> Bool whereas, GHC 7.6 complains about this class AlsoSpecial a where isAlsoSpecial :: b -> Bool This is the error message I get: The class method `isAlsoSpecial' mentions none of the type variables of the class AlsoSpecial a When checking the class method: isAlsoSpecial :: forall b. b -> Bool In the class declaration for `AlsoSpecial' My question is: Why must I use the type variable of the class declaration (i.e. *a*) in the type signature for the associated method? Is there a fundamental reason for this or is it just a GHC specific limitation? I didn't see a need for it when watching this video http://channel9.msdn.com/posts/MDCC-TechTalk-Classes-Jim-but-not-as-we-know-them that explains the translation that GHC does to turn type classes into core. Thanks! Dimitri From andres at well-typed.com Wed Apr 23 08:35:39 2014 From: andres at well-typed.com (=?UTF-8?Q?Andres_L=C3=B6h?=) Date: Wed, 23 Apr 2014 10:35:39 +0200 Subject: [Haskell-beginners] why do classes require the type variable in type signatures? In-Reply-To: <53577885.1080001@ucdavis.edu> References: <53577885.1080001@ucdavis.edu> Message-ID: If you have a class like this > class AlsoSpecial a where > isAlsoSpecial :: b -> Bool then 'isAlsoSpecial' would get a type like this isAlsoSpecial :: AlsoSpecial a => b -> Bool There may be several instances of class 'AlsoSpecial' with different implementations. GHC has the task of figuring out which of the implementations to use in a specific situation. It does so by looking at the context in which it is used. But if, like here, the type itself does not mention the class variable, then learning about the context won't help. For example, if we use the function like this isAlsoSpecial 'x' then GHC learns that 'b' must be a 'Char', but it still does not know anything about 'a'. In fact, there's absolutely no way to establish information about 'a' at all. This is the problem, and this is why the class method is rejected in the first place. (The translation to Core is still possible. In Core itself, every type application is explicit, so in Core there'd be a syntax to manually resolve the ambiguity by explicitly selecting which 'a' we want to use and passing the appropriate dictionary. But Haskell's surface language doesn't provide this option, and it never has.) Cheers, Andres -- Andres L?h, Haskell Consultant Well-Typed LLP, http://www.well-typed.com Registered in England & Wales, OC335890 250 Ice Wharf, 17 New Wharf Road, London N1 9RF, England From hjgtuyl at chello.nl Wed Apr 23 13:01:14 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Wed, 23 Apr 2014 15:01:14 +0200 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: On Tue, 22 Apr 2014 20:52:04 +0200, John M. Dlugosz wrote: > I used cabal unpack to get the source of a package to read. Now I want > to make a change. How do I tell Haskell Platform to make that (the > same way cabal knows how to) and do so with the provided files rather > than downloading into temp. > I got a package to work before using Leksah, but I'd like to do it > directly since the cabal file is present and works just fine. Go to the directory where the .cabal file is, and type cabal install without a package name. > A related issue: what should I do about the local copy of a module name > that is also installed? Can I make my project use that instead, without > having to globally change the name in all the files? You can use a GHC extension, to specify the package to use, see http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html look for Package-qualified imports Regards, Henk-Jan van Tuyl -- Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From ngnr63q02 at sneakemail.com Wed Apr 23 16:43:17 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Wed, 23 Apr 2014 11:43:17 -0500 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: On 4/23/2014 8:01 AM, Henk-Jan van Tuyl wrote: > > Go to the directory where the .cabal file is, and type > cabal install > without a package name. Ah, I see. I was looking for a makefile or something that was part of the package. But won't that "install" it, overwriting the normal one? > >> A related issue: what should I do about the local copy of a module name that is also >> installed? Can I make my project use that instead, without having to globally change >> the name in all the files? > > You can use a GHC extension, to specify the package to use, see > http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html > look for > Package-qualified imports > > Regards, > Henk-Jan van Tuyl > > From timmelzer at gmail.com Wed Apr 23 16:59:34 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Wed, 23 Apr 2014 18:59:34 +0200 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: Am 23.04.2014 18:44 schrieb "John M. Dlugosz" : > > On 4/23/2014 8:01 AM, Henk-Jan van Tuyl wrote: >> >> >> Go to the directory where the .cabal file is, and type >> cabal install >> without a package name. > > > Ah, I see. I was looking for a makefile or something that was part of the package. You can consider the *.cabal file a Makefile describing the package. > But won't that "install" it, overwriting the normal one? Yes. But you could enter the cabal-file and change the name of the package. There are ways to tell fh-wedel which package it shall use on compiletime. -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Wed Apr 23 19:36:18 2014 From: karl at karlv.net (Karl Voelker) Date: Wed, 23 Apr 2014 12:36:18 -0700 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: > On Apr 23, 2014, at 9:43 AM, "John M. Dlugosz" wrote: > > But won't that "install" it, overwriting the normal one? It would be a good idea to do all of this in a cabal sandbox. -Karl From peterjoel at gmail.com Wed Apr 23 20:27:55 2014 From: peterjoel at gmail.com (Peter Hall) Date: Wed, 23 Apr 2014 21:27:55 +0100 Subject: [Haskell-beginners] why do classes require the type variable in type signatures? In-Reply-To: <53577885.1080001@ucdavis.edu> References: <53577885.1080001@ucdavis.edu> Message-ID: Consider two instances of your class: data Foo = Foo data Bar = Bar instance AlsoSpecial Foo where -- This implementation *must* be constant True or False because -- we don't constrain b at all, so it has to work for all b. isAlsoSpecial b = True instance AlsoSpecial Bar where isAlsoSpecial b = False Now what would you expect this to output? isAlsoSpecial "hello" Peter On 23 April 2014 09:23, Dimitri DeFigueiredo wrote: > Hello All, > > Why does this compile > > class Special a where > isSpecial :: a -> Bool > > whereas, GHC 7.6 complains about this > > class AlsoSpecial a where > isAlsoSpecial :: b -> Bool > > > This is the error message I get: > > The class method `isAlsoSpecial' > mentions none of the type variables of the class AlsoSpecial a > When checking the class method: > isAlsoSpecial :: forall b. b -> Bool > In the class declaration for `AlsoSpecial' > > My question is: Why must I use the type variable of the class declaration > (i.e. *a*) in the type signature for the associated method? Is there a > fundamental reason for this or is it just a GHC specific limitation? I > didn't see a need for it when watching this video > > http://channel9.msdn.com/posts/MDCC-TechTalk-Classes- > Jim-but-not-as-we-know-them > > that explains the translation that GHC does to turn type classes into core. > > > Thanks! > > > Dimitri > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Thu Apr 24 03:59:59 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Wed, 23 Apr 2014 22:59:59 -0500 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: On 4/23/2014 11:59 AM, Norbert Melzer wrote: > > Yes. But you could enter the cabal-file and change the name of the package. There are ways > to tell fh-wedel which package it shall use on compiletime. > What's fh-wedel? GOogle only turns up a university of applied sciences in Germany. Changing the name of the package would not change the name of the modules inside it, or is there a prefix I can add automatically or something? From timmelzer at gmail.com Thu Apr 24 07:35:31 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Thu, 24 Apr 2014 09:35:31 +0200 Subject: [Haskell-beginners] How to build a package? In-Reply-To: References: Message-ID: Google is right on FH Wedel, however, what I really wanted to write was ghci instead. Auto correction on my must have gone wild ;-) Am 24.04.2014 06:01 schrieb "John M. Dlugosz" : > On 4/23/2014 11:59 AM, Norbert Melzer wrote: > >> >> Yes. But you could enter the cabal-file and change the name of the >> package. There are ways >> to tell fh-wedel which package it shall use on compiletime. >> >> > What's fh-wedel? GOogle only turns up a university of applied sciences in > Germany. > > Changing the name of the package would not change the name of the modules > inside it, or is there a prefix I can add automatically or something? > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Fri Apr 25 14:41:32 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 09:41:32 -0500 Subject: [Haskell-beginners] development workflow ? Message-ID: If you're working on a Haskell project that includes packages, and the packages are in subdirectories of the source tree, how do you "build"? In my case, I want to alter the "gloss" package so I unpacked it, changed the name to "customgloss" in the .cabal file, and installed. Meanwhile, I used the package-quallified import GHC feature. So I have to go into that subdirectory and run cabal install, let it recompile everything in there and copy it to the repository, then chdir back out and build/run my main program. How about a "make"-like way that knows if a source file in that package directory has changed, only recompiles the changed parts, and can put the output files in my directory tree with the source rather than copying to the machine repository? How do you normally go about developing a new version of a package? ?John From daniel.trstenjak at gmail.com Fri Apr 25 14:55:36 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Fri, 25 Apr 2014 16:55:36 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: <20140425145536.GB7761@machine> Hi John, On Fri, Apr 25, 2014 at 09:41:32AM -0500, John M. Dlugosz wrote: > How do you normally go about developing a new version of a package? A cabal sandbox with 'add-source' might help here. If your directory tree looks something like: /whatever/customgloss /whatever/yourproject Then you could do: cd /whatever/yourproject cabal sandbox init cabal sandbox add-source ../customgloss cabal install If your code in 'customgloss' changes, then calling again 'cabal install' in 'yourproject' will rebuild 'customgloss'. But you might rethink if copying 'gloss' that way is really a good long term solution. Greetings, Daniel From ngnr63q02 at sneakemail.com Fri Apr 25 14:56:16 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 09:56:16 -0500 Subject: [Haskell-beginners] easy question: transform Float -> Int how? Message-ID: If I have a value of type Float, and I want to pass it (approximately rounded) to a function wanting an Int, how do I do that? From ngnr63q02 at sneakemail.com Fri Apr 25 14:58:04 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 09:58:04 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425145536.GB7761@machine> References: <20140425145536.GB7761@machine> Message-ID: On 4/25/2014 9:55 AM, Daniel Trstenjak wrote: > But you might rethink if copying 'gloss' that way is really a good long term solution. > I have no idea what's a good idea; I'm just showing what I've learned thus far. What is the right way to develop a package? From toad3k at gmail.com Fri Apr 25 15:03:04 2014 From: toad3k at gmail.com (David McBride) Date: Fri, 25 Apr 2014 11:03:04 -0400 Subject: [Haskell-beginners] easy question: transform Float -> Int how? In-Reply-To: References: Message-ID: Use the round function (or ceiling or floor). On Fri, Apr 25, 2014 at 10:56 AM, John M. Dlugosz wrote: > If I have a value of type Float, and I want to pass it (approximately > rounded) to a function wanting an Int, how do I do that? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nrujac at gmail.com Fri Apr 25 15:03:36 2014 From: nrujac at gmail.com (Arjun Comar) Date: Fri, 25 Apr 2014 11:03:36 -0400 Subject: [Haskell-beginners] easy question: transform Float -> Int how? In-Reply-To: References: Message-ID: Hi John, This page should have you covered. Check section 3 especially. Thanks, Arjun On Fri, Apr 25, 2014 at 10:56 AM, John M. Dlugosz wrote: > If I have a value of type Float, and I want to pass it (approximately > rounded) to a function wanting an Int, how do I do that? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Fri Apr 25 15:06:08 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Fri, 25 Apr 2014 17:06:08 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: <20140425145536.GB7761@machine> Message-ID: <20140425150608.GA8860@machine> On Fri, Apr 25, 2014 at 09:58:04AM -0500, John M. Dlugosz wrote: > I have no idea what's a good idea; I'm just showing what I've > learned thus far. What is the right way to develop a package? Why have you copied gloss? Have you just added a new function? Have you modified the code? From ngnr63q02 at sneakemail.com Fri Apr 25 16:14:41 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 11:14:41 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425150608.GA8860@machine> References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> Message-ID: On 4/25/2014 10:06 AM, Daniel Trstenjak wrote: > On Fri, Apr 25, 2014 at 09:58:04AM -0500, John M. Dlugosz wrote: >> I have no idea what's a good idea; I'm just showing what I've >> learned thus far. What is the right way to develop a package? > > Why have you copied gloss? > Have you just added a new function? > Have you modified the code? > Because the circles are not round. I've modified one function. Maybe I'll do more with it and invite them to Pull. From daniel.trstenjak at gmail.com Fri Apr 25 16:26:48 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Fri, 25 Apr 2014 18:26:48 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> Message-ID: <20140425162648.GA11380@machine> On Fri, Apr 25, 2014 at 11:14:41AM -0500, John M. Dlugosz wrote: > Because the circles are not round. I've modified one function. > Maybe I'll do more with it and invite them to Pull. If you're changes are sensible and you could get your changes merged back, than this is certainly the way to go. If you can't get your changes merged back - or for the short term - it might be easier to just copy the specific function, modify and rename it. I don't know nothing about gloss, so would this approach be possible? Copying the whole package seems a bit excessive. Greetings, Daniel From ngnr63q02 at sneakemail.com Fri Apr 25 16:26:44 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 11:26:44 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425150608.GA8860@machine> References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> Message-ID: On 4/25/2014 10:06 AM, Daniel Trstenjak wrote: > On Fri, Apr 25, 2014 at 09:58:04AM -0500, John M. Dlugosz wrote: >> I have no idea what's a good idea; I'm just showing what I've >> learned thus far. What is the right way to develop a package? > > Why have you copied gloss? > Have you just added a new function? > Have you modified the code? > To be specific, in Graphics/Gloss/Internals/Render/Circle.hs {-# INLINE circleSteps #-} circleSteps :: Float -> Int circleSteps sDiam | sDiam < 8 = 8 | sDiam < 16 = 16 | sDiam < 32 = 32 | otherwise = round sDiam Originally, the otherwise branch said 64. When drawing my Pappas Chain, the outer circle was not tangent to all the little circles, but clipped through them and when zoomed was obviously a polygon and not a circle at all. So, why's it limited to 64? Maybe we don't want to go crazy if zoomed in deeply, and most of it doesn't show anyway. So I thought I might tinker with it some more to make it proper circle quality only where it intersects the viewport, and crude outside (if not omitted entirely). I might also turn on anti-aliasing, which if not exposed already means adding a function in the private guts to change the setting of the OpenGL handle. But more generally, everyone who develops a package and continues to develop a new version while the old one is installed and used by other components must be doing something (sandbox for this case, I think). And even if a body of code is not intended to be a general purpose library shipped on its own, a big project ought to be divided into parts, so it is desirable to know which files changed and do far less work to build than to compile every single source file again. ?John From ngnr63q02 at sneakemail.com Fri Apr 25 16:29:46 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Fri, 25 Apr 2014 11:29:46 -0500 Subject: [Haskell-beginners] easy question: transform Float -> Int how? In-Reply-To: References: Message-ID: On 4/25/2014 10:03 AM, Arjun Comar wrote: > Hi John, > > This page should have you covered. > Check section 3 especially. Very nice?thanks for the link. David: I must have overlooked round,truncate,ceiling. I think I didn't realize that they don't return the same type that they take as argument. Anyway, "round" worked. Thanks! From daniel.trstenjak at gmail.com Fri Apr 25 16:59:04 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Fri, 25 Apr 2014 18:59:04 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> Message-ID: <20140425165904.GA12577@machine> On Fri, Apr 25, 2014 at 11:26:44AM -0500, John M. Dlugosz wrote: > To be specific, in Graphics/Gloss/Internals/Render/Circle.hs > > {-# INLINE circleSteps #-} > circleSteps :: Float -> Int > circleSteps sDiam > | sDiam < 8 = 8 > | sDiam < 16 = 16 > | sDiam < 32 = 32 > | otherwise = round sDiam Ok, I see, if you have to modify such an internal function, then forking the package might really be the only solution. If you're using a cabal sandbox with 'add-source', then you might not need to rename the gloss package, if cabal first searches for dependencies in the added sources and then on hackage. But I'm not sure about this one. Greetings, Daniel From byorgey at seas.upenn.edu Fri Apr 25 17:39:35 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri, 25 Apr 2014 13:39:35 -0400 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425165904.GA12577@machine> References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> <20140425165904.GA12577@machine> Message-ID: <20140425173935.GA664@seas.upenn.edu> On Fri, Apr 25, 2014 at 06:59:04PM +0200, Daniel Trstenjak wrote: > > On Fri, Apr 25, 2014 at 11:26:44AM -0500, John M. Dlugosz wrote: > > To be specific, in Graphics/Gloss/Internals/Render/Circle.hs > > > > {-# INLINE circleSteps #-} > > circleSteps :: Float -> Int > > circleSteps sDiam > > | sDiam < 8 = 8 > > | sDiam < 16 = 16 > > | sDiam < 32 = 32 > > | otherwise = round sDiam > > Ok, I see, if you have to modify such an internal function, then > forking the package might really be the only solution. > > If you're using a cabal sandbox with 'add-source', then you > might not need to rename the gloss package, if cabal first searches > for dependencies in the added sources and then on hackage. Yes, that's what it does. Renaming the package should not be necessary. -Brent From martin.drautzburg at web.de Sun Apr 27 18:44:57 2014 From: martin.drautzburg at web.de (martin) Date: Sun, 27 Apr 2014 20:44:57 +0200 Subject: [Haskell-beginners] Clocks, scheduling, jack and supercollider Message-ID: <535D5029.9060702@web.de> Hello all, I just installed the haskell-supercollider bindings ("hsc3") and I am able to send events to supercollider and hear sound. But I have no idea how to schedule events to be sent in the future - a prerequisite to play a song. Ideally I would sync to an external clock source, like a time code (SMPTE/MIDI). Also it would be nice to listen to jack-transport, so I can fast-forward and rewind from another application. But the jack bindings for Haskell do not mention jack-transport. Do you have any advice where I should look next. From byorgey at seas.upenn.edu Sun Apr 27 19:04:51 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sun, 27 Apr 2014 15:04:51 -0400 Subject: [Haskell-beginners] Clocks, scheduling, jack and supercollider In-Reply-To: <535D5029.9060702@web.de> References: <535D5029.9060702@web.de> Message-ID: <20140427190451.GA4487@seas.upenn.edu> On Sun, Apr 27, 2014 at 08:44:57PM +0200, martin wrote: > Hello all, > > I just installed the haskell-supercollider bindings ("hsc3") and I am able to send events to supercollider and hear > sound. But I have no idea how to schedule events to be sent in the future - a prerequisite to play a song. > > Ideally I would sync to an external clock source, like a time code (SMPTE/MIDI). Also it would be nice to listen to > jack-transport, so I can fast-forward and rewind from another application. But the jack bindings for Haskell do not > mention jack-transport. > > Do you have any advice where I should look next. You might have more luck sending this question to the haskell-art mailing list: http://lurk.org/groups/haskell-art/ though I certainly don't intend to discourage such questions here. If someone here can answer it, so much the better! -Brent From ngnr63q02 at sneakemail.com Mon Apr 28 03:21:41 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 27 Apr 2014 22:21:41 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425162648.GA11380@machine> References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> <20140425162648.GA11380@machine> Message-ID: On 4/25/2014 11:26 AM, Daniel Trstenjak wrote: > > > If you can't get your changes merged back - or for the short > term - it might be easier to just copy the specific > function, modify and rename it. > > I don't know nothing about gloss, so would this approach be possible? > The function is an internal back-end detail, not one the program calls directly. So renaming it would mean changing the caller, etc. From ngnr63q02 at sneakemail.com Mon Apr 28 03:24:38 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 27 Apr 2014 22:24:38 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: <20140425173935.GA664@seas.upenn.edu> References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> <20140425165904.GA12577@machine> <20140425173935.GA664@seas.upenn.edu> Message-ID: OK, use sandbox and "add-source". But does that address the smart "make" concept? That is, will it automatically rebuild the gloss package if a file in it changed, when I compile the program that imports it? On 4/25/2014 12:39 PM, Brent Yorgey wrote: > On Fri, Apr 25, 2014 at 06:59:04PM +0200, Daniel Trstenjak wrote: >> >> On Fri, Apr 25, 2014 at 11:26:44AM -0500, John M. Dlugosz wrote: >>> To be specific, in Graphics/Gloss/Internals/Render/Circle.hs >>> >>> {-# INLINE circleSteps #-} >>> circleSteps :: Float -> Int >>> circleSteps sDiam >>> | sDiam < 8 = 8 >>> | sDiam < 16 = 16 >>> | sDiam < 32 = 32 >>> | otherwise = round sDiam >> >> Ok, I see, if you have to modify such an internal function, then >> forking the package might really be the only solution. >> >> If you're using a cabal sandbox with 'add-source', then you >> might not need to rename the gloss package, if cabal first searches >> for dependencies in the added sources and then on hackage. > > Yes, that's what it does. Renaming the package should not be > necessary. > > -Brent > From ngnr63q02 at sneakemail.com Mon Apr 28 03:59:27 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Sun, 27 Apr 2014 22:59:27 -0500 Subject: [Haskell-beginners] annoying precedence of unary negate Message-ID: [ ? , translate (-50) 0 $ color green $ Circle 50 , ?] The parens around -50 are necessary. Is there a more elegant way to write it? Without the parens, I don't understand what the compiler sees it as, based on the error message. How does the compiler parse it in that case? Also, is the comma in a list different from the operator comma which is noted as right-associative precedence 5? I had thought that the item separator in the list was special syntax that had very low precedence. After all, I can write f$g as a list item without parens, and $ is lower than comma. What is comma (as an operator) used for? From magnus at therning.org Mon Apr 28 04:24:47 2014 From: magnus at therning.org (Magnus Therning) Date: Mon, 28 Apr 2014 06:24:47 +0200 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: Message-ID: <20140428042447.GC1212@tatooine.lan> On Sun, Apr 27, 2014 at 10:59:27PM -0500, John M. Dlugosz wrote: > > [ ? , translate (-50) 0 $ color green $ Circle 50 , ?] > > The parens around -50 are necessary. > Is there a more elegant way to write it? > > Without the parens, I don't understand what the compiler sees it as, based > on the error message. How does the compiler parse it in that case? It most likely sees it as the binary minus rather than the unary minus. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus The results point out the fragility of programmer expertise: advanced programmers have strong expectations about what programs should look like, and when those expectations are violated--in seemingly innocuous ways--their performance drops drastically. -- Elliot Soloway and Kate Ehrlich -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From ky3 at atamo.com Mon Apr 28 05:13:58 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 28 Apr 2014 12:13:58 +0700 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: Message-ID: On Mon, Apr 28, 2014 at 10:59 AM, John M. Dlugosz wrote: > > [ ? , translate (-50) 0 $ color green $ Circle 50 , ?] > > The parens around -50 are necessary. > Is there a more elegant way to write it? > > Without the parens, I don't understand what the compiler sees it as, based > on the error message. How does the compiler parse it in that case? Without the parens, minus would be parsed just like plus. What would 'translate +50 0 blah' parse into? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From friedrichwiemer at gmail.com Mon Apr 28 06:53:14 2014 From: friedrichwiemer at gmail.com (Friedrich Wiemer) Date: Mon, 28 Apr 2014 08:53:14 +0200 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: Message-ID: <535DFADA.5070907@gmail.com> > What is comma (as an operator) used for? it's used to build tuples: > ?: (,) 4 2 > (4,2) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 555 bytes Desc: OpenPGP digital signature URL: From ky3 at atamo.com Mon Apr 28 07:08:24 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 28 Apr 2014 14:08:24 +0700 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: Message-ID: On Mon, Apr 28, 2014 at 10:59 AM, John M. Dlugosz wrote: > Also, is the comma in a list different from the operator comma which is > noted as right-associative precedence 5? I had thought that the item > separator in the list was special syntax that had very low precedence. The _colon_ is a non-rebindable special-syntax operator with infixr 5. Otoh, the _comma_ in a list is _not_ an operator but merely an item separator. Comma-separated list notation e.g. [1,2,3] is special syntax that desugars to e.g. 1:2:3:[]. Other than that, the colon and comma don't have anything else in common. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Mon Apr 28 09:13:29 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 28 Apr 2014 04:13:29 -0500 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: Message-ID: On 4/28/2014 2:08 AM, Kim-Ee Yeoh wrote: > > On Mon, Apr 28, 2014 at 10:59 AM, John M. Dlugosz > wrote: > > Also, is the comma in a list different from the operator comma which is noted as > right-associative precedence 5? I had thought that the item separator in the list was > special syntax that had very low precedence. > > > The _colon_ is a non-rebindable special-syntax operator with infixr 5. > > Otoh, the _comma_ in a list is _not_ an operator but merely an item separator. > > Comma-separated list notation e.g. [1,2,3] is special syntax that desugars to e.g. 1:2:3:[]. > > Other than that, the colon and comma don't have anything else in common. > > -- Kim-Ee > > I misread the chart: the ?:,++? meant to use the comma to separate two operators, not be one of three operators listed. From ngnr63q02 at sneakemail.com Mon Apr 28 09:30:49 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Mon, 28 Apr 2014 04:30:49 -0500 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: <20140428042447.GC1212@tatooine.lan> References: <20140428042447.GC1212@tatooine.lan> Message-ID: On 4/27/2014 11:24 PM, Magnus Therning wrote: > On Sun, Apr 27, 2014 at 10:59:27PM -0500, John M. Dlugosz wrote: >> > > It most likely sees it as the binary minus rather than the unary minus. > That's what I thought. Couldn't match expected type `Picture' with actual type `Float -> Picture -> Picture' In the expression: translate - 50 0 $ color green (Circle 50) In the first argument of `Pictures', namely `[Circle 100, translate - 50 0 $ color green (Circle 50), scale 200 200 $ color red $ (Pictures $ take 20 chain1), testcirc]' In the expression: Pictures [Circle 100, translate - 50 0 $ color green (Circle 50), scale 200 200 $ color red $ (Pictures $ take 20 chain1), testcirc] But what is the error message telling me? Given that infix is done after adjacency application, it should parse as: ((translate - (50 0) ) $ (color (green (Circle 50)))) Left of the $, that is the parse tree subtract+ | + translate | + apply+ | + 50 | + 0 I think it would complain that 50 isn't a function, or the first argument of subtract is not a Num but a function translate :: Float -> Float -> Picture -> Picture, or that the argument of translate isn't a Float but something it can't make sense of. Why is it looking for a Picture? Where is it getting Float->Picture->Picture (seems to be a curried translate? But the next token is not something it would like so how can it find a first argument?) Understanding the compiler's errors is a skill I want to learn, as well as shake out my understanding of what's really going on. Thanks, ?John From magnus at therning.org Mon Apr 28 10:43:10 2014 From: magnus at therning.org (Magnus Therning) Date: Mon, 28 Apr 2014 12:43:10 +0200 Subject: [Haskell-beginners] annoying precedence of unary negate In-Reply-To: References: <20140428042447.GC1212@tatooine.lan> Message-ID: On Mon, Apr 28, 2014 at 11:30 AM, John M. Dlugosz wrote: > On 4/27/2014 11:24 PM, Magnus Therning wrote: >> It most likely sees it as the binary minus rather than the unary minus. > > That's what I thought. > > Couldn't match expected type `Picture' > with actual type `Float -> Picture -> Picture' > In the expression: translate - 50 0 $ color green (Circle 50) > In the first argument of `Pictures', namely > `[Circle 100, translate - 50 0 $ color green (Circle 50), > scale 200 200 $ color red $ (Pictures $ take 20 chain1), testcirc]' > In the expression: > Pictures > [Circle 100, translate - 50 0 $ color green (Circle 50), > scale 200 200 $ color red $ (Pictures $ take 20 chain1), testcirc] > > But what is the error message telling me? > Given that infix is done after adjacency application, it should parse as: > ((translate - (50 0) ) $ (color (green (Circle 50)))) > > Left of the $, that is the parse tree > > subtract+ > | > + translate > | > + apply+ > | > + 50 > | > + 0 > > I think it would complain that 50 isn't a function, or the first argument of > subtract is not a Num but a function > translate :: Float -> Float -> Picture -> Picture, > or that the argument of translate isn't a Float but something it can't make > sense of. > Why is it looking for a Picture? Where is it getting > Float->Picture->Picture (seems to be a curried translate? But the next > token is not something it would like so how can it find a first argument?) > > Understanding the compiler's errors is a skill I want to learn, as well as > shake out my understanding of what's really going on. It's not easy to answer that without knowing the types of the involved values/functions. You can always play around a bit in ghci with the ':type' command to see if you can work it out for yourself :) It can be confusing though, functions are values too, and precedence rules come into play too. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus at therning.org jabber: magnus at therning.org twitter: magthe http://therning.org/magnus From timmelzer at gmail.com Mon Apr 28 15:49:28 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Mon, 28 Apr 2014 17:49:28 +0200 Subject: [Haskell-beginners] Building a tree Message-ID: Hi! I have to implement a solver for the game 2048 and decided to use Haskell from the pool of possible languages. My current implementation has a fixed recursion depth of 5 and simply sums up the possible score the best line of moves would give. One problem with this implementation is, that in some cases obvious defends are favoured because of the points that the dead end grants in the short term. So I now want to build up a tree of moves and pick that move that has the deepest subtree. My problem now is, that I haven't worked with trees before, so I don't have any idea how to build it up recursively. Also, since I don't consider new stones that would spawn after doing my move, I might create infinite subterranean just swiping left and right or up and down or anything else, how would I be able to consider such subtrees as a game over on the first repetition? TIA Norbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Mon Apr 28 15:59:50 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 28 Apr 2014 17:59:50 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: <20140425145536.GB7761@machine> <20140425150608.GA8860@machine> <20140425165904.GA12577@machine> <20140425173935.GA664@seas.upenn.edu> Message-ID: <20140428155950.GA13973@machine> On Sun, Apr 27, 2014 at 10:24:38PM -0500, John M. Dlugosz wrote: > OK, use sandbox and "add-source". > But does that address the smart "make" concept? That is, will it > automatically rebuild the gloss package if a file in it changed, > when I compile the program that imports it? Yes. From maydwell at gmail.com Tue Apr 29 01:09:07 2014 From: maydwell at gmail.com (Lyndon Maydwell) Date: Tue, 29 Apr 2014 11:09:07 +1000 Subject: [Haskell-beginners] Building a tree In-Reply-To: References: Message-ID: There are some nice "unfold" style methods available in Data.Tree if you're looking for a library to help out. I've gotten into the habit of generating all possibilities lazily and then pruning rather than doing the pruning during generation, so unfold works well with this. Is that the kind of thing you're after? On Tue, Apr 29, 2014 at 1:49 AM, Norbert Melzer wrote: > Hi! > > I have to implement a solver for the game 2048 and decided to use Haskell > from the pool of possible languages. > > My current implementation has a fixed recursion depth of 5 and simply sums > up the possible score the best line of moves would give. One problem with > this implementation is, that in some cases obvious defends are favoured > because of the points that the dead end grants in the short term. > > So I now want to build up a tree of moves and pick that move that has the > deepest subtree. > > My problem now is, that I haven't worked with trees before, so I don't have > any idea how to build it up recursively. Also, since I don't consider new > stones that would spawn after doing my move, I might create infinite > subterranean just swiping left and right or up and down or anything else, > how would I be able to consider such subtrees as a game over on the first > repetition? > > TIA > Norbert > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From lortabac at gmx.com Mon Apr 28 18:26:21 2014 From: lortabac at gmx.com (Lorenzo Tabacchini) Date: Mon, 28 Apr 2014 20:26:21 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field Message-ID: <535E9D4D.3040000@gmx.com> Imagine the following data type: data Person = Child { childAge :: Int, school :: School } | Adult { adultAge :: Int, job :: Job } Both the alternatives share an "age" field, but in order to access it we are obliged to match all the constructors: personAge :: Person -> Int personAge (Child {childAge = age}) = age personAge (Adult {adultAge = age}) = age Is there a way to define a common field in a data type (somehow like inheritance in the OOP world)? It would be practical if we could define "age :: Int" as a common field and do something like: personAge (_ {age = age}) = age An alternative solution could be extracting the varying fields in a different type: data WorkingPerson = Child School | Adult Job data Person = Person { age :: Int, workingPerson :: WorkingPerson } Or to make Person a type class (which would probably require existential types in order to be useful). But the first one looks simpler and more natural. Does this feature exist (maybe in other forms) and, if not, is there a reason? Would it make sense to have a GHC extension (or even to make a Template Haskell hack) for this feature? -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Tue Apr 29 10:13:04 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Tue, 29 Apr 2014 12:13:04 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535E9D4D.3040000@gmx.com> References: <535E9D4D.3040000@gmx.com> Message-ID: <20140429101304.GA8577@machine> Hi Lorenzo, I don't think that the first one is more natural, because you could define a child like: Child { childAge = 99, ... }, but you also might have someone quite old still studying, so I might go with something like: data Occupation = Student School | Employee Job data Person = Person { age :: Int, occupation :: Occupation } But if you really want the first one, then you're most likely only one "lens"[1] away from a solution: age :: Lens' Person Int age = lens getAge setAge where getAge Child { childAge = age } = age getAge Adult { adultAge = age } = age setAge (c at Child {}) newAge = c { childAge = newAge } setAge (a at Adult {}) newAge = a { adultAge = newAge } To get the age of a person you could now write: person ^. age And to modify it: person & age .~ 22 But I wouldn't prefer the lens solution. Greetings, Daniel [1] https://hackage.haskell.org/package/lens From edwards.benj at gmail.com Tue Apr 29 10:45:00 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Tue, 29 Apr 2014 10:45:00 +0000 Subject: [Haskell-beginners] Type constructors sharing a common field References: <535E9D4D.3040000@gmx.com> <20140429101304.GA8577@machine> Message-ID: In addition to the comments made by Daniel, as a general rule, you don?t want a sum type with selectors functions as your example stands, because you can end up with compiling programs like this: module Main where main :: IO ()main = let p = Child 10 age = adultAge p in print age this craps out with an error at runtime. -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Tue Apr 29 11:57:18 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Tue, 29 Apr 2014 13:57:18 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: References: <535E9D4D.3040000@gmx.com> <20140429101304.GA8577@machine> Message-ID: > this craps out with an error at runtime. If you're trying to promote Haskell as a safe language, then this behaviour is always a bit humbling. -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Tue Apr 29 12:08:27 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Tue, 29 Apr 2014 14:08:27 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535E9D4D.3040000@gmx.com> References: <535E9D4D.3040000@gmx.com> Message-ID: <949260567b67c70c862771a924a8fc96@roundcube.froglogic.com> On 2014-04-28 20:26, Lorenzo Tabacchini wrote: > Imagine the following data type: > > data Person = Child { childAge :: Int, school :: School } > | Adult { adultAge :: Int, job :: Job } > > Both the alternatives share an "age" field, but in order to access it we > are obliged to match all the constructors: > > personAge :: Person -> Int > personAge (Child {childAge = age}) = age > personAge (Adult {adultAge = age}) = age > > Is there a way to define a common field in a data type (somehow like > inheritance in the OOP world)? Since you already have dedicated types for the school and the job, why not have a descriptive name for the age as well so that you can drop the accessor functions altogether? E.g. type Age = Int data Person = Child Age School | Adult Age Job If needed, you could of course still define age :: Person -> Age age (Child x _) = x age (Adult x _) = x but you may also find that you don't even need such a function in the first place, pattern matching may do the job just fine: mayRideRollerCoaster :: Person -> Bool mayRideRollerCoaster (Child age _) = age > 12 mayRideRollerCoaster _ = True -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From michael at orlitzky.com Tue Apr 29 13:07:17 2014 From: michael at orlitzky.com (Michael Orlitzky) Date: Tue, 29 Apr 2014 09:07:17 -0400 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535E9D4D.3040000@gmx.com> References: <535E9D4D.3040000@gmx.com> Message-ID: <535FA405.9070908@orlitzky.com> On 04/28/2014 02:26 PM, Lorenzo Tabacchini wrote: > > Would it make sense to have a GHC extension (or even to make a Template > Haskell hack) for this feature? > http://www.well-typed.com/blog/84/ From lortabac at gmx.com Mon Apr 28 22:35:14 2014 From: lortabac at gmx.com (Lorenzo Tabacchini) Date: Tue, 29 Apr 2014 00:35:14 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535FA405.9070908@orlitzky.com> References: <535FA405.9070908@orlitzky.com> Message-ID: <535ED7A2.3060501@gmx.com> > http://www.well-typed.com/blog/84/ Thank you! It's the kind of solution I was looking for. Apparently I'll have to wait for GHC 7.10. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lortabac at gmx.com Tue Apr 29 00:32:38 2014 From: lortabac at gmx.com (Lorenzo Tabacchini) Date: Tue, 29 Apr 2014 02:32:38 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: References: Message-ID: <535EF326.8000307@gmx.com> If you could have a single polymorphic "age" function for all types having an age, you could never have this runtime error, because the compiler would infer which "age" we are referring to. The GHC OverloadedRecordFields extension that Michael linked seems to do what I am looking for (http://www.well-typed.com/blog/84/). From edwards.benj at gmail.com Tue Apr 29 15:55:43 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Tue, 29 Apr 2014 15:55:43 +0000 Subject: [Haskell-beginners] Type constructors sharing a common field References: <535EF326.8000307@gmx.com> Message-ID: On Tue Apr 29 2014 at 16:42:40, Lorenzo Tabacchini lortabac at gmx.comwrote: If you could have a single polymorphic "age" function for all types > having an age, you could never have this runtime error, because the > compiler would infer which "age" we are referring to. > The GHC OverloadedRecordFields extension that Michael linked seems to do > what I am looking for (http://www.well-typed.com/blog/84/). > This is accurate. In the meantime avoid partiality as much as possible. If you must have an age accessor, better to define the lens explicitly. module Main where import Control.Applicativeimport Control.Lens main :: IO ()main = let p = Child 10 in print $ p ^. age data Person = Child Int | Adult Int age :: Lens' Person Intage k (Child x) = Child <$> k xage k (Adult x) = Adult <$> k x Yeah it?s boilerplate. You could makeLenses and then not export the automatically generated selector functions. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chaddai.fouche at gmail.com Tue Apr 29 17:12:10 2014 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Tue, 29 Apr 2014 19:12:10 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: On Fri, Apr 25, 2014 at 4:41 PM, John M. Dlugosz wrote: > If you're working on a Haskell project that includes packages, and the > packages are in subdirectories of the source tree, how do you "build"? > > In my case, I want to alter the "gloss" package so I unpacked it, changed > the name to "customgloss" in the .cabal file, and installed. Meanwhile, I > used the package-quallified import GHC feature. > > While that's not the question you asked, I note that you unpacked gloss which probably means "cabal unpack gloss" (?). This is in general a terrible idea because the latest released version may not be up to the development version and if you want to work sanely, you'll have to put this code in revision control, and you won't use the same as the author and so on... So when you send a patch he may not be able to simply apply it to his latest version and then it'll sit forgotten in his mail box and your work will be lost for the community (ok, I may be overdoing it here...). So the sane alternative is to check on the hackage page if a repository is specified, like here : "git clone https://github.com/benl23x5/gloss" will get you the latest and greatest, already in revision control and facilitate the creation of a pull request in the future (in fact if you have a github account, it's even better to go to this page and fork his repo to have your own version on github). -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Tue Apr 29 18:12:15 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Tue, 29 Apr 2014 12:12:15 -0600 Subject: [Haskell-beginners] Functors in Haskell Message-ID: <2872aac109b41fac1190d6a73e34b240@pobox.com> Hi All, I'm trying to merge in my head the concepts of: - functors from category theory; with, - the Functor typeclass in haskell. Here's what I have come up with: A Functor F in haskell is a type constructor that provides a functor map (fmap) and that maps Hask (the category of haskell types) into a category generated by the Functor. For example, Maybe maps the category of all haskell types "a" (i.e. Hask) into the category of all types of the form "Maybe a" by providing an implementation of fmap. Neglecting the fact that apparently Hask isn't a category, does this sound reasonable? I think the insight for me here is that functors in haskell are type constructors mapping Hask into another category. Is that a good way to think about it? Thanks! Dimitri From rein.henrichs at gmail.com Tue Apr 29 18:35:30 2014 From: rein.henrichs at gmail.com (Rein Henrichs) Date: Tue, 29 Apr 2014 11:35:30 -0700 Subject: [Haskell-beginners] Functors in Haskell In-Reply-To: <2872aac109b41fac1190d6a73e34b240@pobox.com> References: <2872aac109b41fac1190d6a73e34b240@pobox.com> Message-ID: Yes, this is reasonable, but it might be useful to be a bit more explicit about the categories involved. An instance of Functor is a functor which maps objects and arrows in the category of Hask (of Haskell types and functions) to... objects and arrows in the category of Hask. It is an endofunctor. The object mapping must map a type to a type and therefore must have the kind * -> *. This means that it is a type constructor, e.g., Maybe. The arrow mapping must map arrows to arrows, which means it must map functions to functions. fmap provides this mapping and the Functor laws in Haskell are of course equivalent to the laws from category theory. And while the inclusion of bottom is problematic for Hask, it is still possible to reason about it in a "morally correct" way ( http://www.cse.chalmers.se/~nad/publications/danielsson-et-al-popl2006.pdf). -------------- next part -------------- An HTML attachment was scrubbed... URL: From ari.brandeis.king at gmail.com Tue Apr 29 18:50:48 2014 From: ari.brandeis.king at gmail.com (Ari King) Date: Tue, 29 Apr 2014 14:50:48 -0400 Subject: [Haskell-beginners] DB + CSV + Mail Message-ID: Hi, I'd like to port a python script that queries a DB, writes the results to a CSV file, and emails that file. I'd appreciate suggestions on libraries and/or approaches that can help me achieve this. For DB querying I was thinking of using HDBC. Best, Ari -------------- next part -------------- An HTML attachment was scrubbed... URL: From jmacristovao at gmail.com Tue Apr 29 19:03:12 2014 From: jmacristovao at gmail.com (=?UTF-8?B?Sm/Do28gQ3Jpc3TDs3bDo28=?=) Date: Tue, 29 Apr 2014 20:03:12 +0100 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: > "git clone https://github.com/benl23x5/gloss" will get you > the latest and greatest Or, for a recent enough cabal, and properly written cabal files, like the one from gloss: cabal get -s gloss 2014-04-29 18:12 GMT+01:00 Chadda? Fouch? : > On Fri, Apr 25, 2014 at 4:41 PM, John M. Dlugosz > wrote: > >> If you're working on a Haskell project that includes packages, and the >> packages are in subdirectories of the source tree, how do you "build"? >> >> In my case, I want to alter the "gloss" package so I unpacked it, changed >> the name to "customgloss" in the .cabal file, and installed. Meanwhile, I >> used the package-quallified import GHC feature. >> >> > While that's not the question you asked, I note that you unpacked gloss > which probably means "cabal unpack gloss" (?). This is in general a > terrible idea because the latest released version may not be up to the > development version and if you want to work sanely, you'll have to put this > code in revision control, and you won't use the same as the author and so > on... So when you send a patch he may not be able to simply apply it to his > latest version and then it'll sit forgotten in his mail box and your work > will be lost for the community (ok, I may be overdoing it here...). > > So the sane alternative is to check on the hackage page if a repository is > specified, like here : "git clone https://github.com/benl23x5/gloss" will > get you the latest and greatest, already in revision control and facilitate > the creation of a pull request in the future (in fact if you have a github > account, it's even better to go to this page and fork his repo to have your > own version on github). > > -- > Jeda? > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Tue Apr 29 19:05:13 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Tue, 29 Apr 2014 13:05:13 -0600 Subject: [Haskell-beginners] Functors in Haskell In-Reply-To: References: <2872aac109b41fac1190d6a73e34b240@pobox.com> Message-ID: <535FF7E9.2040507@ucdavis.edu> Wow, this makes it very clear! So, the object mapping is provided by the type constructor and the arrow mapping by fmap. Thanks :-) Em 29/04/14 12:35, Rein Henrichs escreveu: > Yes, this is reasonable, but it might be useful to be a bit more > explicit about the categories involved. > > An instance of Functor is a functor which maps objects and arrows in > the category of Hask (of Haskell types and functions) to... objects > and arrows in the category of Hask. It is an endofunctor. The object > mapping must map a type to a type and therefore must have the kind * > -> *. This means that it is a type constructor, e.g., Maybe. The arrow > mapping must map arrows to arrows, which means it must map functions > to functions. fmap provides this mapping and the Functor laws in > Haskell are of course equivalent to the laws from category theory. > > And while the inclusion of bottom is problematic for Hask, it is still > possible to reason about it in a "morally correct" way > (http://www.cse.chalmers.se/~nad/publications/danielsson-et-al-popl2006.pdf > ). > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Tue Apr 29 19:22:36 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue, 29 Apr 2014 15:22:36 -0400 Subject: [Haskell-beginners] Functors in Haskell In-Reply-To: <535FF7E9.2040507@ucdavis.edu> References: <2872aac109b41fac1190d6a73e34b240@pobox.com> <535FF7E9.2040507@ucdavis.edu> Message-ID: <20140429192236.GA31602@seas.upenn.edu> Right. Note, your original formulation---saying that a Functor instance maps from Hask to some subcategory of Hask (e.g. all types of the form Maybe X) was not wrong. It's just not a very useful way to think about it; as Rein described it's more useful to think of Functor instances as functors Hask -> Hask. -Brent On Tue, Apr 29, 2014 at 01:05:13PM -0600, Dimitri DeFigueiredo wrote: > Wow, this makes it very clear! > > So, the object mapping is provided by the type constructor and the > arrow mapping by fmap. > > Thanks :-) > > > Em 29/04/14 12:35, Rein Henrichs escreveu: > >Yes, this is reasonable, but it might be useful to be a bit more > >explicit about the categories involved. > > > >An instance of Functor is a functor which maps objects and arrows > >in the category of Hask (of Haskell types and functions) to... > >objects and arrows in the category of Hask. It is an endofunctor. > >The object mapping must map a type to a type and therefore must > >have the kind * -> *. This means that it is a type constructor, > >e.g., Maybe. The arrow mapping must map arrows to arrows, which > >means it must map functions to functions. fmap provides this > >mapping and the Functor laws in Haskell are of course equivalent to > >the laws from category theory. > > > >And while the inclusion of bottom is problematic for Hask, it is > >still possible to reason about it in a "morally correct" way (http://www.cse.chalmers.se/~nad/publications/danielsson-et-al-popl2006.pdf ). > > > > > >_______________________________________________ > >Beginners mailing list > >Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From ngnr63q02 at sneakemail.com Tue Apr 29 19:37:10 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 29 Apr 2014 14:37:10 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: On 4/29/2014 12:12 PM, Chadda? Fouch? wrote: > So the sane alternative is to check on the hackage page if a repository is specified, like > here : "git clone https://github.com/benl23x5/gloss" will get you the latest and greatest, > already in revision control and facilitate the creation of a pull request in the future > (in fact if you have a github account, it's even better to go to this page and fork his > repo to have your own version on github). > Thanks. I was thinking as much if I make more than a trivial change and want to contribute. I was wondering if the local repository subdirectory needs to be anyplace in particular, or cabal "add source" just takes care of it wherever it's located? ?John From ngnr63q02 at sneakemail.com Tue Apr 29 19:37:52 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Tue, 29 Apr 2014 14:37:52 -0500 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: On 4/29/2014 2:03 PM, Jo?o Crist?v?o wrote: > > "git clone https://github.com/benl23x5/gloss" will get you > > the latest and greatest > > Or, for a recent enough cabal, and properly written cabal files, like the one from gloss: > > cabal get -s gloss > cool! From chaddai.fouche at gmail.com Tue Apr 29 20:08:35 2014 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Tue, 29 Apr 2014 22:08:35 +0200 Subject: [Haskell-beginners] development workflow ? In-Reply-To: References: Message-ID: On Tue, Apr 29, 2014 at 9:03 PM, Jo?o Crist?v?o wrote: > > "git clone https://github.com/benl23x5/gloss" will get you > > the latest and greatest > > Or, for a recent enough cabal, and properly written cabal files, like the > one from gloss: > > cabal get -s gloss > Very nice, I didn't know this command but cabal has really become a nice tool to put in place and keep up-to-date a development environment covering several packages ! -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Apr 29 20:54:18 2014 From: toad3k at gmail.com (David McBride) Date: Tue, 29 Apr 2014 16:54:18 -0400 Subject: [Haskell-beginners] DB + CSV + Mail In-Reply-To: References: Message-ID: Use mysql-simple/postgresql-simple for database. Use cassava for csv, and umm I know there is a library for mailing, but I don't recall what it is. On Tue, Apr 29, 2014 at 2:50 PM, Ari King wrote: > Hi, > > I'd like to port a python script that queries a DB, writes the results to > a CSV file, and emails that file. I'd appreciate suggestions on libraries > and/or approaches that can help me achieve this. > > For DB querying I was thinking of using HDBC. > > Best, > Ari > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Tue Apr 29 20:56:23 2014 From: cma at bitemyapp.com (Christopher Allen) Date: Tue, 29 Apr 2014 15:56:23 -0500 Subject: [Haskell-beginners] DB + CSV + Mail In-Reply-To: References: Message-ID: https://hackage.haskell.org/package/smtp-mail On Tue, Apr 29, 2014 at 3:54 PM, David McBride wrote: > Use mysql-simple/postgresql-simple for database. Use cassava for csv, and > umm I know there is a library for mailing, but I don't recall what it is. > > > On Tue, Apr 29, 2014 at 2:50 PM, Ari King wrote: > >> Hi, >> >> I'd like to port a python script that queries a DB, writes the results to >> a CSV file, and emails that file. I'd appreciate suggestions on libraries >> and/or approaches that can help me achieve this. >> >> For DB querying I was thinking of using HDBC. >> >> Best, >> Ari >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hallgren at chalmers.se Wed Apr 30 10:02:44 2014 From: hallgren at chalmers.se (Thomas Hallgren) Date: Wed, 30 Apr 2014 11:02:44 +0100 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535E9D4D.3040000@gmx.com> References: <535E9D4D.3040000@gmx.com> Message-ID: Hi, On 2014-04-28 19:26, Lorenzo Tabacchini wrote: > Imagine the following data type: > > data Person = Child { childAge :: Int, school :: School } > | Adult { adultAge :: Int, job :: Job } > > Both the alternatives share an "age" field, but in order to access it we are > obliged to match all the constructors: > > personAge :: Person -> Int > personAge (Child {childAge = age}) = age > personAge (Adult {adultAge = age}) = age > Is there a way to define a common field in a data type (somehow like inheritance in the OOP world)? Yes, there is: data Person = Child { personAge :: Int, school :: School } | Adult { personAge :: Int, job :: Job } No extension needed! Thomas H From lortabac at gmx.com Tue Apr 29 05:42:13 2014 From: lortabac at gmx.com (Lorenzo Tabacchini) Date: Tue, 29 Apr 2014 07:42:13 +0200 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: References: Message-ID: <535F3BB5.9090509@gmx.com> > Yes, there is: > > data Person = Child { personAge :: Int, school :: School } > | Adult { personAge :: Int, job :: Job } > > > No extension needed! Thanks! This helps reduce the number of field names, but it does not address the main problem, which is the obligation of pattern matching on all constructors. The Person type is just a simple example. If you have a lot of constructors repeating all of them without a real reason can be time-consuming and boring. I am aware that the real reason of the problem may be a wrong architecture, but I still have the feeling that my solution does have some use cases. From edwards.benj at gmail.com Wed Apr 30 13:29:02 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Wed, 30 Apr 2014 13:29:02 +0000 Subject: [Haskell-beginners] Type constructors sharing a common field References: <535F3BB5.9090509@gmx.com> Message-ID: > > Thanks! > This helps reduce the number of field names, but it does not address the > main problem, which is the obligation of pattern matching on all > constructors. > The Person type is just a simple example. If you have a lot of > constructors repeating all of them without a real reason can be > time-consuming and boring. > > I am aware that the real reason of the problem may be a wrong > architecture, but I still have the feeling that my solution does have some > use cases. > If you use a sum type, you *always* need to check every case. That is what you sign up for. If you want to express a commonality that should expressed in the type [1]. Otherwise if you extend Person with a constructor that has *no* notion of age then you cannot have a total function from Person -> Int. You would want something like a Prism or just a simple function from Person -> Maybe Int. Try to avoid non-total functions. That way lies madness. [1] as has already been suggested, this involves factoring out the things that are common from the things that aren't data SomeSum = Part1 String | Part2 Int | Part3 data Common a = Common Int a data Composite = Common SomeSum Now you can see that a function wanting the common Int must necessarily succeed when applied to Composite, but that there is no total function Composite -> String that accesses the String value held by Part1. I hope this is useful to you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Wed Apr 30 13:38:06 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Wed, 30 Apr 2014 08:38:06 -0500 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: References: <535EF326.8000307@gmx.com> Message-ID: On 4/29/2014 10:55 AM, Benjamin Edwards wrote: > |module Mainwhere > > import Control.Applicative > import Control.Lens > > main ::IO () > main =let p =Child 10 > in print $ p ^. age > > data Person =Child Int |Adult Int > > age ::Lens' Person Int > age k (Child x) =Child <$> k x > age k (Adult x) =Adult <$> k x > | Hmm, what does the <$> mean here? I know it for functor application, but I don't understand how that works in this example. From edwards.benj at gmail.com Wed Apr 30 13:51:56 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Wed, 30 Apr 2014 13:51:56 +0000 Subject: [Haskell-beginners] Type constructors sharing a common field References: <535EF326.8000307@gmx.com> Message-ID: > > Hmm, what does the <$> mean here? I know it for functor application, but > I don't > understand how that works in this example. > It's a synonym for fmap. Lens' is a type synonym. age really has function type forall Functor f. (Int -> f Int) -> Person -> f Person. Applying k x gives me f Int and fmapping the constructor gives me f Person. If you want to read an excellent but challenging article on why lenses work this way click here . Otherwise the signature should give you enough to be able to play type tetris to figure out where the fmap comes in (hopefully). This isn't super beginner friendly, but it's where haskell seems to be headed these days. Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From ngnr63q02 at sneakemail.com Wed Apr 30 14:54:27 2014 From: ngnr63q02 at sneakemail.com (John M. Dlugosz) Date: Wed, 30 Apr 2014 09:54:27 -0500 Subject: [Haskell-beginners] Type constructors sharing a common field In-Reply-To: <535F3BB5.9090509@gmx.com> References: <535F3BB5.9090509@gmx.com> Message-ID: On 4/29/2014 12:42 AM, Lorenzo Tabacchini wrote: >> Yes, there is: >> >> data Person = Child { personAge :: Int, school :: School } >> | Adult { personAge :: Int, job :: Job } >> >> >> No extension needed! > > > Thanks! > This helps reduce the number of field names, but it does not address the main problem, > which is the obligation of pattern matching on all constructors. > The Person type is just a simple example. If you have a lot of constructors repeating all > of them without a real reason can be time-consuming and boring. > > I am aware that the real reason of the problem may be a wrong architecture, but I still > have the feeling that my solution does have some use cases. > That reminds me of database design: Normalize! In this case, Daniel's suggestion is in line with the theory. With varying numbers of fields between the different kinds of people, it suggests making separate tables and joining them to produce the child or adult views. More generally, perhaps database schema rather than Object Oriented is a better place to transfer engineering skills from when it comes to designing the information model. ?John From gilbertomelfe at gmail.com Wed Apr 30 15:29:25 2014 From: gilbertomelfe at gmail.com (Gilberto Melfe) Date: Wed, 30 Apr 2014 16:29:25 +0100 Subject: [Haskell-beginners] A little explanation! Message-ID: Hello everybody ! Could someone explain me exactly how this function works? elementAt_w'pf = (last .) . take . (+ 1) It's a posted solution to: 99 Haskell problems, problem 3. I'm having trouble with the "(last .) ." thing! Hope someone can help! Thank You! Gilberto -------------- next part -------------- An HTML attachment was scrubbed... URL: From orclev at gmail.com Wed Apr 30 15:53:29 2014 From: orclev at gmail.com (Kyle Murphy) Date: Wed, 30 Apr 2014 11:53:29 -0400 Subject: [Haskell-beginners] A little explanation! In-Reply-To: References: Message-ID: Near as I can tell, this is basically having to do with partial application and the order of precedence for the (.) operator. A great way to look at this stuff is using the :t command in GHCi and checking out the types involved. Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c Prelude> :t last last :: [a] -> a Prelude> :t (last .) (last .) :: (a -> [c]) -> a -> c Looking back at the type for (.) and plugging in "[a]" in place of "b" and "a" in place of "c" we get: ([b] -> b) -> (a -> [b]) -> a -> b and since "last" takes the place of the first function we can reduce that to: (a -> [b]) -> a -> b GHCi used "c" where we used "b" but you can clearly see the signatures are identical other than that detail. What we're left with is, (last .) is used to take a function from some type "a" that returns a list of type "b", and a "a" value, and then returns the last value from that list of "b" types. -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe wrote: > Hello everybody ! > > Could someone explain me exactly how this function works? > > elementAt_w'pf = (last .) . take . (+ 1) > > It's a posted solution to: 99 Haskell problems, problem 3. > > I'm having trouble with the "(last .) ." thing! > > Hope someone can help! > > Thank You! > > Gilberto > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ari.brandeis.king at gmail.com Wed Apr 30 16:18:08 2014 From: ari.brandeis.king at gmail.com (Ari King) Date: Wed, 30 Apr 2014 12:18:08 -0400 Subject: [Haskell-beginners] How to Update Cabal? Message-ID: Hi, I installed the haskell platform for ubuntu, which contains cabal-install version 1.16.0.2 I'd like to use sandboxes, which as I understand is available in versions >= 1.18. Is it best to clone the cabal git repo and install as described here? In which case, should I remove the existing cabal installation from "/usr/bin"? Best, Ari -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.trstenjak at gmail.com Wed Apr 30 16:22:18 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Wed, 30 Apr 2014 18:22:18 +0200 Subject: [Haskell-beginners] How to Update Cabal? In-Reply-To: References: Message-ID: <20140430162218.GA28575@machine> Hi Ari, On Wed, Apr 30, 2014 at 12:18:08PM -0400, Ari King wrote: > Is it best to clone the cabal git repo and install as described here? In which > case, should I remove the existing cabal installation from "/usr/bin"? It's just: cabal install cabal-install Greetings, Daniel From michael at orlitzky.com Wed Apr 30 17:19:02 2014 From: michael at orlitzky.com (Michael Orlitzky) Date: Wed, 30 Apr 2014 13:19:02 -0400 Subject: [Haskell-beginners] A little explanation! In-Reply-To: References: Message-ID: <53613086.1050809@orlitzky.com> On 04/30/2014 11:29 AM, Gilberto Melfe wrote: > Hello everybody ! > > Could someone explain me exactly how this function works? > > elementAt_w'pf = (last .) . take . (+ 1) > > It's a posted solution to: 99 Haskell problems, problem 3. > > I'm having trouble with the "(last .) ." thing! > It's not your fault, that solution is retarded. Once you remove all of the intentional obfuscation, it looks like, elementAt_w'pf n xs = last (take (n + 1) xs) which is easy to understand. You can un-retard it step-by-step: elementAt_w'pf = (last .) . take . (+ 1) <=> elementAt_w'pf n = ((last .) . take . (+ 1)) n = (last .) ((take . (+ 1)) n) = (last .) (take (n + 1)) = last . (take (n + 1)) <=> elementAt_w'pf n xs = (last . (take (n + 1))) xs = last (take (n + 1) xs) All I've used above is the precedence of the function composition operator, and the fact that (f . g) x = f (g x). From defigueiredo at ucdavis.edu Wed Apr 30 17:25:58 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 30 Apr 2014 11:25:58 -0600 Subject: [Haskell-beginners] A little explanation! follow up In-Reply-To: References: Message-ID: <53613226.903@ucdavis.edu> I have a follow up question on this one. Is code like this: elementAt_w'pf = (last .) . take . (+ 1) considered good haskell? If so, will this ever become easy to read? What do I do to practice reading this? More specifically, how would I know in mind head, before asking the type checker in GHCi to write this pipeline with the section (last .) instead of simply making the (wrong) pipe: elementAt_w'pf = last . take . (+ 1) I am used to using pipes in the unix shell, but in the shell there is always only one input and one output. There is no currying going on and so it is very clear what we are dealing with. How do I learn this? Thanks, Dimitri Em 30/04/14 09:53, Kyle Murphy escreveu: > Near as I can tell, this is basically having to do with partial > application and the order of precedence for the (.) operator. A great > way to look at this stuff is using the :t command in GHCi and checking > out the types involved. > > Prelude> :t (.) > (.) :: (b -> c) -> (a -> b) -> a -> c > > Prelude> :t last > last :: [a] -> a > > Prelude> :t (last .) > (last .) :: (a -> [c]) -> a -> c > > Looking back at the type for (.) and plugging in "[a]" in place of "b" > and "a" in place of "c" we get: > ([b] -> b) -> (a -> [b]) -> a -> b > > and since "last" takes the place of the first function we can reduce > that to: > (a -> [b]) -> a -> b > > GHCi used "c" where we used "b" but you can clearly see the signatures > are identical other than that detail. > > What we're left with is, (last .) is used to take a function from some > type "a" that returns a list of type "b", and a "a" value, and then > returns the last value from that list of "b" types. > > > -R. Kyle Murphy > -- > Curiosity was framed, Ignorance killed the cat. > > > On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe > > wrote: > > Hello everybody ! > > Could someone explain me exactly how this function works? > > elementAt_w'pf = (last .) . take . (+ 1) > > It's a posted solution to: 99 Haskell problems, problem 3. > > I'm having trouble with the "(last .) ." thing! > > Hope someone can help! > > Thank You! > > Gilberto > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From timmelzer at gmail.com Wed Apr 30 17:40:10 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Wed, 30 Apr 2014 19:40:10 +0200 Subject: [Haskell-beginners] How to Update Cabal? In-Reply-To: <20140430162218.GA28575@machine> References: <20140430162218.GA28575@machine> Message-ID: Probably one needs to ensure that ~/.cabal/bin is in the PATH-variable before the path that cabal normally lives in. Alternatively you have to use sudo and the --global switch for cabal (not recommended). Am 30.04.2014 18:22 schrieb "Daniel Trstenjak" : > > Hi Ari, > > On Wed, Apr 30, 2014 at 12:18:08PM -0400, Ari King wrote: > > Is it best to clone the cabal git repo and install as described here? In > which > > case, should I remove the existing cabal installation from "/usr/bin"? > > It's just: > > cabal install cabal-install > > > Greetings, > Daniel > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Wed Apr 30 18:15:57 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 30 Apr 2014 14:15:57 -0400 Subject: [Haskell-beginners] A little explanation! follow up In-Reply-To: <53613226.903@ucdavis.edu> References: <53613226.903@ucdavis.edu> Message-ID: <20140430181557.GA16986@seas.upenn.edu> On Wed, Apr 30, 2014 at 11:25:58AM -0600, Dimitri DeFigueiredo wrote: > I have a follow up question on this one. Is code like this: > > elementAt_w'pf = (last .) . take . (+ 1) > > considered good haskell? Uses of composition sections like (last .) is not very common; I would not consider it good haskell (though tastes may differ). On the other hand, point-free notation, used in moderation, is considered good Haskell style. For example, instead of foo x = bar (baz (quux x)) you should write foo = bar . baz . quux In this case I would certainly consider the second definition better style than the first. > If so, will this ever become easy to read? > What do I do to practice reading this? Just read and write lots of code. Try transforming things into and out of point-free notation as an exercise. > More specifically, how would I know in mind head, before asking the > type checker in GHCi to write this pipeline with the section (last .) > instead of simply making the (wrong) pipe: > > elementAt_w'pf = last . take . (+ 1) Simply by working out the types in your head. This becomes much easier with practice. > I am used to using pipes in the unix shell, but in the shell there is > always only one input and one output. There is no currying going on > and so it is very clear what we are dealing with. How do I learn > this? There is always only one input and one output in Haskell as well. The thing that makes it more complicated is that unlike in the shell, those inputs and outputs can themselves be functions. -Brent > > > Em 30/04/14 09:53, Kyle Murphy escreveu: > >Near as I can tell, this is basically having to do with partial > >application and the order of precedence for the (.) operator. A > >great way to look at this stuff is using the :t command in GHCi and > >checking out the types involved. > > > >Prelude> :t (.) > >(.) :: (b -> c) -> (a -> b) -> a -> c > > > >Prelude> :t last > >last :: [a] -> a > > > >Prelude> :t (last .) > >(last .) :: (a -> [c]) -> a -> c > > > >Looking back at the type for (.) and plugging in "[a]" in place of > >"b" and "a" in place of "c" we get: > >([b] -> b) -> (a -> [b]) -> a -> b > > > >and since "last" takes the place of the first function we can > >reduce that to: > >(a -> [b]) -> a -> b > > > >GHCi used "c" where we used "b" but you can clearly see the > >signatures are identical other than that detail. > > > >What we're left with is, (last .) is used to take a function from > >some type "a" that returns a list of type "b", and a "a" value, and > >then returns the last value from that list of "b" types. > > > > > >-R. Kyle Murphy > >-- > >Curiosity was framed, Ignorance killed the cat. > > > > > >On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe > >> wrote: > > > > Hello everybody ! > > > > Could someone explain me exactly how this function works? > > > > elementAt_w'pf = (last .) . take . (+ 1) > > > > It's a posted solution to: 99 Haskell problems, problem 3. > > > > I'm having trouble with the "(last .) ." thing! > > > > Hope someone can help! > > > > Thank You! > > > > Gilberto > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > > > > > > >_______________________________________________ > >Beginners mailing list > >Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From orclev at gmail.com Wed Apr 30 18:16:45 2014 From: orclev at gmail.com (Kyle Murphy) Date: Wed, 30 Apr 2014 14:16:45 -0400 Subject: [Haskell-beginners] A little explanation! In-Reply-To: <53613086.1050809@orlitzky.com> References: <53613086.1050809@orlitzky.com> Message-ID: On the point of it being "retarded" there's some merit to that in that it's written in point-free (or as some joking call it, pointless) style. The idea is to remove the explicit variables and so the functions are re-arranged and composed such that all the variables come at the end of the statement and you're left with nothing but a chain of functions at the start (which can sometimes be easier to understand since it's just function composition). The downside, as you've seen is that it often hurts readability and so there are some that are strongly opposed to point-free style. If used carefully, and things are broken into smaller easier to understand chunks (via for instance let or where clauses) it can still be readable even using point-free style, but it's really dependent on both the skill of the person writing the statement, and the familiarity and skill of the person reading the statement. When in doubt about legibility it's better to avoid point-free style, in particular if you start having to do strange things like apply the (.) operator to itself (E.G. "(f .) . g" or similar) you're probably being to clever for your own good. I wouldn't go so far as to say you should *never* use point-free style, but you should seriously consider if you're trying to just be clever and show off, or if it really is making the statement simpler to understand via more clearly showing how the functions are composed. -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Wed, Apr 30, 2014 at 1:19 PM, Michael Orlitzky wrote: > On 04/30/2014 11:29 AM, Gilberto Melfe wrote: > > Hello everybody ! > > > > Could someone explain me exactly how this function works? > > > > elementAt_w'pf = (last .) . take . (+ 1) > > > > It's a posted solution to: 99 Haskell problems, problem 3. > > > > I'm having trouble with the "(last .) ." thing! > > > > It's not your fault, that solution is retarded. Once you remove all of > the intentional obfuscation, it looks like, > > elementAt_w'pf n xs = last (take (n + 1) xs) > > which is easy to understand. You can un-retard it step-by-step: > > elementAt_w'pf = (last .) . take . (+ 1) > > <=> elementAt_w'pf n = ((last .) . take . (+ 1)) n > > = (last .) ((take . (+ 1)) n) > > = (last .) (take (n + 1)) > > = last . (take (n + 1)) > > <=> elementAt_w'pf n xs = (last . (take (n + 1))) xs > > = last (take (n + 1) xs) > > All I've used above is the precedence of the function composition > operator, and the fact that (f . g) x = f (g x). > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From byorgey at seas.upenn.edu Wed Apr 30 18:19:26 2014 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed, 30 Apr 2014 14:19:26 -0400 Subject: [Haskell-beginners] How to Update Cabal? In-Reply-To: References: <20140430162218.GA28575@machine> Message-ID: <20140430181926.GB16986@seas.upenn.edu> On Wed, Apr 30, 2014 at 07:40:10PM +0200, Norbert Melzer wrote: > Probably one needs to ensure that ~/.cabal/bin is in the PATH-variable > before the path that cabal normally lives in. Alternatively you have to use > sudo and the --global switch for cabal (not recommended). *If* you ever have to install something globally with cabal, note that you should use cabal install --global --root-cmd=sudo and NOT 'sudo cabal install --global'. This is so that cabal will only take root privileges for the actual copying/installation, and not pollute your package cache, etc. with files owned by root. *But* as Norbert says (and I strongly agree), it is not recommended to use --global with cabal, unless you know what you are doing and why. -Brent > Am 30.04.2014 18:22 schrieb "Daniel Trstenjak" : > > > > > Hi Ari, > > > > On Wed, Apr 30, 2014 at 12:18:08PM -0400, Ari King wrote: > > > Is it best to clone the cabal git repo and install as described here? In > > which > > > case, should I remove the existing cabal installation from "/usr/bin"? > > > > It's just: > > > > cabal install cabal-install > > > > > > Greetings, > > Daniel > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From davidleothomas at gmail.com Wed Apr 30 18:32:18 2014 From: davidleothomas at gmail.com (David Thomas) Date: Wed, 30 Apr 2014 11:32:18 -0700 Subject: [Haskell-beginners] A little explanation! In-Reply-To: References: <53613086.1050809@orlitzky.com> Message-ID: I think this one is most clear removing only one point: elementAt_w'pf n = last . take (n + 1) No line noise, simple composition of two functions. Extremes of point free style are frequently harmful, but understanding it is worthwhile and sensibly limited application can improve readability. I'm not sure I'd call the example here "extreme" but it's certainly approaching it (in terms of what I find easily readable). On Wed, Apr 30, 2014 at 11:16 AM, Kyle Murphy wrote: > On the point of it being "retarded" there's some merit to that in that it's > written in point-free (or as some joking call it, pointless) style. The idea > is to remove the explicit variables and so the functions are re-arranged and > composed such that all the variables come at the end of the statement and > you're left with nothing but a chain of functions at the start (which can > sometimes be easier to understand since it's just function composition). The > downside, as you've seen is that it often hurts readability and so there are > some that are strongly opposed to point-free style. If used carefully, and > things are broken into smaller easier to understand chunks (via for instance > let or where clauses) it can still be readable even using point-free style, > but it's really dependent on both the skill of the person writing the > statement, and the familiarity and skill of the person reading the > statement. When in doubt about legibility it's better to avoid point-free > style, in particular if you start having to do strange things like apply the > (.) operator to itself (E.G. "(f .) . g" or similar) you're probably being > to clever for your own good. I wouldn't go so far as to say you should > *never* use point-free style, but you should seriously consider if you're > trying to just be clever and show off, or if it really is making the > statement simpler to understand via more clearly showing how the functions > are composed. > > > -R. Kyle Murphy > -- > Curiosity was framed, Ignorance killed the cat. > > > On Wed, Apr 30, 2014 at 1:19 PM, Michael Orlitzky > wrote: >> >> On 04/30/2014 11:29 AM, Gilberto Melfe wrote: >> > Hello everybody ! >> > >> > Could someone explain me exactly how this function works? >> > >> > elementAt_w'pf = (last .) . take . (+ 1) >> > >> > It's a posted solution to: 99 Haskell problems, problem 3. >> > >> > I'm having trouble with the "(last .) ." thing! >> > >> >> It's not your fault, that solution is retarded. Once you remove all of >> the intentional obfuscation, it looks like, >> >> elementAt_w'pf n xs = last (take (n + 1) xs) >> >> which is easy to understand. You can un-retard it step-by-step: >> >> elementAt_w'pf = (last .) . take . (+ 1) >> >> <=> elementAt_w'pf n = ((last .) . take . (+ 1)) n >> >> = (last .) ((take . (+ 1)) n) >> >> = (last .) (take (n + 1)) >> >> = last . (take (n + 1)) >> >> <=> elementAt_w'pf n xs = (last . (take (n + 1))) xs >> >> = last (take (n + 1) xs) >> >> All I've used above is the precedence of the function composition >> operator, and the fact that (f . g) x = f (g x). >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From defigueiredo at ucdavis.edu Wed Apr 30 18:44:37 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Wed, 30 Apr 2014 12:44:37 -0600 Subject: [Haskell-beginners] A little explanation! follow up In-Reply-To: <20140430181557.GA16986@seas.upenn.edu> References: <53613226.903@ucdavis.edu> <20140430181557.GA16986@seas.upenn.edu> Message-ID: <53614495.6040300@ucdavis.edu> Thanks everyone. It seems I asked the follow-up question too soon. Michael's or David's proposed "reformulations" elementAt_w'pf n xs = last (take (n + 1) xs) or elementAt_w'pf n = last . take (n + 1) are so much easier to read than the original elementAt_w'pf = (last .) . take . (+ 1) that I will defer learning how to read the original version for now. It seems learning to quickly decipher that beast in one's head is not a prerequisite to being an effective haskell programmer. So, I'll just parse it at my slow pace for now. I think I can see how pointfree style (which I really like) can be overused sometimes. Thanks again. This list rocks! :-) Dimitri Em 30/04/14 12:15, Brent Yorgey escreveu: > On Wed, Apr 30, 2014 at 11:25:58AM -0600, Dimitri DeFigueiredo wrote: >> I have a follow up question on this one. Is code like this: >> >> elementAt_w'pf = (last .) . take . (+ 1) >> >> considered good haskell? > Uses of composition sections like (last .) is not very common; I would > not consider it good haskell (though tastes may differ). On the other > hand, point-free notation, used in moderation, is considered good > Haskell style. For example, instead of > > foo x = bar (baz (quux x)) > > you should write > > foo = bar . baz . quux > > In this case I would certainly consider the second definition better > style than the first. > >> If so, will this ever become easy to read? >> What do I do to practice reading this? > Just read and write lots of code. Try transforming things into and > out of point-free notation as an exercise. > >> More specifically, how would I know in mind head, before asking the >> type checker in GHCi to write this pipeline with the section (last .) >> instead of simply making the (wrong) pipe: >> >> elementAt_w'pf = last . take . (+ 1) > Simply by working out the types in your head. This becomes much > easier with practice. > >> I am used to using pipes in the unix shell, but in the shell there is >> always only one input and one output. There is no currying going on >> and so it is very clear what we are dealing with. How do I learn >> this? > There is always only one input and one output in Haskell as well. The > thing that makes it more complicated is that unlike in the shell, > those inputs and outputs can themselves be functions. > > -Brent > >> >> Em 30/04/14 09:53, Kyle Murphy escreveu: >>> Near as I can tell, this is basically having to do with partial >>> application and the order of precedence for the (.) operator. A >>> great way to look at this stuff is using the :t command in GHCi and >>> checking out the types involved. >>> >>> Prelude> :t (.) >>> (.) :: (b -> c) -> (a -> b) -> a -> c >>> >>> Prelude> :t last >>> last :: [a] -> a >>> >>> Prelude> :t (last .) >>> (last .) :: (a -> [c]) -> a -> c >>> >>> Looking back at the type for (.) and plugging in "[a]" in place of >>> "b" and "a" in place of "c" we get: >>> ([b] -> b) -> (a -> [b]) -> a -> b >>> >>> and since "last" takes the place of the first function we can >>> reduce that to: >>> (a -> [b]) -> a -> b >>> >>> GHCi used "c" where we used "b" but you can clearly see the >>> signatures are identical other than that detail. >>> >>> What we're left with is, (last .) is used to take a function from >>> some type "a" that returns a list of type "b", and a "a" value, and >>> then returns the last value from that list of "b" types. >>> >>> >>> -R. Kyle Murphy >>> -- >>> Curiosity was framed, Ignorance killed the cat. >>> >>> >>> On Wed, Apr 30, 2014 at 11:29 AM, Gilberto Melfe >>> > wrote: >>> >>> Hello everybody ! >>> >>> Could someone explain me exactly how this function works? >>> >>> elementAt_w'pf = (last .) . take . (+ 1) >>> >>> It's a posted solution to: 99 Haskell problems, problem 3. >>> >>> I'm having trouble with the "(last .) ." thing! >>> >>> Hope someone can help! >>> >>> Thank You! >>> >>> Gilberto >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From Robert.Weisser at gmx.com Wed Apr 30 22:18:05 2014 From: Robert.Weisser at gmx.com (Robert Weisser) Date: Thu, 01 May 2014 00:18:05 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse Message-ID: <20140430221806.137400@gmx.com> I decided to try out Eclipse with the EclipseFP plug-in for Haskell. I downloaded Eclipse, and then started to install EclipseFP. At some point, I got a screen which said that I needed to install buildwrapper and scion-browser. It offered to install both of them, and optionally install hoogle, hlint, stylish-haskell, SourceGraph, and cabal-dev. The option to install the additional items was pre-checked. I left it checked. The installation of buildwrapper, etc. took a long time. It seemed to be going well until I saw the messages below, which say that installing SourceGraph will likely break several other packages, most of which were part of the EclipseFP installation. I am not a complete novice with Haskell but I am a complete novice with Cabal. I'm not sure what to do. Here are some options I have considered: 1. Use --force-reinstalls and hope for the best. 2. Try to resolve the problem with Cabal, although I don't know to do this. 3. Use EclipseFP without SourceGraph and cabal-dev. 4. Use EclipseFP without SourceGraph, but install cabal-dev using cabal on the command line. 5. Forget about Eclipse altogether, and go back to using vim. Any advice would be appreciated. By the way, I am using Haskell Platform 7.6.3. Here are the messages I saw in the Eclipse console window: Installing executable SourceGraph Resolving dependencies... In order, the following would be installed: asn1-types-0.2.3 (new package) asn1-encoding-0.8.1.3 (new package) asn1-parse-0.8.1 (new package) bktrees-0.3.1 (new package) byteable-0.1.1 (new package) cereal-0.4.0.1 (new package) colour-2.3.3 (new package) cpphs-1.18.2 (new version) crypto-pubkey-types-0.4.2.2 (new package) cryptohash-0.11.4 (new package) digest-0.0.1.2 (new package) dlist-0.5 (new version) aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> 0.5 data-default-0.5.3 (reinstall) cookie-0.4.1.1 (new package) haskell-src-exts-1.13.5 (new version) hslua-0.3.12 (new package) mime-types-0.1.0.4 (new package) multiset-0.2.2 (new package) pandoc-types-1.12.3.2 (new package) pem-0.2.2 (new package) polyparse-1.8 (new version) publicsuffixlist-0.1 (new package) http-client-0.3.2 (new package) regex-pcre-builtin-0.94.4.8.8.34 (new package) highlighting-kate-0.5.6.1 (new package) resourcet-0.4.10.2 (new version) securemem-0.1.3 (new package) crypto-cipher-types-0.0.9 (new package) cipher-aes-0.2.7 (new package) cipher-rc4-0.1.4 (new package) crypto-random-0.0.7 (new package) cprng-aes-0.5.2 (new package) crypto-numbers-0.2.3 (new package) crypto-pubkey-0.2.4 (new package) socks-0.5.4 (new package) temporary-1.1.2.5 (new package) text-stream-decode-0.1.0.5 (new package) conduit-1.0.17.1 (new version) http-client-conduit-0.2.0.1 (new package) wl-pprint-text-1.1.0.2 (new package) graphviz-2999.16.0.0 (new package) x509-1.4.11 (new package) x509-store-1.4.4 (new package) x509-system-1.4.5 (new package) x509-validation-1.5.0 (new package) tls-1.2.6 (new package) connection-0.2.1 (new package) http-client-tls-0.2.1.1 (new package) http-conduit-2.0.0.10 (new package) xml-1.3.13 (new package) texmath-0.6.6.1 (new package) yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, resourcet-1.1.2 -> 0.4.10.2 zip-archive-0.2.2.1 (new package) pandoc-1.12.3.3 (new package) Graphalyze-0.14.0.2 (new package) SourceGraph-0.7.0.5 (new package) cabal: The following packages are likely to be broken by the reinstalls: stylish-haskell-0.5.10.0 scion-browser-0.3.1 persistent-template-1.3.1.3 persistent-sqlite-1.3.0.5 persistent-1.3.0.6 buildwrapper-0.8.0 dynamic-cabal-0.3.1 Use --force-reinstalls if you want to install anyway. -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Wed Apr 30 23:19:36 2014 From: timmelzer at gmail.com (Norbert Melzer) Date: Thu, 1 May 2014 01:19:36 +0200 Subject: [Haskell-beginners] Problem installing EclipseFp in Eclipse In-Reply-To: <20140430221806.137400@gmx.com> References: <20140430221806.137400@gmx.com> Message-ID: Omit cabal-dev it is not maintained anymore and most of its functionality is part of cabal already (cabal sandbox) Am 01.05.2014 00:18 schrieb "Robert Weisser" : > I decided to try out Eclipse with the EclipseFP plug-in for Haskell. > I downloaded Eclipse, and then started to install EclipseFP. At > some point, I got a screen which said that I needed to install > buildwrapper and scion-browser. It offered to install both > of them, and optionally install hoogle, hlint, stylish-haskell, > SourceGraph, and cabal-dev. The option to install the additional > items was pre-checked. I left it checked. The installation of > buildwrapper, etc. took a long time. It seemed to be going well > until I saw the messages below, which say that installing SourceGraph > will likely break several other packages, most of which were part > of the EclipseFP installation. > > I am not a complete novice with Haskell but I am a complete novice > with Cabal. I'm not sure what to do. Here are some options I have > considered: > > 1. Use --force-reinstalls and hope for the best. > > 2. Try to resolve the problem with Cabal, although I don't know to > do this. > > 3. Use EclipseFP without SourceGraph and cabal-dev. > > 4. Use EclipseFP without SourceGraph, but install cabal-dev using > cabal on the command line. > > 5. Forget about Eclipse altogether, and go back to using vim. > > Any advice would be appreciated. > > By the way, I am using Haskell Platform 7.6.3. > > Here are the messages I saw in the Eclipse console window: > > Installing executable SourceGraph > > Resolving dependencies... > In order, the following would be installed: > asn1-types-0.2.3 (new package) > asn1-encoding-0.8.1.3 (new package) > asn1-parse-0.8.1 (new package) > bktrees-0.3.1 (new package) > byteable-0.1.1 (new package) > cereal-0.4.0.1 (new package) > colour-2.3.3 (new package) > cpphs-1.18.2 (new version) > crypto-pubkey-types-0.4.2.2 (new package) > cryptohash-0.11.4 (new package) > digest-0.0.1.2 (new package) > dlist-0.5 (new version) > aeson-0.7.0.3 (reinstall) changes: dlist-0.7.0.1 -> 0.5 > data-default-instances-dlist-0.0.1 (reinstall) changes: dlist-0.7.0.1 -> > 0.5 > data-default-0.5.3 (reinstall) > cookie-0.4.1.1 (new package) > haskell-src-exts-1.13.5 (new version) > hslua-0.3.12 (new package) > mime-types-0.1.0.4 (new package) > multiset-0.2.2 (new package) > pandoc-types-1.12.3.2 (new package) > pem-0.2.2 (new package) > polyparse-1.8 (new version) > publicsuffixlist-0.1 (new package) > http-client-0.3.2 (new package) > regex-pcre-builtin-0.94.4.8.8.34 (new package) > highlighting-kate-0.5.6.1 (new package) > resourcet-0.4.10.2 (new version) > securemem-0.1.3 (new package) > crypto-cipher-types-0.0.9 (new package) > cipher-aes-0.2.7 (new package) > cipher-rc4-0.1.4 (new package) > crypto-random-0.0.7 (new package) > cprng-aes-0.5.2 (new package) > crypto-numbers-0.2.3 (new package) > crypto-pubkey-0.2.4 (new package) > socks-0.5.4 (new package) > temporary-1.1.2.5 (new package) > text-stream-decode-0.1.0.5 (new package) > conduit-1.0.17.1 (new version) > http-client-conduit-0.2.0.1 (new package) > wl-pprint-text-1.1.0.2 (new package) > graphviz-2999.16.0.0 (new package) > x509-1.4.11 (new package) > x509-store-1.4.4 (new package) > x509-system-1.4.5 (new package) > x509-validation-1.5.0 (new package) > tls-1.2.6 (new package) > connection-0.2.1 (new package) > http-client-tls-0.2.1.1 (new package) > http-conduit-2.0.0.10 (new package) > xml-1.3.13 (new package) > texmath-0.6.6.1 (new package) > yaml-0.8.8.2 (reinstall) changes: conduit-1.1.1 -> 1.0.17.1, > resourcet-1.1.2 > -> 0.4.10.2 > zip-archive-0.2.2.1 (new package) > pandoc-1.12.3.3 (new package) > Graphalyze-0.14.0.2 (new package) > SourceGraph-0.7.0.5 (new package) > cabal: The following packages are likely to be broken by the reinstalls: > stylish-haskell-0.5.10.0 > scion-browser-0.3.1 > persistent-template-1.3.1.3 > persistent-sqlite-1.3.0.5 > persistent-1.3.0.6 > buildwrapper-0.8.0 > dynamic-cabal-0.3.1 > Use --force-reinstalls if you want to install anyway. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: