haskell

Collecting IO outputs into list

how can i do multiple calls to SDL.pollEvent :: IO Event until the output is SDL.NoEvent and collect all the results into a list? in imperative terms something like this: events = [] event = SDL.pollEvent; while( event != SDL.NoEvent ) events.add( event ) event = SDL.pollEvent ...

How to define a function in ghci across multiple lines?

I'm trying to define any simple function that spans multiple lines in ghci, take the following as an example: let abs n | n >= 0 = n | otherwise = -n So far I've tried pressing Enter after the first line: Prelude> let abs n | n >= 0 = n Prelude> | otherwise = -n <interactive>:1:0: parse error on input `|' I've a...

help understand strange behavior in a lazy code

...

Using perfectly formatted input as list in Haskell

I'm doing a program in Haskell (on the Haskell platform), and I know I'm getting perfectly formatted inputs, so the input may look like [ ['a'], ['b'], ['c'] ] I want Haskell to be able to take this and use it as a list of it's own. And, I'd like this list to be over multiple lines, i.e., I want this to also work: [ ['a'], ['b'],...

Does any method exist quickly to detect valid range versions of used library

I'm a beginner Haskell programmer. I have written some useful code for the last six month. And I want to release a library from it. The code will use system installation cabal as any Haskell library. A library is released with cabal has a meta data file where there is a logical predicate from the libraries and their versions. A develope...

Avoiding explicit recursion in Haskell

The following simple function applies a given monadic function iteratively until it hits a Nothing, at which point it returns the last non-Nothing value. It does what I need, and I understand how it works. lastJustM :: (Monad m) => (a -> m (Maybe a)) -> a -> m a lastJustM g x = g x >>= maybe (return x) (lastJustM g) As part of my self...

Haskell UI framework?

Is there, by chance, an emerging Haskell UI framework for Windows? I recently took up looking over the language, and from what I see, it would be for great little "one-off" applications (elaborate scripts). However, without a good UI framework I can't see it getting in under the smoke and mirrors of the more obvious contenders. I've r...

How can I implement a splay tree that performs the zig operation last, not first?

For my Algorithms & Data Structures class, I've been tasked with implementing a splay tree in Haskell. My algorithm for the splay operation is as follows: If the node to be splayed is the root, the unaltered tree is returned. If the node to be splayed is one level from the root, a zig operation is performed and the resulting tree is re...

Haskell: Defaulting constraints to type

Consider this example: applyKTimes :: Integral i => i -> (a -> a) -> a -> a applyKTimes 0 _ x = x applyKTimes k f x = applyKTimes (k-1) f (f x) applyThrice :: (a -> a) -> a -> a applyThrice = applyKTimes 3 The 3 in applyThrice is defaulted by GHC to an Integer as shown when compiling with -Wall: Warning: Defaulting the following con...

Haskell: Gluing a char and a list together?

So I have this code here: toWords :: String -> [String] toWords "" = [] toWords (nr1 : rest) | nr1 == ' ' = toWords rest | otherwise = [nr1] : toWords rest The "toWords" function should simply remove all spaces and return a list with all the words. But this happens: *Main> toWords "How are you?" ["H","o","w","a","r","...

Better data stream reading in Haskell

I am trying to parse an input stream where the first line tells me how many lines of data there are. I'm ending up with the following code, and it works, but I think there is a better way. Is there? main = do numCases <- getLine proc $ read numCases proc :: Integer -> IO () proc numCases | numCases == 0 = return () ...

Why does s ++ t not lead to a stack overflow for large s?

I'm wondering why Prelude> head $ reverse $ [1..10000000] ++ [99] 99 does not lead to a stack overflow error. The ++ in the prelude seems straight forward and non-tail-recursive: (++) :: [a] -> [a] -> [a] (++) [] ys = ys (++) (x:xs) ys = x : xs ++ ys EDIT: Initially, I thought the issue has something to do with the way ++ is de...

Problem understanding treesort in Haskell

I am trying to figure out how exactly does treesort from here work (I understand flatten, insert and foldr). I suppose what's being done in treesort is applying insert for each element on the list thus generating a tree and then flattening it. The only problem I can't overcome here is where the list (that is the argument of the function...

Scala Hoogle equivalent?

Hoogle allows you to search many standard Haskell libraries by either function name, or by approximate type signature. I find it very useful. Is there anything like Hoogle for Scala? Search in ScalaDoc 2 only finds types and packages by name. ...

Linking/Combining Type Classes in Haskell

Say I have two type classes defined as follows that are identical in function but different in names: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a class PhantomMonad p where pbind :: p a -> (a -> p b) -> p b preturn :: a -> p b Is there a way to tie these two classes together so someth...

(type theoretical) How is ([] ==) [] typed in haskell?

It sounds silly, but I can't get it. Why can the expression [] == [] be typed at all? More specifically, which type (in class Eq) is inferred to the type of list elements? In a ghci session, I see the following: Prelude> :t (==[]) (==[]) :: (Eq [a]) => [a] -> Bool But the constraint Eq [a] implies Eq a also, as is shown here: Prelud...

Haskell: OpenGL, how to prevent immediate window closing

Hello. Whenever I close OpenGL window it makes ghci console from which the app was started to immediately disappear. Is there a way to prevent this behaviour? A sample app. Thanks. ...

Haskell "Source reduction"

I'm revising for an upcoming Haskell exam and I don't understand one of the questions on a past paper. Google turns up nothing useful fst(x, y) = x square i = i * i i) Source reduce, using Haskells lazy evaluation, the expression: fst(square(3+4), square 8) ii) Source reduce, using strict evaluation, the same expression iii) State...

Haskell - how to cast types?

I am trying to do following: 10 ** length xs * x but I get: No instance for (Floating Int) arising from a use of `**' ...

Haskell - how can I check if number is Double/Float?

I would like to do smth like: x `mod` 1.0 == 0 // => int but it seems mod works only for int... help! EDIT: I am trying to check if given number is triangle, http://en.wikipedia.org/wiki/Triangle_number so my idea was to check if n1 is Int... (n*(n+1))/2 = s => n1 = (-1 +sqrt(1 + 8s))/2 ...