haskell

Do ghc-compiled binaries require GHC or are they self-contained?

雨燕双飞 提交于 2019-12-28 05:16:08
问题 If a friend wants to run my Haskell binaries, does he have to first install Haskell, or can he immediately run the binary by itself? Is the answer the same on Mac, Windows, and Linux? 回答1: GHC does produce stand-alone binaries that do not require GHC itself to be installed, however they do link against some dynamic libraries, most notably libgmp . The remaining libraries are commonly found out of the box on most Linux systems. I believe the situation is similar on Windows. You can check which

Haskell GHC: what is the time complexity of a pattern match with N constructors?

我的梦境 提交于 2019-12-28 03:46:28
问题 Let's say we have the following Haskell: data T = T0 | T1 | T2 | ... | TN toInt :: T -> Int toInt t = case t of T0 -> 0 T1 -> 1 T2 -> 2 ... TN -> N What algorithm is used to perform the pattern match here? I see two options: (1) Linear search, something like if (t.tag == T0) { ... } else if (t.tag == T1) { ... } else ... (2) Binary search, which would be sensible in this specific task: searching for t.tag in the set { TO ... T1023 }. However, where pattern matching in general has many other

No instance for (Fractional Int) arising from a use of `/'

拥有回忆 提交于 2019-12-28 03:44:06
问题 I am new to Haskell, and I am struggling with debugging my code. Fixing an error leads to other errors... Here is my code. import Data.Maybe data Op = Add | Sub | Mul | Div | And | Or | Not | Eq | Less | Great deriving (Eq, Show) data Exp = Literal Value | Primitive Op [Exp] | Variable String | If Exp Exp Exp | Let [(String, Exp)] Exp deriving (Show, Eq) data Value = Number Int | Bool Bool | String String deriving (Eq, Show) type Env = [(String, Value)] eval :: Env -> Exp -> Value eval e

Is there a monad that doesn't have a corresponding monad transformer (except IO)?

喜你入骨 提交于 2019-12-28 03:31:09
问题 So far, every monad (that can be represented as a data type) that I have encountered had a corresponding monad transformer, or could have one. Is there such a monad that can't have one? Or do all monads have a corresponding transformer? By a transformer t corresponding to monad m I mean that t Identity is isomorphic to m . And of course that it satisfies the monad transformer laws and that t n is a monad for any monad n . I'd like to see either a proof (ideally a constructive one) that every

Comparing Haskell's Snap and Yesod web frameworks

孤街醉人 提交于 2019-12-28 03:15:09
问题 The two Haskell web frameworks in the news recently are Yesod (at 0.8) and Snap (at 0.4). It's quite obvious that Yesod currently supports a lot more features than Snap. However, I can't stand the syntax Yesod uses for its HTML, CSS and Javascript. So, I'd like to understand what I'd be missing if I went with Snap instead. For example, doesn't look like database support is there. How about sessions? Other features? 回答1: Full disclosure: I'm one of the lead developers of Snap. First of all,

How to make a type with restrictions

戏子无情 提交于 2019-12-28 02:51:31
问题 For example I want to make a type MyType of integer triples. But not just Cartesian product of three Integer, I want the type to represent all (x, y, z) such that x + y + z = 5 How do I do that? Except of using just (x, y) since z = 5 - x - y And the same question if I have three constructors A, B, C and the type should be all (A x, B y, C z) such that x + y + z = 5 回答1: I think the trick here is that you don't enforce it on the type-level, you use "smart constructors": i.e. only allow

Algorithm - How to delete duplicate elements in a list efficiently?

核能气质少年 提交于 2019-12-28 02:39:13
问题 There is a list L . It contains elements of arbitrary type each . How to delete all duplicate elements in such list efficiently? ORDER must be preserved Just an algorithm is required, so no import any external library is allowed. Related questions In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order? How do you remove duplicates from a list in Python whilst preserving order? Removing duplicates from list of lists

Foldr/Foldl for free when Tree is implementing Foldable foldMap?

家住魔仙堡 提交于 2019-12-28 02:12:10
问题 I am a beginner at Haskell and learning from "Learn You a Haskell". There's something I don't understand about the Tree implementation of Foldable . instance F.Foldable Tree where foldMap f Empty = mempty foldMap f (Node x l r) = F.foldMap f l `mappend` f x `mappend` F.foldMap f r Quote from LYAH: "So if we just implement foldMap for some type, we get foldr and foldl on that type for free !" . Can someone explain this? I don't understand how and why do I get foldr and foldl for free now...

Composing Monads v. Applicative Functors

你离开我真会死。 提交于 2019-12-27 22:06:16
问题 The Typeclassopedia's Monad Transformers section explains: Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use Applicative if you don’t need the full power that Monad provides) Looking at the types of >>= and <*> , the above statement is not clear to me. (<*>) :: Applicative f => f (a -> b) -> f a -> f b (>>=) :: Monad m => m a -> (a -> m b) -> m b Please explain the "monads do not compose as nicely as applicative functors." I read this answer,

Composing Monads v. Applicative Functors

 ̄綄美尐妖づ 提交于 2019-12-27 22:05:20
问题 The Typeclassopedia's Monad Transformers section explains: Unfortunately, monads do not compose as nicely as applicative functors (yet another reason to use Applicative if you don’t need the full power that Monad provides) Looking at the types of >>= and <*> , the above statement is not clear to me. (<*>) :: Applicative f => f (a -> b) -> f a -> f b (>>=) :: Monad m => m a -> (a -> m b) -> m b Please explain the "monads do not compose as nicely as applicative functors." I read this answer,