haskell

Performance problem with Euler problem and recursion on Int64 types

。_饼干妹妹 提交于 2019-12-29 05:58:50
问题 I'm currently learning Haskell using the project Euler problems as my playground. I was astound by how slow my Haskell programs turned out to be compared to similar programs written in other languages. I'm wondering if I've forseen something, or if this is the kind of performance penalties one has to expect when using Haskell. The following program in inspired by Problem 331, but I've changed it before posting so I don't spoil anything for other people. It computes the arc length of a

What is “n” in RankNTypes

守給你的承諾、 提交于 2019-12-29 05:19:24
问题 I understand how forall enables us to write polymorphic function. According to this chapter, the normal function which we generally write are Rank 1 types. And this function is of Rank 2 type: foo :: (forall a. a -> a) -> (Char,Bool) foo f = (f 'c', f True) It explains like this: In general, a rank-n type is a function that has at least one rank-(n-1) argument but no arguments of even higher rank. What does it actually mean by rank argument ? Can somebody give an example of Rank 3 type which

What does uncurry ($) do?

隐身守侯 提交于 2019-12-29 05:18:33
问题 I'm doing some excersises where I have to add a function's type and explain what it does. I'm stuck with this: phy = uncurry ($) The type, according to GHCi is phy :: (a -> b, a) -> b . My haskell knowledge is basic so I really have no idea what it does. 回答1: Let's spell out the type part systematically. We'll start with the types of uncurry and ($) : uncurry :: (a -> b -> c) -> (a, b) -> c ($) :: (a -> b) -> a -> b Since the target expression has ($) as the argument of uncurry , let's line

How to type cast

拟墨画扇 提交于 2019-12-29 05:14:47
问题 C#: static int F(object x) { return x is string ? 1 : 2; } Haskell? The tricky bit seems to me that Haskell does not have a root type object . Edited: I do not care about converting to string. I want to know how to typecast (for example to see if an object is a Customer or an Order . 回答1: In Haskell, all types that allow a conversion to a string instantiate the Show typeclass which provides show :: Show a => a -> String So your whole code is nothing but f x = show x or f = show with the same

How to type cast

女生的网名这么多〃 提交于 2019-12-29 05:14:33
问题 C#: static int F(object x) { return x is string ? 1 : 2; } Haskell? The tricky bit seems to me that Haskell does not have a root type object . Edited: I do not care about converting to string. I want to know how to typecast (for example to see if an object is a Customer or an Order . 回答1: In Haskell, all types that allow a conversion to a string instantiate the Show typeclass which provides show :: Show a => a -> String So your whole code is nothing but f x = show x or f = show with the same

Statically link C++ library with a Haskell library

被刻印的时光 ゝ 提交于 2019-12-29 04:42:08
问题 Setup: I have a Haskell library HLib which makes calls to a C/C++ backend CLib for efficiency. The backend is small and specialized for use with HLib . The interface to CLib will only be exposed through HLib ; HLib tests, HLib benchmarks and third party libraries depending on HLib will not make direct FFI calls to CLib . From the test/benchmark/3rd party lib perspective, HLib should be appear purely Haskell. This means in the cabal file sections for, e.g., HLib tests, there should be no

What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

◇◆丶佛笑我妖孽 提交于 2019-12-29 04:30:18
问题 More broadly this question is about various approaches to the expression problem. The idea is that your program is a combination of a datatype and operations over it. We want to be able to add new cases without recompiling the old classes. Now Haskell has some really awesome solutions to the expression problem with the TypeClass. In particular - we can do: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool member :: (Eq a) => a -> [a] -> Bool member y [] = False member y (x:xs) =

Making (a, a) a Functor

主宰稳场 提交于 2019-12-29 04:13:27
问题 How can I make (a, a) a Functor without resorting to a newtype ? Basically I want it to work like this: instance Functor (a, a) where fmap f (x, y) = (f x, f y) But of course that's not a legal way to express it: Kind mis-match The first argument of `Functor' should have kind `* -> *', but `(a, a)' has kind `*' In the instance declaration for `Functor (a, a)' What I really want is a type-level function like this: \a -> (a, a) (invalid syntax). So a type alias, perhaps? type V2 a = (a, a)

Automatic conversion between String and Data.Text in haskell

为君一笑 提交于 2019-12-29 03:34:07
问题 As Nikita Volkov mentioned in his question Data.Text vs String I also wondered why I have to deal with the different String implementations type String = [Char] and Data.Text in haskell. In my code I use the pack and unpack functions really often. My question: Is there a way to have an automatic conversion between both string types so that I can avoid writing pack and unpack so often? In other programming languages like Python or JavaScript there is for example an automatic conversion between

Laziness and tail recursion in Haskell, why is this crashing?

孤街浪徒 提交于 2019-12-29 03:10:37
问题 I have this fairly simple function to compute the mean of elements of a big list, using two accumulators to hold the sum so far and the count so far: mean = go 0 0 where go s l [] = s / fromIntegral l go s l (x:xs) = go (s+x) (l+1) xs main = do putStrLn (show (mean [0..10000000])) Now, in a strict language, this would be tail-recursive, and there would be no problem. However, as Haskell is lazy, my googling has led me to understand that (s+x) and (l+1) will be passed down the recursion as