haskell

Is there a way to optimise this program in Haskell?

Hi I am doing project euler question 224. And whipped up this list comprehension in Haskell: prob39 = length [ d | d <- [1..75000000], c <- [1..37500000], b <-[1..c], a <- [1..b], a+b+c == d, a^2 + b^2 == (c^2 -1)] I compiled it with GHC and it has been running with above average kernel priority for over an hour without returning a re...

Can I write components in Haskell to be used on a Django site?

I have an idea for a web service, but am very new to web programming. Django looks great and like something I can pick up quickly. I have a lot of experience in Haskell (and very little in python) and would like to be able to start writing some of the backend non-web-related things in my favorite language. But of course I don't want to d...

Haskell compilation problem

Hi. I have a problem compiling Haskell programs, that import the Text.Regex.Posix module. I have tried to isolate the problem in a small test program: module Main () where import Text.Regex.Posix ((=~)) main = return () Running the interpreter works fine: /regex-test$ runghc Main.hs /regex-test$ However, compiling this program wi...

How do I do python-style indent/dedent tokens with alex/haskell?

Hi, I'm writing a lexer for a small language in Alex with Haskell. The language is specified to have pythonesque significant indentation, with an INDENT token or a DEDENT token emitted whenever the indentation level changes. In a traditional imperative language like C, you'd keep a global in the lexer and update it with the indentation...

Pattern match data types and their nested name in Haskell

I have: data Color = Blue | Green | Red | White | Yellow deriving (Eq,Ord) And then data Term = Color | ... data Bag = Bag { color :: Color ... } Now I want to be able to pattern match to make sure that the term given is a Color and if so check it's "value" (Blue/Green...). Something like this: func :: Term -> Bag -> Bool func (c ...

Writing a time function in Haskell

I'm new to Haskell and I'd like to be able to time the runtime of a given function call or snippet of code. In Clojure I can use 'time': user=> (time (apply * (range 2 10000))) "Elapsed time: 289.795 msecs" 2846259680917054518906413212119868890148051... In Scala, I can define the function myself: scala> def time[T](code : => T) = {...

Haskell recursive problem, tiny parser. A few things

data Expr = Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr deriving(Eq, Show) That is the datatype for Expr, I have a few questions. I'm suppose to parse expressions like *(Expr,Expr) as shown in the datatype definition. However I do have some problems with "creating" a valid Expr. I use patte...

How to programmatically retrieve GHC package information?

More specifically, given an arbritary package name I need to retrieve the same library-dirs field that can be obtained with the ghc-pkg describe command from inside a running Haskell program. ...

How to get Haskell syntax highlighting on PHP blog

Hi, I'm making a blog in PHP and I'd really like to have syntax highlighting on Haskell code. Are there any tools for that out there? I've found hscolour but I don't know if it's possible to integrate it in PHP. I'm using CakePHP if that makes a difference. Thanks. ...

Computing π to "infinite" binary precision in C#

So far it looks like Fabrice Bellard's base 2 equation is the way to go Ironically this will require a BigReal type; do we have this for .Net? .Net 4.0 has BigInteger. Anyone have a Haskell version? ...

Concatenating a list of numbers into one integer in haskell

Hi I am doing yet another euler project question (38). I have this function which returns a list of numbers but what I need is that list of numbers to be one number. It calculates the concatenated product of an integer. f (a,b) = a*b conProInt x n = map f (zip (replicate n x) ([1..n])) prob38 = maximum [ (conProInt (x) (n)) | x <- [1...

Haskell recursive problem, tiny parser. Negation of Expr and let expressions

data Expr = Var Char | Tall Int | Sum Expr Expr | Mult Expr Expr | Neg Expr | Let Expr Expr Expr deriving(Eq, Show) parseExpr :: String -> (Expr, String) parseExpr ('*':'(':s) = (Mult x y, s'') where (x,',':s') = parseExpr s (y,')':s'') = parseExpr s' parseExpr ('+':'(':s) = (Sum x y, s'') where (x,',':s') = par...

What is your favourite cleverly written functional code?

What are your favourite short, mind-blowing snippets in functional languages? My two favourite ones are (Haskell): powerset = filterM (const [True, False]) foldl f v xs = foldr (\x g a -> g (f a x)) id xs v -- from Hutton's tutorial (I tagged the question as Haskell, but examples in all languages - including non-FP ones - are welco...

Why is Haskell throwing a 'cannot construct infinite type' error?

I wrote the following code in Haskell to compute the dot product of two vectors, but cannot compile it due to the following error: cannot construct infinite type: a = [a] When generalising the type(s) for dot' dot :: (Num a) => [a] -> [a] -> a [] `dot` [] = 0 x@[xi,xs] `dot` y@[yi,ys] = xi*yi + (xs `dot` ys) I've taken a look at thi...

Guard is skipped when it should not be.

I have the following snippet of code module Main where main :: IO() main = do ne <- getLine c <- getLine putStrLn $ show $ foo c (words ne) foo :: String -> [String] -> Integer foo c (n:e:_) = foo' (read c::Integer) (read e::Integer) (read n::Integer) [2..] where foo' c e n (x:xs) | mod (x^e) n == c = mod x n ...

Why does Haskell interpret my Num type as an Enum?

I'm trying to compile the following function in Haskell to mimic differentiation of a polynomial whose constants are specified in a numerical list: diff :: (Num a) => [a] -> [a] diff [] = error "Polynomial unspecified" diff coeff = zipWith (*) (tail coeff) [0..] Haskell refuses to compile it, giving me the following reason: Could not...

Return specific type within Haskell

I have a pretty general question about Haskell's type system. I'm trying to become familiar with it, and I have the following function: getN :: Num a => a getN = 5.0 :: Double When I run this, I get the following error: Couldn't match expected type `a' against inferred type `Double' `a' is a rigid type variable bound by the t...

Haskell recursive problem, tiny parser. Check variables

I'm still working on a tiny parser for a tiny language defined in a task at school. The parser that generates an AST(Abstract syntax tree) is working. What I want is to check the defined variables, they must be bounded by the let expression. First the method that is defined in the task(suggestion, not needed): checkVars :: Expr -> Char...

Haskell base-4.x package installation

Hi, when I try to install this package http://hackage.haskell.org/package/base-4.1.0.0 by running: runhaskell Setup configure I am getting this error: attempting to use module `System.IO' (System/IO.hs) which is not loaded Not in scope: `System.IO.stderr' Not in scope: `System.IO.stdin' ghc-6.8.2: panic! (the 'impossible' happened...

Comprehensions in Python and Javascript are only very basic?

Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell. Do they allow things like multiple generators? Or are they just a basic map-filter form? If they don't allow multiple generators, I find them quite disappointing ...