haskell

When choosing a functional programming language for use with LLVM, what are the trade-offs?

Let's assume for the moment that C++ is not a functional programming language. If you want to write a compiler using LLVM for the back-end, and you want to use a functional programming language and its bindings to LLVM to do your work, you have two choices as far as I know: Objective Caml and Haskell. If there are others, then I'd like...

Koch Snowflake Implementation in Haskell

I was looking at this on the Wikipedia Page and was wondering if anyone has a working implementation of this. I'm trying to learn Haskell, finding it slightly difficult and am working on the Koch Snowflake and Sierpinski Triangle. Any code or suggestions welcome. Thanks ...

An error with Haskell classes I fall all the time and can't understand

Hi, there's an error I come across all the time but can't understand how to make it right. An example of code that gives me this error is: class Someclass a where somefunc :: (Num b) => b -> a -> a data Sometype = Somecons Int instance Someclass Sometype where somefunc x (Somecons y) = Somecons (x+y) The error message is: ...

Comparing wildcards for equality in Haskell..?

In Haskell, is there a way to compare that all wildcards are of the same type and value? For example, I want to create a function that exhibits the following behavior: (1 M) (2 M) (3 M) -> True (1 S) (2 S) (3 S) -> True (1 S) (2 M) (3 S) -> False In other words, the first parameter should be 1, 2 and 3 and the second parameter should ...

Equivalence testing in Haskell

Hi A quick question that has been bugging me lately. Does Haskell perform all the equivalence test in a function that returns a booleanm, even if one returns a false value? eg: f a b = ((a+b) == 2) && ((a*b) == 2) If the first test returns false, will it perform the second test after the &&? Or is Haskell lazy enough to not do it and...

How to rearrange this function to return the extended list in Haskell

Hi I am doing problem 68 at project euler and came up with the following code in Haskell to return the list of numbers which fit the (given) solution: lists = [n|n<- permutations [1..6] , ring n ] ring [a,b,c,d,e,f] = (length $ nub $ map sum [[d,c,b],[f,b,a],[e,a,c]]) == 1 This only returns a list of lists of 6 numbers each which f...

Inventing a suitable infix operator symbol for liftM

When working with monadic expressions in Haskell, the use of liftM's (even in infix position) often seems quite unaesthetic and verbose to me. Most other monadic primitives (>>=, >>) and even liftM's pure pendant $ are infix operators. This makes me think why there is no operator symbol for monadic lifting. Do you have reasonable, cons...

Creating monads in haskell

I want to create my own monad. This is what i wrote: data LeafConType a = LeafCon (a,Int,Int) instance Monad (LeafConType ) where return = LeafCon lc@(LeafCon (t,i,n)) >>= f = if i>=n then lc else f (t,i,n) But this dont work. Ghc says: leafcon.hs:26:1: Occurs chec...

Haskell too many where clauses, any alternate suggestions

I am totally new in Haskell and when writing small programs i normally end up with too many where clauses to check many things in the function, so it is good practice to write where clauses or is there any other good alternatives for this ? for example in the code below i tried to find if there is ant duplicate elements in each row of t...

Haskell minimum/maximum Double Constant

Is there any way in Haskell to get the constant that is the largest and smallest possible positive rational number greater than zero that can be represented by doubles? ...

How to recursively compare the digits in a number in Haskell

Hi I am doing problem 112 on Project Euler and came up with the following to test the example case (I'll change the number in answer to 0.99 to get the real answer): isIncre x | x == 99 = False | otherwise = isIncre' x where isIncre' x = ??? isDecre x = isIncre (read $ reverse $ show x :: Int) isBouncy x = (is...

Why is this Haskell incorrect?

I have a Haskell file which looks like this: longest::[Integer]->[Integer]->[Integer] max a b = if length a > length b then a else b llfs::[Integer]->Integer llfs li = length(foldl longest group(li)) llfs([1, 2, 3, 3, 4, 5, 1, 1, 1]) Which should print out the result of the function call at the end, however when I run the file I get...

Generating an infinite sequence in Haskell

I know that infinite sequences are possible in Haskell - however, I'm not entirely sure how to generate one Given a method generate::Integer->Integer which take an integer and produces the next integer in the sequence, how would I build an infinite sequence out of this? ...

Haskell: How do I define the types my function can take as parameters? AND how do I access unnamed variables in a data structure?

1) Here is my code, the find function needs to take a (Node a) and a type (a) as parameters but my function definition doesn't seem to work, what am I doing wrong? Little info on the net that I can find, so thanks for any help! 2) When my find function is implemented I'll need to access a specific variable in a Node, how do I do this?!?...

how to simulate the concept of object identity in Haskell

I am considering the design of an interpreter for Python like object oriented language in Haskell. One particular problem I am facing is related to the concept of object identity. If we consider Python's id(object) function, the definition suggests that it returns the "identity" of an object. This is an integer (or long integer) which is...

Can somebody walk me through this Haskell function (State monad related)?

tick :: State Int Int tick = get >>= \n -> put (n+1) >>= \y -> return n I'm confused as to how put (n+1) has any effect on the end result of this function at all. It seems like this function should return the initial state unchanged. I'm trying to run through this in my mind, but I keep running out of room to hold things ...

Filter/Map composition problem Haskell

I've been given this question in a tutorial, and I really don't know how to go about it. How must g and h be defined in terms of p and f in order to ensure that filter p . map f = map g . filter h always holds? Any pointers in the right direction would be greatly appreciated. ...

Multiple Haskell cabal-packages in one directory

What is the recommended way of having several cabal packages in one directory? Why: I have an old project with many separable modules. Since originally they formed just one program it was, and still is, handy to have them in same directory for easy compiling. Options Just suffer and split everything, including VCS holding the stuff, ...

multicore programming in Haskell - Control.Parallel

I'm trying to learn how to use the Control.Parallel module but I think I didn't get it right. I'm trying to run the following code (fibs.hs): import Control.Parallel fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = p `par` (q `pseq` (p + q)) where p = fib (n-1) q = fib (n-2) main = print $ fib 30 I compiled this w...

Algorithm - How to delete duplicate elements in a list efficiently?

There is a list L. It contains elements of arbitrary type each. How to delete all duplicate elements in such list efficiently? ORDER must be preserved Just an algorithm is required, so no import any external library is allowed. Related questions In Python, what is the fastest algorithm for removing duplicates from a list so that all ...