haskell

Why does my Mapreduce implementation (real world haskell) using iteratee IO also fails with “Too many open files”

♀尐吖头ヾ 提交于 2019-12-30 04:22:08
问题 I am implementing a haskell program wich compares each line of a file with each other line in the file. Which can be implemented single threaded as follows distance :: Int -> Int -> Int distance a b = (a-b)*(a-b) sumOfDistancesOnSmallFile :: FilePath -> IO Int sumOfDistancesOnSmallFile path = do fileContents <- readFile path return $ allDistances $ map read $ lines $ fileContents where allDistances (x:xs) = (allDistances xs) + ( sum $ map (distance x) xs) allDistances _ = 0 This will run in O

Why is repeat defined in Prelude as it is? [duplicate]

*爱你&永不变心* 提交于 2019-12-30 04:06:35
问题 This question already has answers here : Why recursive `let` make space effcient? (3 answers) Closed 6 years ago . Repeat is defined as follows: repeat :: a -> [a] repeat x = xs where xs = x:xs Is there any reason that the following isn't used? repeat :: a -> [a] repeat x = x : repeat x (Obviously there are many equivalent definitions for many Prelude functions, but my latter description just feels much more obvious. I wonder if there's a performance or style reason for the way it is.) 回答1:

json parsing in haskell

醉酒当歌 提交于 2019-12-30 04:02:08
问题 I'm trying to parse JSON data in haskell. Having gone through a slew of websites, this is the furthest I have been able to get to. data Address = Address { house :: Integer, street :: String, city :: String, state :: String, zip :: Integer } deriving (Show) data Person = Person { name :: String, age :: Integer, address :: Address } deriving (Show) getName :: Person -> String getName (Person n _ _) = n getAddress :: Person -> Address getAddress (Person _ _ a) = a getState :: Address -> String

Haskell arrays vs lists

a 夏天 提交于 2019-12-30 03:48:50
问题 I'm playing with Haskell and Project Euler's 23rd problem. After solving it with lists I went here where I saw some array work. This solution was much faster than mine. So here's the question. When should I use arrays in Haskell? Is their performance better than lists' and in which cases? 回答1: And if you're doing much indexing as well as much updating,you can use Map s (or IntMap s), O(log size) indexing and update, good enough for most uses, code easy to get right or, if Map s are too slow,

Debugging a memory leak that doesn't show on heap profiling

99封情书 提交于 2019-12-30 03:38:12
问题 I'm working on a Haskell daemon that receives and processes JSON requests. While the operations of the daemon are complex, the main structure is intentionally kept simple: Its internal state is just an IORef with a data structure and all threads perform atomic operations on this IORef . Then there are a few threads that upon a trigger take the value a do something with it. The problem is that the daemon is leaking memory and I can't find out why. It's certainly related to the requests: when

Is it possible to generate and run TemplateHaskell generated code at runtime?

拟墨画扇 提交于 2019-12-30 03:22:12
问题 Is it possible to generate and run TemplateHaskell generated code at runtime? Using C, at runtime, I can: create the source code of a function, call out to gcc to compile it to a .so (linux) (or use llvm, etc.), load the .so and call the function. Is a similar thing possible with Template Haskell? 回答1: Yes, it's possible. The GHC API will compile Template Haskell. A proof-of-concept is available at https://github.com/JohnLato/meta-th, which, although not very sophisticated, shows one general

Zipping with padding in Haskell

断了今生、忘了曾经 提交于 2019-12-30 03:07:49
问题 A couple of times I've found myself wanting a zip in Haskell that adds padding to the shorter list instead of truncating the longer one. This is easy enough to write. ( Monoid works for me here, but you could also just pass in the elements that you want to use for padding.) zipPad :: (Monoid a, Monoid b) => [a] -> [b] -> [(a, b)] zipPad xs [] = zip xs (repeat mempty) zipPad [] ys = zip (repeat mempty) ys zipPad (x:xs) (y:ys) = (x, y) : zipPad xs ys This approach gets ugly when trying to

Ghc: partially compile Haskell code?

前提是你 提交于 2019-12-30 02:47:10
问题 When I compile a Haskell file with ghci , typically with :load , and if there is no type error, all the expressions are loaded in the ghc interpreter. It's very nice: I can play around with :t to figure out the type of various expressions. My problem is: if there is a tiny error somewhere, ghci is not able to load anything (not even the imported modules!!), which makes finding the right types even more difficult. I always do the same: comment out all the bits that do not typecheck, find the

Is there a Python equivalent of the Haskell 'let'

僤鯓⒐⒋嵵緔 提交于 2019-12-30 02:42:06
问题 Is there a Python equivalent of the Haskell 'let' expression that would allow me to write something like: list2 = [let (name,size)=lookup(productId) in (barcode(productId),metric(size)) for productId in list] If not, what would be the most readable alternative? Added for clarification of the let syntax: x = let (name,size)=lookup(productId) in (barcode(productId),metric(size)) is equivalent to (name,size) = lookup(productId) x = (barcode(productId),metric(size)) The second version doesn't

Printing an AST with variable names

家住魔仙堡 提交于 2019-12-30 02:27:06
问题 I am trying to implement an EDSL in Haskell. I would like to pretty print the AST with the variable names that are bound (if I can't get the real names then some generated names would do). This is how far I have got with a simple example: import Control.Monad.State data Free f a = Roll (f (Free f a)) | Pure a instance Functor f => Monad (Free f) where return = Pure (Pure a) >>= f = f a (Roll f) >>= g = Roll $ fmap (>>= g) f data Expr a = I a | Plus (Expr a) (Expr a) deriving (Show) data