haskell

Make ReadArgs 1.0 work with a single argument

佐手、 提交于 2020-01-03 14:00:39
问题 Playing around with the ReadArgs package, it seems that it does not support single-argument situations. {-# LANGUAGE ScopedTypeVariables #-} import ReadArgs (readArgs) main = do (foo :: Int) <- readArgs print foo The error is (when using version 1.0): No instance for (ReadArgs.ArgumentTuple Int) arising from a use of `readArgs' My question is twofold: How does readArgs work? How can that library be adjusted to allow it to work with a single argument as well? N.B. version 1.1 of ReadArgs

How to print comma-separated list with hamlet?

Deadly 提交于 2020-01-03 12:37:15
问题 With the hamlet templating language that comes with yesod, what is the best way of printing a comma-separated list? E.g. assume this code which just prints one entry after another, how do I insert commas in between the elements? Or maybe even add an “and” before the last entry: The values in the list are $ forall entry <- list #{entry} and that is it. Some templating languages such as Template Toolkit provide directives to detect the first or last iteration. 回答1: I don't think there's

How to print comma-separated list with hamlet?

血红的双手。 提交于 2020-01-03 12:35:31
问题 With the hamlet templating language that comes with yesod, what is the best way of printing a comma-separated list? E.g. assume this code which just prints one entry after another, how do I insert commas in between the elements? Or maybe even add an “and” before the last entry: The values in the list are $ forall entry <- list #{entry} and that is it. Some templating languages such as Template Toolkit provide directives to detect the first or last iteration. 回答1: I don't think there's

How does Haskell convert integer literals to different types?

馋奶兔 提交于 2020-01-03 12:31:05
问题 I have following anonymous function: *Exercises> g = \(Sum n) -> Sum (n - 1) I use it like: *Exercises> g (Sum 56) Sum {getSum = 55} *Exercises> g 56 Sum {getSum = 55} The second example, how does the compiler convert 56 to Sum 56 ? In the prelude, I saw that Sum is an instance of Num , but it not clear about the conversion. 回答1: When Haskell sees an integer literal such as 56 , it interprets it as fromInteger 56 . The type of fromInteger is Num a => Integer -> a , so the type of this code is

How does Haskell convert integer literals to different types?

◇◆丶佛笑我妖孽 提交于 2020-01-03 12:29:00
问题 I have following anonymous function: *Exercises> g = \(Sum n) -> Sum (n - 1) I use it like: *Exercises> g (Sum 56) Sum {getSum = 55} *Exercises> g 56 Sum {getSum = 55} The second example, how does the compiler convert 56 to Sum 56 ? In the prelude, I saw that Sum is an instance of Num , but it not clear about the conversion. 回答1: When Haskell sees an integer literal such as 56 , it interprets it as fromInteger 56 . The type of fromInteger is Num a => Integer -> a , so the type of this code is

Example of Invariant Functor?

拟墨画扇 提交于 2020-01-03 12:28:19
问题 I'm reading documentation on monad layers package and my brain is going to boil up. In the mmtl section of this document the author talks about invariant functor . It's method invmap is like fmap of Functor but it takes an inverse morphism (b -> a) also. I understand why author says that hoist of MFunctor is more powerful than tmap of Invariant but i don't see what's the point of that inverse morphism. Is there any example of an Invariant which can't be an instance of Functor ? 回答1: Here's a

Cabal 2.0 required when using a nightly snapshot with stack

拜拜、爱过 提交于 2020-01-03 12:28:10
问题 I'm trying to setup a new project using the nightly-2017-08-17 snapshot stack new test --resolver nightly-2017-08-17 However this gives the following error: Downloading template "new-template" to create project "test" in test/ ... Looking for .cabal or package.yaml files to use to init the project. Using cabal packages: - test/test.cabal Selected resolver: nightly-2017-08-17 Unable to parse cabal file: FromString "This package requires at least Cabal version 2.0" Nothing Cabal is in its

Getting the head and tail of a custom list type in Haskell

百般思念 提交于 2020-01-03 11:46:53
问题 I have a custom list type: data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq) data CList a = Nil | NotNil ( NNList a) deriving (Eq) I'm trying to implement a function that returns the head and tail of a list: cListGet :: CList a -> Maybe (a, CList a) My attempt: cListGet :: CList a -> Maybe (a, CList a) cListGet Nil = Nothing cListGet xs@(NotNil nxs) = case nxs of Sing x -> (x, Nil) Append l r -> ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r)) Which

Getting the head and tail of a custom list type in Haskell

牧云@^-^@ 提交于 2020-01-03 11:46:44
问题 I have a custom list type: data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq) data CList a = Nil | NotNil ( NNList a) deriving (Eq) I'm trying to implement a function that returns the head and tail of a list: cListGet :: CList a -> Maybe (a, CList a) My attempt: cListGet :: CList a -> Maybe (a, CList a) cListGet Nil = Nothing cListGet xs@(NotNil nxs) = case nxs of Sing x -> (x, Nil) Append l r -> ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r)) Which

ErrorT is deprecated, but ExceptT doesn't fit

爱⌒轻易说出口 提交于 2020-01-03 11:33:15
问题 I had a monadic computation. At some point it started requiring a MonadFail constraint because of a monadic pattern-match. My easy fix was to run it with this: fmap (either error id) . runErrorT Yet ouch: Deprecated: "Use Control.Monad.Trans.Except instead" So it appears that ErrorT is deprecated, and I'm to use ExceptT instead. That sounds fine from the outside, but it doesn't appear like ExceptT is a drop-in replacement at all! Just look at the instance declarations: instance (Monad m,