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
1 answer

Why does OCaml sometimes require eta expansion?

If I have the following OCaml function: let myFun = CCVector.map ((+) 1);; It works fine in Utop, and Merlin doesn't mark it as a compilation error. When I try to compile it, however, I get the following error: Error: The type of this expression, …
LogicChains
  • 3,852
  • 2
  • 13
  • 21
21
votes
3 answers

Enable --hyperlink-source for "cabal install"

The command cabal haddock has very useful --hyperlink-source option. I would like to have the source hyperlinked when building documentation with cabal install. The ticket #517 seems to be just about it:…
Tener
  • 5,191
  • 4
  • 23
  • 42
21
votes
1 answer

Calling Haskell from Java, dynamic linking error Relocation

I'm having troubles with compiling a standalone library for use by Java (with C++ inbetween). There is a program in Haskell exporting one function that processes some text and returns it. The program in Haskell needs some external data (binary…
niczka
  • 353
  • 1
  • 12
21
votes
1 answer

Odd ghc error message, "My brain just exploded"?

When I try to pattern-match a GADT in an proc syntax (with Netwire and Vinyl): sceneRoot = proc inputs -> do let (Identity camera :& Identity children) = inputs returnA -< (<*>) (map (rGet draw) children) . pure I get the…
Dan
  • 10,532
  • 2
  • 42
  • 74
21
votes
6 answers

Haskell read raw keyboard input

I'm writing a terminal-mode program in Haskell. How would I go about reading raw keypress information? In particular, there seems to be something providing line-editing facilities on top of Haskell. If I do getLine, I seem to be able to use the…
MathematicalOrchid
  • 58,942
  • 16
  • 110
  • 206
21
votes
4 answers

What is [] (list constructor) in Haskell?

I'm Having problems understanding functors, specifically what a concrete type is in LYAH. I believe this is because I don't understand what [] really is. fmap :: (a -> b) -> f a -> f b Is [], a type-constructor? Or, is it a value…
user157251
  • 64,489
  • 38
  • 208
  • 350
21
votes
3 answers

What does `~` mean in Haskell?

I'm studying the mtl library and trying to do some MonadTransformers of my own. I was checking the Control.Monad.State.StateT declaration, and across all the code, I see this syntax: execStateT :: (Monad m) => StateT s m a -> s -> m s execStateT m s…
Roman Gonzalez
  • 1,851
  • 13
  • 17
21
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
  • 7,804
  • 1
  • 22
  • 34
21
votes
2 answers

How to write the type declaration of an Haskell function with no arguments?

How to write the type declaration of an haskell function without arguments?
danza
  • 9,361
  • 7
  • 34
  • 41
21
votes
2 answers

Control.Monad.State found in multiple packages haskell

While evaluating the line "import Control.Monad.State" in a Haskell module, GHC gives me the following error: Could not find module `Control.Monad.State': it was found in multiple packages: monads-fd-0.0.0.1 mtl-1.1.0.2 Failed, modules loaded:…
Bill
  • 38,492
  • 24
  • 114
  • 205
21
votes
2 answers

Are Functor instances unique?

I was wondering to what extent Functor instances in Haskell are determined (uniquely) by the functor laws. Since ghc can derive Functor instances for at least "run-of-the-mill" data types, it seems that they must be unique at least in a wide variety…
ErikR
  • 50,049
  • 6
  • 66
  • 121
21
votes
1 answer

Why are cabal reinstalls "always dangerous"?

When reinstalling a package using Cabal, one usually sees this warning: Warning: Note that reinstalls are always dangerous. Continuing anyway... What are some of the reasons behind this message?
pravnar
  • 833
  • 7
  • 11
21
votes
2 answers

Haskell: unnecessary reevaluations of constant expressions

I am going to demonstrate the problem using the following example program {-# LANGUAGE BangPatterns #-} data Point = Point !Double !Double fmod :: Double -> Double -> Double fmod a b | a < 0 = b - fmod (abs a) b | otherwise = if a <…
21
votes
2 answers

Haskell scoping in nested function definitions using where

I have a problem with Haskell's scoping in where definitions. When I have the following function f, where I want to pass the x to the locally defined function f1 without explicitely using it as a parameter, I get an error saying that the type of x…
poke
  • 307,619
  • 61
  • 472
  • 533
21
votes
1 answer

Functors and Applicatives for types of kind (* -> *) -> *

I ran into a situation where my code would benefit from using Functor and Applicative -like abstractions, but for types of kind (* -> *) -> *. Defining a higher-kinded functor can be done with RankNTypes like this class HFunctor f where hfmap ::…
shang
  • 23,876
  • 3
  • 55
  • 83
1 2 3
99
100