haskell

GHC Haskell performance of IPv4 address rendering

这一生的挚爱 提交于 2020-01-02 07:27:29
问题 I recently built a library for handling IPv4 address in haskell. I have written two functions to render an IPv4 address to Text and I am surprised that the naive approach outperforms the approach that I actually thought about. Here are the relevant pieces. First, there is the definition of IPv4: newtype IPv4 = IPv4 { getIPv4 :: Word32 } Next we have the IP address renderer that I expected to perform well: toDotDecimalText :: IPv4 -> Text toDotDecimalText = LText.toStrict . TBuilder.toLazyText

Why does “cabal init” break “ghc-mod check”?

时光总嘲笑我的痴心妄想 提交于 2020-01-02 07:13:35
问题 ghc-mod works in a directory with just a haskell source file but if I run "cabal init" in that directory (/tmp/test), I get the following error: (p1)dave@peach:/tmp/test$ !ghc ghc-mod check Main.hs ghc-mod: /tmp/test/dist/setup-config: hGetContents: invalid argument (invalid byte sequence) What's going wrong here? I'm running ghc-mod 5.2.11, and cabal 1.21.1.0, on a 64 bit ubuntu 14.04 computer. the LANG environment variable is set to en_US.UTF-8 ( I saw responses to some other questions that

Haskell foldl and stack overflow?

蹲街弑〆低调 提交于 2020-01-02 06:05:52
问题 I read a posting claims foldl may occur stack overflow easily. And the posting sample code was: maximum [1..1000000] The code doesn't overflown in my machine. However it can vary by environment. I increased number like this: maximum [1..1000000000] it caused hard disk swapping, so I have to stop evaluation. Sample code is not important. Is it really occur stack overflow? Or just an old days story? 回答1: Some points Recursive function take stack space in each call, so deeply nested calls will

Haskell foldl and stack overflow?

五迷三道 提交于 2020-01-02 06:04:20
问题 I read a posting claims foldl may occur stack overflow easily. And the posting sample code was: maximum [1..1000000] The code doesn't overflown in my machine. However it can vary by environment. I increased number like this: maximum [1..1000000000] it caused hard disk swapping, so I have to stop evaluation. Sample code is not important. Is it really occur stack overflow? Or just an old days story? 回答1: Some points Recursive function take stack space in each call, so deeply nested calls will

Haskell foldl and stack overflow?

一世执手 提交于 2020-01-02 06:03:11
问题 I read a posting claims foldl may occur stack overflow easily. And the posting sample code was: maximum [1..1000000] The code doesn't overflown in my machine. However it can vary by environment. I increased number like this: maximum [1..1000000000] it caused hard disk swapping, so I have to stop evaluation. Sample code is not important. Is it really occur stack overflow? Or just an old days story? 回答1: Some points Recursive function take stack space in each call, so deeply nested calls will

why cant an Int and a floating point number be added in haskell

允我心安 提交于 2020-01-02 05:40:11
问题 why wont this work :- (length [1,2,3,4]) + 3.2 while this works:- 2+3.3 I understand that in the first case the result is an Int+Float but is that not the same in the second case too, or does Haskell automatically infer the type in the second case to be :- Num+Num whereas it does not do that in the first case? 回答1: Haskell never does implicit type conversion for you. + only ever works on two numbers of the same type, and gives you that type as the result as well. Any other usage of + is an

Unusual Kinds and Data Constructors

醉酒当歌 提交于 2020-01-02 05:39:12
问题 I don't know how I didn't notice this, but data constructors and function definitions alike can't use types with kinds other than * and it's variants * -> * etc., due to (->) 's kind signature, even under -XPolyKinds . Here is the code I have tried: {-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} data Nat = S Nat | Z data Foo where Foo :: 'Z -> Foo -- Fails foo :: 'Z -> Int -- Fails foo _ = 1 The error I'm getting is the following: <interactive>:8:12: Expected a type, but ‘Z’ has

Returning something from another type class B in function of type class A in Haskell

强颜欢笑 提交于 2020-01-02 05:39:11
问题 I'm doing a fun project in which I'm trying to redo some basic data types and concepts from Java. Currently I'm tackling Iterators. My approach is the following: (1) Translate Interfaces to Typeclasses (2) Declare custom data types and instances for actual implementations So I created the following type classes: class Iterator it where next :: it e -> (it e, e) hasNext :: it e -> Bool class Iterable i where iterator :: Iterator it => i e -> it e class Iterable c => Collection c where add :: c

Replace record projection function with lenses

你说的曾经没有我的故事 提交于 2020-01-02 05:38:12
问题 Almost every time I make a record, I find myself adding makeLenses ''Record (from lens) immediately afterwards, and I never end up actually using the projection functions that the record gives me. In fact, looking at what makeLenses produces (with the GHC -ddump-splices flag), it looks like not even it uses those projection functions, except to choose a name for the lenses it produces. Is there some way, either through TemplateHaskell , or through a preprocessor, or frankly any other magic,

Haskell - apply tuple of functions to tuple of values?

房东的猫 提交于 2020-01-02 05:19:08
问题 I have a tuple of values representing some state, and want to translate it by an addition (shift). My values are a longer version of ( Int, [Int], Int), and I want something conceptually (but not literally) like this: shift n = ??? (+n) (id, map, id) -- simple(?) which would be equivalent to: shift n (a, b, c) = (a+n, map (+n) b, c+n) I am happy to just go with this explicit function usage, but wondered it there was a more idiomatic point-free version using Applicative or Arrows or ..., or if