Questions tagged [haskell]

Haskell is a purely functional programming language featuring strong static typing, lazy evaluation, extensive parallelism and concurrency support, and unique abstraction capabilities.

Haskell is a purely functional programming language. An open-source product of more than twenty years of research, it allows rapid development of robust, concise, correct software. With strong support for integration with other languages, built-in concurrency and parallelism, debuggers, profilers, rich libraries, and an active community, Haskell makes it easier to produce flexible, maintainable, high-quality software.

Checklist

To avoid answering the same questions over and over again, please check the list of interesting questions and this checklist:

Mixing tabs and spaces

A lot of errors mentioning some sort of parse error on input... are caused by the user mixing spaces and tabs in the indentation, so please check that is not the case. Also, you should probably set up your editor to convert tabs into spaces.

Type-checking problems at compilation

While Haskell's type system is very powerful, its behavior can sometimes be confusing. The more explicit type signatures you add, the less possibilities there are for the compiler to deduce absurd relations. Type signatures also make questions more understandable, because it's obvious what you want a function to do.

Performance issues

In case of performance issues, please make sure that you compile your code with optimizations enabled. Passing -O2 to the compiler makes many performance issues go away. The GHC interpreter is noticeably slower than running the binary outputted from GHC's compiler.

It can also be helpful to compile with -Wall in order to observe (among other useful things) places where numeric types are being defaulted to the arbitrary precision Integer type, so that you can consider whether you get away with using the more efficient fixed precision Int type.

It is also important to know which version of the compiler and libraries you use. Providing those pieces of information may significantly decrease the time it takes the community to answer your question.

Many beginner performance issues stem from a misunderstanding of what lists are, and how they can be used effectively. In particular, they are not arrays, but have the same structure as linked lists in imperative languages:

data List a = Cons a (List a)
            | Empty

Understanding that [a] is a nested (recursive) algebraic data type is important for supporting an intuition for the efficiency of operations like (:) vs (++), (!!), length, etc.

Idiomatic and efficient use of lists involve composing functions like zip, map, filter, foldr, take, etc. many of which allow the intermediate lists to be eliminated entirely.

The Prelude's String type is implemented in terms of lists:

type String = [Char]

This is a very convenient representation but is quite memory inefficient, and unsuitable for text processing where performance is a concern. Instead the text library should be used.

The bytestring is a similarly fast and efficient high-level interface around a string (or stream) of bytes. The Data.ByteString.Char8 module can be used for an efficient representation of a small subset of unicode, for instance where only ASCII text is expected.

"What is function foo / operator #$*?"

Haskell's syntax is very simple, in the sense that everything (apart from the few keywords) is just a library function1, including all infix operators. Those functions can easily be searched for,

  • Hayoo searches identifiers and signatures throughout the entire Hackage database.
  • Hoogle also searches for identifiers and signatures, but only works when the function comes from a known package.

Please try these engines first before asking a question like this or this or this.

"What library should I use for <thing>?"

These types of questions are generally off-topic, but here are some useful resources:

Getting started

  1. Download the Haskell Platform for your platform. This includes the state-of-the-art Glasgow Haskell Compiler (GHC) and common developer tools and libraries.
  2. Check out these Stack Overflow questions with links to popular websites and tutorials:
  1. Have fun, and ask questions!

Interesting questions/answers

Notable Haskell Implementations

  • GHC: Glasgow Haskell Compiler, an optimizing compiler for Haskell.
  • UHC: a Haskell implementation from Utrecht University.
  • Hugs: a once-popular Haskell interpreter that is no longer maintained. Most people now use GHCi for interactive development.

Community

Other places for discussing Haskell, beyond the question & answer format of Stack Overflow:

Free Haskell Programming Books

Haskell Programming Books

Haskell papers

The following list is courtesy of Gangadhar:

  1. Why Functional Programming Matters
  2. History of Haskell
  3. Watch videos on Channel9 related to FP - though not always academic
  4. Follow LtU
  5. Did not read the FP journal - but it can have information of use to you
  6. Monad Reader
  7. The paper on composing contracts by Simon Peyton Jones is a good read, as is pretty much everything from his papers and those of Philip Wadler.
  8. Typing Haskell in Haskell – implementation of basic Haskell typesystem in Haskell by Simon Peyton Jones

More information


1Technically, it would be more correct to say everything is a library value, because something like a numerical constant or an IO action is not actually a function.

50851 questions
22
votes
2 answers

Haskell Pattern Matching on the Empty Set

I'm changing some Haskell code from using lists to sets. I understand everything required, I think, but I'm not sure how to pattern match on sets. Lists have this nice literal syntax that seems hard to emulate with the Set constructor. For example,…
Derek Thurn
  • 14,953
  • 9
  • 42
  • 64
