haskell

Haskell Lazy ByteString + read/write progress function

匆匆过客 提交于 2019-12-29 03:04:23
问题 I am learing Haskell Lazy IO. I am looking for an elegant way to copy a large file (8Gb) while printing copy progress to console. Consider the following simple program that copies a file silently. module Main where import System import qualified Data.ByteString.Lazy as B main = do [from, to] <- getArgs body <- B.readFile from B.writeFile to body Imgine there is a callback function you want to use for reporting: onReadBytes :: Integer -> IO () onReadBytes count = putStrLn $ "Bytes read: " ++

ExitFailure 9 when trying to install ghc-mod using Cabal

孤街浪徒 提交于 2019-12-29 03:03:05
问题 When I try to install it with cabal, I get ~$ cabal install ghc-mod Resolving dependencies... [1 of 1] Compiling Main ( /tmp/haskell-src-exts-1.13.5-21238/haskell-src-exts-1.13.5/Setup.hs, /tmp/haskell-src-exts-1.13.5-21238/haskell-src-exts-1.13.5/dist/setup/Main.o ) /tmp/haskell-src-exts-1.13.5-21238/haskell-src-exts-1.13.5/Setup.hs:1:1: Warning: In the use of `runTests' (imported from Distribution.Simple, but defined in Distribution.Simple.UserHooks): Deprecated: "Please use the new testing

Impredicative types vs. plain old subtyping

北城余情 提交于 2019-12-29 02:47:08
问题 A friend of mine posed a seemingly innocuous Scala language question last week that I didn't have a good answer to: whether there's an easy way to declare a collection of things belonging to some common typeclass. Of course there's no first-class notion of "typeclass" in Scala, so we have to think of this in terms of traits and context bounds (i.e. implicits). Concretely, given some trait T[_] representing a typeclass, and types A , B and C , with corresponding implicits in scope T[A] , T[B]

Curious about the HashTable performance issues

一曲冷凌霜 提交于 2019-12-29 02:45:23
问题 I read that hash tables in Haskell had performance issues (on the Haskell-Cafe in 2006 and Flying Frog Consultancy's blog in 2009), and since I like Haskell it worried me. That was a year ago, what is the status now (June 2010)? Has the "hash table problem" been fixed in GHC? 回答1: The problem was that the garbage collector is required to traverse mutable arrays of pointers ("boxed arrays") looking for pointers to data that might be ready to deallocate. Boxed, mutable arrays are the main

No instance for (Num (Int -> Int)) arising from the literal `5'

我的梦境 提交于 2019-12-29 01:39:11
问题 I have the following function: f :: (Int -> Int) -> Int f = undefined Now I want to call f with 5 (which is incorrect): f 5 Obviously, this should not compile, because 5 is not a function from Int to Int . So I would expect an error message like Couldn't match expected type Int -> Int with Int . But instead I get: No instance for (Num (Int -> Int)) arising from the literal `5' In the first argument of `f', namely `5' In the expression: f 5 In an equation for `it': it = f 5 Why did Num appear

Haskell infinite recursion in list comprehension

删除回忆录丶 提交于 2019-12-29 01:28:09
问题 I am trying to define a function that accepts a point (x,y) as input, and returns an infinite list corresponding to recursively calling P = (u^2 − v^2 + x, 2uv + y) The initial values of u and v are both 0. The first call would be P = (0^2 - 0^2 + 1, 2(0)(0) + 2) = (1,2) Then that resulting tuple (1,2) would be the next values for u and v, so then it would be P = (1^2 - 2^2 + 1, 2(1)(2) + 2) = (-2,6) and so on. I'm trying to figure out how to code this in Haskell. This is what I have so far:

How to have multiple infinite ranges in list comprehensions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 00:38:06
问题 In haskell I have a list comprehension like this: sq = [(x,y,z) | x <- v, y <- v, z <- v, x*x + y*y == z*z, x < y, y < z] where v = [1..] However when I try take 10 sq , it just freezes... Is there a way to handle multiple infinite ranges? Thanks 回答1: In addition to the other answers explaining the problem, here is an alternative solution, generalized to work with level-monad and stream-monad that lend themselves for searches over infinite search spaces (It is also compatible with the list

List partitioning implemented recursively

百般思念 提交于 2019-12-28 19:17:44
问题 I am a Haskell beginner and I have been experimenting with recursive functions. I am working on a function: separate :: [a] -> [[[a]]] that takes in a list and outputs all of the partitions of that list. For example 123 becomes: 1|2|3 12|3 1|23 13|2 132 I have only been able to implement a recursive function that creates the 1|2|3 variant: separate' :: [a] -> [[a]] separate' (r:rs) = [r]:separate' xs >separate [1,2,3] [[1],[2],[3]] I am stuck with trying to create the other variants with

In Haskell, why non-exhaustive patterns are not compile-time errors?

时间秒杀一切 提交于 2019-12-28 16:30:13
问题 This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function? I understand that using -Wall , GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not making it a compile-time error by default given that it's always possible to explicitly define a partial function: f :: [a] -> [b] -> Int f [] _ = error "undefined for empty array" f _ [] = error "undefined for empty array" f (_:xs) (_:ys) = length

Haskell: how to infer the type of an expression manually

你。 提交于 2019-12-28 12:28:52
问题 Given ist the Haskell function: head . filter fst The question is now how to find the type "manually" by hand. If I let Haskell tell me the type I get: head . filter fst :: [(Bool, b)] -> (Bool, b) But I want to understand how this works using only the signatures of the used functions which are defined as follows: head :: [a] -> a (.) :: (b -> c) -> (a -> b) -> a -> c filter :: (a -> Bool) -> [a] -> [a] fst :: (a, b) -> a Edit: so many very good explanations ... it's not easy to select the