haskell

Disadvantage of unlifted type products?

In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and (a, (b, c)). If all pattern matches of all products was always irrefutable, then there would be no difference, and (a, b, c) could be syntactic sugar for (a, (b, c)). Why did Haskell choose to lift type products? ...

Why are side-effects modeled as monads in Haskell?

Could anyone give some pointers on why the unpure computations in Haskell are modeled as monads? I mean monad is just an interface with 4 operations, so what was the reasoning to modeling side-effects in it? ...

Is Software Transactional Memory the same as database transactions?

I have read alot about Software Transactional Memory, especially in relaiton to Haskell but I am trying to figure how it is different from database transactions? Are there some advantages I do not understand with STM? ...

Haskell's type system treats a numerical value as function?

After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin> :t ((+) . ($) . (+)) ((+) . ($) . (+)) :: (Num a) => a -> (a -> a) -> a -> a (Data.Maclaurin is exported by the package vector-space.) So it takes a Num, a function, another Num and ultimately returns a Num. What magic makes the following wo...

Wrong IO actions order using putStr and getLine

I have a code : main = do putStr "Test input : " content <- getLine putStrLn content And when I run it (with runhaskell) or compile it (ghc 6.10.4) result is like this: asd Test input : asd I'm new to haskell and in my opinion printing should be first. Am I right? In code sample on http://learnyouahaskell.com/ which use...

Haskell quiz: a simple function

I'm not a Haskell programmer, but I'm curious about the following questions. Informal function specification: Let MapProduct be a function that takes a function called F and multiple lists. It returns a list containing the results of calling F with one argument from each list in each possible combination. Example: Call MapProduct wit...

Haskell: 'No instance for' arising from a trivial usage of Regex library

Following the (accepted) answer from this question, I am expecting the following to work: Prelude Text.Regex.Posix Text.Regex.Base.RegexLike Text.Regex.Posix.String> makeRegex ".*" (makeRegex is a shortcut for makeRegexOpts with predefined options) However, it doesn't: <interactive>:1:0: No instance for (RegexMaker regex compOpt...

"Ambigous type variable" error when defining custom "read" function

While trying to compile the following code, which is enhanced version of read build on readMay from Safe package. readI :: (Typeable a, Read a) => String -> a readI str = case readMay str of Just x -> x Nothing -> error ("Prelude.read failed, expected type: " ++ (show (typ...

[Haskell] Set of an Unordered Data Type with a given ordering

I'm trying to put this data type in a Haskell Set, but I don't want to give it a general instance of Ord. So I want to give the set an ordering on y-coördinate but without instance Ord Vector. Is this possible? data Vector = V { x :: Double , y :: Double } deriving (Eq) ...

In Haskell, will calling length on a Lazy ByteString force the entire string into memory?

I am reading a large data stream using lazy bytestrings, and want to know if at least X more bytes is available while parsing it. That is, I want to know if the bytestring is at least X bytes long. Will calling length on it result in the entire stream getting loaded, hence defeating the purpose of using the lazy bytestring? If yes, the...

verboseCheck in QuickCheck 2?

The function verboseCheck from QuickCheck 1 seems to be absent in QuickCheck 2 (or at least, I can't find it). Is there any other way to show which values are used during testing? ...

In Haskell, I want to read a file and then write to it. Do I need strictness annotation?

Hi, Still quite new to Haskell.. I want to read the contents of a file, do something with it possibly involving IO (using putStrLn for now) and then write new contents to the same file. I came up with: doit :: String -> IO () doit file = do contents <- withFile tagfile ReadMode $ \h -> hGetContents h putStrLn contents wit...

C compiler selection in cabal package

I decided to add some flags to control the way that C source file is compiled (i.e. something like use-clang, use-intel etc.). C-Sources: c_lib/tiger.c Include-Dirs: c_lib Install-Includes: tiger.h if flag(debug) GHC-Options: -debug -Wall -fno-warn-orphans CPP-Options: -DDEBUG C...

"Subclassing" show in Haskell?

Lets say I have the following: data Greek = Alpha | Beta | Gamma | Phi deriving Show I want to use the default showing of all items except Beta, which I want to say "two". Can I do this? ...

Haskell: convert unicode integer to actual unicode character

Suppose that my Haskell function is given an input, which is supposed to be the number of a unicode code point. How can one convert this to the corresponding character? Example: 123 to '{'. ...

How to apply two functions onto the same string without using ">>"

Hi, I am writing a function like this. func :: IO() func = putStr print "func = putStr print" I know it is incorrect but the idea is I want the putStr applied onto the string then print applied onto the same string "fun = .." so that the output would be: func = putStr print "func = putStr print" which is the same as my function def...

Why must named functions be in a separate file in hugs?

In Haskell in 5 steps the factorial function is defined as follows: let fac n = if n == 0 then 1 else n * fac (n-1) But for hugs, it says that fac needs to be in fac.h. Can anyone explain why this is the case - missing the ability to define named functions seems like a massive limitation for an interpreter? ...

many1 no longer works with Parsec 3.x

After updating to Parsec 3.1 from 2.x, code using many1, such as word = many1 letter fails with No instance for (Stream s m Char) arising from a use of `letter' I found a mailing list post claiming that adding {-#LANGUAGE NoMonomorphismRestriction #-} to the top of the source file would solve the problem, but it did not. ...

What is wrong with my definition of Zip in Haskell?

-- eg. myzip [’a’, ’b’, ’c’] [1, 2, 3, 4] -> [(’a’, 1), (’b’, 2), (’c’, 3)] myzip :: Ord a => [a] -> [a] -> [(a,a)] myzip list1 list2 = [(x,y) | [x, _] <-list1, [y,_] <-list2 ] I get this error message: Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `myzip' Failed, modules loaded: n...

How do you write the function 'pairs' in Haskell?

The pairs function needs to do something like this: pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)] ...