haskell

How do Haskell field labels work?

I am looking at this example using getOpts, and one portion of it really baffles me: the syntax of field labels. First, this seems simple enough, creating a data type and declaring the initial values: data Options = Options { optVerbose :: Bool , optInput :: IO String , optOutput...

How can iterative deepening search implemented efficient in haskell?

I have an optimization problem I want to solve. You have some kind of data-structure: data Foo = { fooA :: Int , fooB :: Int , fooC :: Int , fooD :: Int , fooE :: Int } and a rating function: rateFoo :: myFoo -> Int I have to optimize the result of rateFoo by changing the values in the struct. In this specific case, I dec...

What's the alternative to exceptions in a deep Haskell recursion?

I'm trying to learn Haskell by writing little programs... so I'm currently writing a lexer/parser for simple expressions. (Yes I could use Alex/Happy... but I want to learn the core language first). My parser is essentially a set of recursive functions that build a Tree. In the case of syntax errors, I'd normally throw an exception (i.e...

Graceful exit for multithreaded haskell

This is entirely theoretical at this point, but I've been trying to wrap my head around this problem. Let's take a client for an example. There are forkIOd threads for every connection, and one of them wants to quit the entire program (ie. /exit). How would this information be propagated to other threads? This is not a condition, but ...

Is there a way to limit the memory, ghci can have?

I'm used to debug my code using ghci. Often, something like this happens (not so obvious, of course): ghci> let f@(_:x) = 0:1:zipWith(+)f x ghci> length f Then, nothing happens for some time, and if I don't react fast enough, ghci has eaten maybe 2 GB of RAM, causing my system to freeze. If it's too late, the only way to solve this pr...

Why does <$> act only on the second member of a pair?

Take a quick peek at the following interactive session in GHCi: Prelude> import Control.Applicative Prelude Control.Applicative> (+1) <$> [1,2] [2,3] Prelude Control.Applicative> (+1) <$> (1,2) (1,3) I guess there is a good reason for the behavior of <$> regarding pairs, but I wasn't able to find one so far, so: why is <$> (or `fma...

Haskell Random from Datatype

Hi all, Im pretty new to Haskell. I have a datatype: data Sentence= Prop Int | No Sentence | And [Sentence] | Or [Sentence] deriving Eq I already wrote a Show instance for it However, whether it makes sense or not, I would like to be able to generate a random Sentence. How can i accomplish this...

How would you (re)implement iterate in Haskell ?

iterate :: (a -> a) -> a -> [a] (As you probably know) iterate is a function that takes a function and starting value. Then it applies the function to the starting value, then it applies the same function to the last result, and so on. Prelude> take 5 $ iterate (^2) 2 [2,4,16,256,65536] Prelude> The result is an infinite list. (th...

What language to learn after Haskell?

As my first programming language, I decided to learn Haskell. I'm an analytic philosophy major, and Haskell allowed me to quickly and correctly create programs of interest, for instance, transducers for natural language parsing, theorem provers, and interpreters. Although I've only been programming for two and a half months, I found Hask...

What do we call this (new?) higher-order function?

I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will be explained afterward. Python: >>> def pleat(f, l): return map(lambda t: f(*t), zip(l, l[1:])) >>> pleat(operator.add, [0, 1, 2, 3]) [1, 3, 5] Haskell...

Passing list elements as parameters to curried function

Still a Haskell newbie here. I know just enough to get myself into trouble with wrong assumptions. If I have the following function... quadsum w x y z = w+x+y+z I want a function that can take a list, use each element as a parameter in a specified function like quadsum, and return a curried function for later use. I've been trying ...

Good choice for functional structure for insert and search-

I need a data-structure, which supports the following operations both memory and time-efficient, it can be assumed, that the value has an ordering. Add a value to the structure Find out, whether a value is in the structure Plus, the structure has to be immutable, because I want to use Haskell. If I would not assume immutability, pro...

overview, but very over in functional programming

What does a very general function look like in functional programming? Somebody said "we don't have objects, but we have higher order functions". Do higher order functions replace objects? While programming object-oriented apps, I try to go from a more general to a more detailed idea, lots of times. If I try to do that in functional pr...

Will I develop good/bad habits because of lazy evaluation?

Hi all, I'm looking to learn functional programming with either Haskell or F#. Are there any programming habits (good or bad) that could form as a result Haskell's lazy evaluation? I like the idea of Haskell's functional programming purity for the purposes of understanding functional programming. I'm just a bit worried about two things...

Print strings successively

I have two functions: lowerString :: [Char] -> [Char] lowerString = filter (/='_') upperString :: [Char] -> [Char] upperString [] = [] upperString (x:xs) | x == '_' = x : upperString (tail xs) | otherwise = ' ' : upperString(xs) If I apply them on "_A_B_CDEF": upperString "_A_B_CDEF" would return ___ lowerString "_A_B_CDEF...

How to make Haskell's Network.Browser do gzip compression?

Haskell's Network.Browser module seems to not do any compression. How can I configure it so that it does gzip compression assuming that the server supports it (or fall back to no compression if it doesn't) ? ...

What makes Haskell's type system more "powerful" than other languages' type systems?

Reading Disadvantages of Scala type system versus Haskell?, I have to ask: what is it, specifically, that makes Haskell's type system more powerful than other languages' type systems (C, C++, Java). Apparently, even Scala can't perform some of the same powers as Haskell's type system. What is it, specifically, that makes Haskell's type s...

What makes Iteratees worth the complexity?

First, I understand the how of iteratees, well enough that I could probably write a simplistic and buggy implementation without referring back to any existing ones. What I'd really like to know is why people seem to find them so fascinating, or under what circumstances their benefits justify their complexity. Comparing them to lazy I/O...

Currying out of order in Haskell

Is there an elegant notation for Currying the arguments of a function out of order in Haskell? For example, if you wish to divide 2 by all elements of a list, you can write map ((/) 2) [1,2,3,4,5] However to divide all elements of a list it seems you need to define an anonymous function map (\x -> x/2) [1,2,3,4,5] Anonymous functi...

Can i deal with many files at the same time in Haskell ?

Hi I have to solve a following problem. there are many files let's say 3 for example, with the following content file1 a1 a2 a3 a4 a5 a6 ...... file2 b1 b2 b3 b4 b5 b6 ...... file3 c1 c2 c3 c4 c5 c6 ...... my program has to take filenames in parameter, read those files and print the following result "a1 b1 c1"...