Questions tagged [haskell]

Haskell is a 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:
  3. 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.

46712 questions
21
votes
2 answers

Relation between `DList` and `[]` with Codensity

I've been experimenting with Codensity lately which is supposed to relate DList with [] among other things. Anyway, I've never found code that states this relation. After some experiments I ended up with this: {-# LANGUAGE RankNTypes #-} module…
raichoo
  • 2,527
  • 20
  • 28
21
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
  • 3,689
  • 2
  • 16
  • 36
21
votes
1 answer

How to combine lenses (not compose)

In haskell without lenses I can do things like : data Item = Item { quantity :: Double, price ::Double } cost :: Item -> Double cost = (*) <$> quantity <*> price If I use lenses instead how can I do the equivalent ? The best I can do is cost = to…
mb14
  • 20,914
  • 4
  • 54
  • 97
21
votes
2 answers

Are there contravariant monads?

Functors can be covariant and contravariant. Can this covariant/contravariant duality also be applied to monads? Something like: class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b class ContraMonad m where return ::…
ZhekaKozlov
  • 29,055
  • 16
  • 100
  • 138
21
votes
2 answers

cabal: how to automatically update the build-depends field in the .cabal file?

Is there a way to automatically update the build-depends field in the .cabal-file? For example, if we start with the following .cabal file: name: HUnit version: 1.1.1 synopsis: A unit testing framework for Haskell homepage: …
21
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
  • 713
  • 3
  • 12
21
votes
2 answers

What is the derivation that shows Haskell's \x -> (x, x) equivalent to join (,)?

According to pointfree: \x -> (x, x) is equivalent to: join (,) What is the derivation that shows this?
Ana
  • 11,726
  • 5
  • 49
  • 101
21
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…
21
votes
1 answer

Interpreter auto-selection via Free Monad and Coproduct

I am playing with app architecture and free monads in haskell. I've got it down, except for how to lift my "instruction" into the correct slot of my coproduct without explicitly giving the full Left/Right path. Here's the haskell example I've been…
Brian
  • 682
  • 3
  • 14
21
votes
2 answers

Why is this eta expansion necessary?

Can someone help me understand this/point me to some reading materials? The following works fine: type F a b = Functor f => f a -> f b fComp :: F b c -> F a b -> F a c fComp f f' = f . f' But if I write fComp = (.) instead, the type checker…
Alan O'Donnell
  • 1,246
  • 9
  • 16
21
votes
1 answer

Debugging IO in a package module inside GHCi

I'm doing low-level IO (for library bindings) in Haskell and am experiencing a segfault. I would like to use GHCi's :break to figure out what's going on, but here's what happens: > import SDL > :break SDL.setPaletteColors cannot set breakpoint on…
21
votes
1 answer

Warning that pattern guard is non-exhaustive even though it is

I'm observing an interesting behavior when using pattern matching with pattern guards and all warnings turned on {-# OPTIONS_GHC -Wall #-} module Mood where data Mood = Happy | Indifferent | Sad deriving Show flipMood…
raichoo
  • 2,527
  • 20
  • 28
21
votes
5 answers

Reverse a list in haskell

I am trying to reverse a list. Following is my code: reverseList :: [Int] -> [Int] reverseList [] = [] reverseList (x:xs) = x:reverseList xs What ends up happening is I end up getting the list back in same order. I even have a solution for how to…
user1010101
  • 1,976
  • 7
  • 36
  • 70
21
votes
2 answers

Using a Lens to read multiple fields

Given the types data Prisoner = P { _name :: String , _rank :: Int , _cereal :: Cereal } data Cereal = C { _number :: Int , _percentDailyValue :: Map String Float …
rampion
  • 82,104
  • 41
  • 185
  • 301
21
votes
2 answers

what are the differences between Haskell and PureScript?

PureScript looks very similar to Haskell. It seems to me that large parts, mostly the pure parts, of a PureScript program should be compilable as well by a Haskell compiler. Is that right? This leads to a related questions: Would it be possible to…
Thomas Koch
  • 2,723
  • 2
  • 26
  • 36
1 2 3
99
100