22
votes
5 answers

Should I avoid constructing in Haskell?

While reading a snipped from Haskell for Great Good I found the following situation: treeInsert :: (Ord a) => a -> Tree a -> Tree a treeInsert x EmptyTree = singleton x treeInsert x (Node a left right) | x == a = Node x left right | x <…
FtheBuilder
  • 1,410
  • 12
  • 19
22
votes
1 answer

What is the Haskell standard library?

Can the GHC-specific libraries be called the standard library? Or do only those in the Haskell 2010 report count? Many of the GHC libraries can be implemented by the functions in the Haskell report, possibly combined with C bindings. But the others…
user3810155
22
votes
1 answer

Cabal install gtk failing

I'm trying to install gtk via cabal, however, I'm getting the following type errors when building it [ 22 of 209] Compiling Graphics.UI.Gtk.Embedding.Plug ( dist/build/Graphics/UI/Gtk/Embedding/Plug.hs, dist/build/Graphics/UI/Gtk/Embedding/Plug.o…
Matt
  • 4,029
  • 3
  • 20
  • 37
22
votes
3 answers

Is Curry-Howard correspondent of double negation ((a->r)->r) or ((a->⊥)->⊥)?

Which is the Curry-Howard correspondent of double negation of a; (a -> r) -> r or (a -> ⊥) -> ⊥, or both? Both types can be encoded in Haskell as follows, where ⊥ is encoded as forall b. b. p1 :: forall r. ((a -> r) -> r) p2 :: (a -> (forall b. b))…
nushio
  • 753
  • 3
  • 12
22
votes
2 answers

How to call Haskell from Javascript with GHCJS

I've been playing about with GHCJS. The FFI can be used to call javascript from Haskell but I can't figure out how do go the other way round. Say I had a super useful utility function I wrote in Haskell: sayHello :: String -> IO () sayHello name =…
Gareth Charnock
  • 1,166
  • 7
  • 17
22
votes
2 answers

Preferred way to do locales in the Haskell Platform

The Haskell platform includes two obsolete libraries, old-time and old-locale. For old-time, it also includes the preferred alternative (namely time), but I can't figure out what the recommended alternative for old-locale is. Is this simply a…
22
votes
3 answers

How to pick a random list element in a pure function?

I want to make a Haskell function that can pick out a random number from a given list. My type signature is: randomPick :: [a] -> a What should I do?
user345662
  • 221
  • 2
  • 3
22
votes
1 answer

Infinite loop in haskell?

I thought this would produce a factorial function... (within ghci) Prelude> let ft 0 = 1 Prelude> let ft n = n * ft (n - 1) Prelude> ft 5 (hangs indefinitely, until ^C).
Mike
  • 229
  • 1
  • 3
22
votes
1 answer

QuickCheck Gen is not a monad

I have occasionally seen people say that the Gen type in QuickCheck does not obey the monad laws, though I have not seen much of an explanation to go with it. Now, QuickCheck 2.7's Test.QuickCheck.Gen.Unsafe module says Gen is only "morally" a…
massysett
  • 1,100
  • 6
  • 13
22
votes
3 answers

Why constraints on data are a bad thing?

I know this question has been asked and answered lots of times but I still don't really understand why putting constraints on a data type is a bad thing. For example, let's take Data.Map k a. All of the useful functions involving a Map need an Ord k…
mb14
  • 22,276
  • 7
  • 60
  • 102
22
votes
2 answers

Precise flow control in Haskell

The Idea Hello! I'm trying to implement in Haskell an image processing library based on dataflow ideology. I've got a problem connected to how I want to handle the flow of control. The main idea is to introduce a time. The time is a Float, which…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
22
votes
7 answers

Implement zip using foldr

I'm currently on chapter 4 of Real World Haskell, and I'm trying to wrap my head around implementing foldl in terms of foldr. (Here's their code:) myFoldl :: (a -> b -> a) -> a -> [b] -> a myFoldl f z xs = foldr step id xs z where step x g a =…
itsadok
  • 28,822
  • 30
  • 126
  • 171
22
votes
4 answers

Haskell: Detect current OS

I'm currently writing a Haskell package. I want it to behave differently on Windows and Unix. What is the most "correct" way to detect what OS my code is being compiled on? I did try using CPP and #ifdef _WIN32, but that doesn't appear to work at…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
22
votes
7 answers

Haskell caching results of a function

I have a function that takes a parameter and produces a result. Unfortunately, it takes quite long for the function to produce the result. The function is being called quite often with the same input, that's why it would be convenient if I could…
ondra
  • 9,122
  • 1
  • 25
  • 34
1 2 3
99
100