haskell

Pattern matching data types in Haskell. Short cuts?

In the following Haskell code, how can this be written more succinctly? Is it necessary to list all four conditions, or can these be summarized by a more compact pattern? For instance, is there a way I can take advantage of Haskell already knowing how to add a float and an int, without having to manually specify fromIntegral? data Sig...

Pointer equality in Haskell?

Is there any notion of pointer quality in Haskell? == requires things to be deriving Eq, and I have something which contains a (Value -> IO Value), and neither -> nor IO derive Eq. EDIT: I'm creating an interpreter for another language which does have pointer equality, so I'm trying to model this behavior while still being able to use H...

Finding whether or not an item is contained within an k-ary tree

I have a data type data KTree a = Empty | Leaf a | Node a [KTree a] deriving (Eq, Show) I would like to write a function that returns either true or false as to whether an item is contained within my tree. ktreeContains :: Eq a => a -> (KTree a) -> Bool ktreeContains _ Empty = False ktreeContains y (Leaf x) = (x==y) -- code for node...

Calculating a product recursively only using addition

I don't know why the following haskell source code for calculating products recursively only using addition doesn't work. mult a b = a + mult a (b-1) I'm always getting a stack overflow error. ...

How to get 64-bit binaries from GHC for Snow Leopard?

Hey guys, I've recently upgraded my OS to Snow Leopard, which broke my GHC. I was able to fix it on one machine by adding flags for 32-bit compiles in /usr/bin/ghc (something like -optl -m32 -opta -m32 -optc -m32, gathered from here). Now I can't get it to produce 64-bit binaries for my other machine, which supports 64-bits. The 32-b...

Ways to get the middle of a list in Haskell?

I've just started learning about Functional Programming, using Haskel. I'm slowly getting through Erik Meijer's lectures on Channel 9 (I've watched the first 4 so far) and in the 4th video Erik explains how tail works, and it fascinated me. I've tried to write a function that returns the middle of a list (2 items for even lengths, 1 fo...

How do you route stdin from a file to a function when running GHCI

I'm using ghci 6.10.4 at the dos command line in XP, and also in emacs using haskell-mode-2.4 When running programs that operate on stdin, is there a way I can redirect a file to be stdin? For example if I have a function called main that reads from stdin, I can't do: *Main> main < words.txt Is there another way? Also I would like t...

Remove elements by index in haskell

I'm new in haskell and I'm looking for some standard functions to work with lists by indexes. My exact problem is that i want to remove 3 elements after every 5. If its not clear enough here is illustration: OOOOOXXXOOOOOXXX... I know how to write huge function with many parameters, but is there any clever way to do this? ...

Problem detecting cyclic numbers in Haskell

Hi I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3) `div` 2 p8 n = n*(3*n -2) x n = take 2 $ show n x2 n = reverse $ take 2 $ reverse $ show n pX p = dropWhile (< 999) $ takeWhile (< ...

Are peekCString and peekCStringLen lazy?

I have a C function that creates a null terminated string and returns a pointer to it, there is also corresponding deallocation function. foreign import ccall unsafe "get_str" getStr :: IO CString foreign import ccall unsafe "free_str" freeStr :: CString -> IO () I want to create a Haskell String from the returned CString, and free CS...

Efficient queue in Haskell.

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: start x = [] end x = reverse start -- [] start1 = [1,2,3] ++ start end start1 -- [3,2,1] end should be able to do this without invoking 'r...

Iterating over arrays in haskell

My problem is that I need to iterate over array and calculate some value depend on every element. I was looking for some fold-like function for arrays, but standard library seems to be very useless with arrays. Or i'm missing something? The other solution may be 'binding' array to a list. Binding mean that I don't want to copy that arra...

Extending an existing type in OCaml

I've been doing some OCaml programming lately to learn the language and to get more acquainted with functional programming. Recently, I've started to think that I'd like to be able to extend an existing type (either built in-or one of my own), for example: type bexp = And of bexp * bexp | Or of bexp * bexp | Xor of bexp * bexp | Not ...

HDBC Driver for FireBird Database

Can someone point me to an HDBC(Haskell) Driver for FireBird Database, Nothing turned up on Google. Is my best option at this point to use an HDBC-ODBC bridge to connect to a FireBird database from a Haskell Program? ...

New Line Haskell

Hey. For a tutorial this week, one of the questions asks to create a function formatLines by using other functions formatLine and formatList, to format a list of lines. My code looks like this; type Line = String formatLine :: Line -> String formatLine l = l ++ "\n" formatList :: (a -> String) -> [a] -> String formatList f [] = [] fo...

How foldr works

Can anybody explain how foldr works? Take these examples: Prelude> foldr (-) 54 [10,11] 53 Prelude> foldr (\x y -> (x+y)/2) 54 [12,4,10,6] 12.0 I am confused about these executions, any suggestions? ...

Euler Problem in Haskell -- Can Someone Spot My Error

Hi all, I'm trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up to the task, but I'm getting a result that looks inconsistent to say the least. Here's my palindrome detector (which was simplic...

Haskell tail function for empty lists

Hi, I have a problem with a function that should only return the tail of a list. The functions is myTail and should give a useable result, even if the input is an empty list. I want to understand all 3 ways: pattern matching, guarded equation and conditional expressions this works: >myTail_pat :: [a] -> [a] >myTail_pat (x:xs) = xs >m...

Raise ParseError in Haskell/Parsec

What is the prefered way to raise errors (ParseError) in Parsec? I got some code inside a parser that performs a check and if the check fails a ParseError should be returned (i.e. Left ParseError when running parse). ...

Help explain this chunk of haskell code that outputs a stream of primes

Hi, I have trouble understanding this chunk of code: let sieve (p:xs) = p : sieve {filter (\ x -> x 'mod' p /= 0) xs) in sieve [2 .. ] Can someone break it down for me? I understand there is recursion in it, but thats the problem I can't understand how the recursion in this example works. Thank you! ...