haskell

Why is GHC complaining about wrong type?

This little function checks a (finite) Brainfuck string for validity. It check's whether the [ and ] are balanced. The code is very straightforward and written to be tail-recursive: -- checks Brainfuck for validity. validateBrainfuck :: Monad m => String -> m String validateBrainfuck s = maybe (return s) (fail . fromJust) (validate s 0)...

passing functions as argument

Hi, I am new to Haskell,I should write a function that takes a function among its parameter, uses it and return a function whose behavior change depending the function I passed in, to be clear: in my application: a simple cache server, I read a context file and among all the parameter ther is "LOG: active"if log is active I have to give ...

head and tail calls on empty list bringing an exception

I'm following a tutorial. (Real World Haskell) And I have one beginner question about head and tail called on empty lists: In GHCi it returns exception. Intuitively I think I would say they both should return an empty list. Could you correct me ? Why not ? (as far as I remember in OzML left or right of an empty list returns nil) I sur...

Implement reverse in Haskell that runs in linear time

I'm just learning Haskell, so sorry if my question is stupid. I'm reading learnyouahaskell.com and now I'm at chapter 5 "Recursion". There's an example of implementation of standard 'reverse' function: reverse' :: [a] -> [a] reverse' [] = [] reverse' (x:xs) = reverse' xs ++ [x] But it seems that it runs in O(N^2) time, while the...

How to "debug" Haskell with printfs ?

Hi there, coming from the Ocaml community, I'm trying to learn a bit of Haskell. The transition goes quite well but I'm a bit confused with debugging. I used to put (lots of) "printf" in my ocaml code, to inspect some intermediate values, or as flag to see where the computation exactly failed. Since printf is an IO action, do I have to...

How to define a variable based on an if/then/else statement

I'm trying to translate some python code to haskell. However I reached a point where I'm not sure how to proceed. if len(prod) % 2 == 0: ss = float(1.5 * count_vowels(cust)) else: ss = float(count_consonants(cust)) # muliplicaton by 1 is implied. if len(cust_factors.intersection(prod_factors)) > 0: ss *= 1.5 return ss I'...

Haskell: Escaped character from character

I'm writing a parsec parser which reads in strings and converts escaped characters, as part of exercise 3 here. For that exercise I am using this function: escapedCharFromChar :: Char -> Char escapedCharFromChar c = read $ concat ["'\\",[c],"'"] I am not to impressed with the use of read to convert the character x into the escape cha...

where is the join function ?

Hi, I need the join function to add separators between the members of a list. I found this function in Data.List.Utils and Data.String.Utils. Unfortunately, those modules doesn't seem to be part of the standard platform. As it's a quite standard function, I'm pretty sure it should be somewhere in the standard platform , maybe with an ot...

Haskell: Lifting a reads function to a parsec parser

As part of the 4th exercise here I would like to use a reads type function such as readHex with a parsec Parser. To do this I have written a function: liftReadsToParse :: Parser String -> (String -> [(a, String)]) -> Parser a liftReadsToParse p f = p >>= \s -> if null (f s) then fail "No parse" else (return . fst . head ) (f s) Which...

How can I get Haskell readline installing under Cygwin?

Here's the message I get when trying to install in via cabal-install: $ cabal install --extra-include-dirs="C:\no_spaces\cygwin\usr\include" --extra-lib-dirs="C:\no_spaces\cygwin\lib" readline Resolving dependencies... Configuring readline-1.0.1.0... checking for gcc... gcc checking for C compiler default output file name... a.exe check...

Partial SQL insert in haskelldb

I just started a new project and wanted to use HaskellDB in the beginning. I created a database with 2 columns: create table sensor ( service text, name text ); ..found out how to do the basic HaskellDB machinery (ohhh..the documentation) and wanted to do an insert. However, I wanted to do a partial insert (there are supposed ...

haskell floating point precision

I wrote a little program to find the position of a point(x,y) in relation to a line defined by a point(px,py) and an angle(deg) to the x-axis (in a cartesian coordinate system). toRadian deg = deg * (pi / 180) lineSlope deg = tan $ toRadian deg lineYintercept (x,y) deg = y - (x * lineSlope deg) relativePointPosition (px,py) deg (x,y)...

Does D have 'newtype'?

Does D have 'newtype' (as in Haskell). It's a naive question, as I'm just skimming D, but Google didn't turn up anything useful. In Haskell this is a way of making different types of the same thing distinct at compile time, but without incurring any runtime performance penalties. e.g. you could make newtypes (doubles) for metres, seco...

How does the Haskell compiler handle the 'where' statement?

In the following function, I'm wondering if the compiler is clever enough to work out that x is going to remain constant, or will it compute the head of the list for every item in the list? (I'm using GHC) allSame :: Eq a => [a] -> Bool allSame xs = all (==x) xs where x = head xs ...

List all real users on Linux/*nix and data associated with them

Hello I'd like to list all real users on Linux/*nix and it's data. I probably need to parse /etc/passwd and /etc/shadow and check if user have password. The problem is that is not cross-platform (on *BSD there is no /etc/shadow). Is there any cross-platform library/command I can use? ...

understanding attoparsec

Hi I am studing Haskell, attoparsec was suggested to me for parsing a file, now I must to understand how to use it; somebody gave me this piece of code: # type Environment = M.Map String String import Data.Attoparsec (maybeResult) import qualified Data.Attoparsec.Char8 as A import qualified Data.ByteString.Char8 as B environment :: ...

What is Haskell good at?

Possible Duplicate: What is Haskell actually useful for? What is Haskell good at? ...

Beginning Haskell - getting "not in scope: data constructor" error

I'm going through the problems in the Haskell O'Reilly book. The problem I am working on is Using the binary tree type that we defined earlier in this chapter, write a function that will determine the height of the tree. The height is the largest number of hops from the root to an Empty. For example, the tree Empty has height zero; Nod...

Ocaml: how to pattern match on an arbitrary number of arguments?

Hi all, is there an OCaml equivalent to Haskell's pattern matching on an arbitrary number of arguments? For example, can I have something resembling: merge [] lst = lst merge lst [] = lst merge l1 @ (n : ns) l2 @ (m : ms) = if n < m then n : merge ns l2 else m : merge l1 ms (The example's lifted from Developing Applications with O...

How to factor a number functionally

For example, if the input is 825 the output expected is (0 1 2 0 1). What this means is: 0 two's, 1 three's, 2 five's, 0 seven's and 1 eleven. Doing this imperatively was quite easy for me. Functional, not so much. Could you please guide me how to go about solving the above problem in a functional way? Note: Fold/reduce ways will be pr...