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

Extremely small or NaN values appear in training neural network

I'm trying to implement a neural network architecture in Haskell, and use it on MNIST. I'm using the hmatrix package for linear algebra. My training framework is built using the pipes package. My code compiles and doesn't crash. But the problem is,…
Charles Langlois
  • 3,968
  • 2
  • 11
  • 20
323
votes
2 answers

Techniques for Tracing Constraints

Here's the scenario: I've written some code with a type signature and GHC complains could not deduce x ~ y for some x and y. You can usually throw GHC a bone and simply add the isomorphism to the function constraints, but this is a bad idea for…
crockeea
  • 21,467
  • 10
  • 44
  • 93
301
votes
8 answers

What is Weak Head Normal Form?

What does Weak Head Normal Form (WHNF) mean? What does Head Normal form (HNF) and Normal Form (NF) mean? Real World Haskell states: The familiar seq function evaluates an expression to what we call head normal form (abbreviated HNF). It stops…
user295190
299
votes
7 answers

Abusing the algebra of algebraic data types - why does this work?

The 'algebraic' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand…
Chris Taylor
  • 44,831
  • 12
  • 101
  • 146
286
votes
4 answers

Why is Haskell (GHC) so darn fast?

Haskell (with the GHC compiler) is a lot faster than you'd expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an…
PyRulez
  • 9,505
  • 9
  • 37
  • 82
282
votes
3 answers

What does the exclamation mark mean in a Haskell declaration?

I came across the following definition as I try to learn Haskell using a real project to drive it. I don't understand what the exclamation mark in front of each argument means and my books didn't seem to mention it. data MidiMessage = MidiMessage…
David
  • 5,438
  • 4
  • 28
  • 39
268
votes
1 answer

How fundamentally different are push-pull and arrowized FRP?

I want to study FRP in Haskell, but it's a bit difficult to decide on a library to use. Many seem to be dead attempts, some seem to have been resurrected (such as recent activity on Yampa). From what I read, it seems that there are two "kinds" of…
Guillaume Ponce
  • 2,483
  • 2
  • 10
  • 9
261
votes
6 answers

What's so bad about Template Haskell?

It seems that Template Haskell is often viewed by the Haskell community as an unfortunate convenience. It's hard to put into words exactly what I have observed in this regard, but consider these few examples Template Haskell listed under "The Ugly…
Dan Burton
  • 51,332
  • 25
  • 109
  • 190
259
votes
1 answer

Getting associated type synonyms with template Haskell

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting…
247
votes
1 answer

Haskell: Lists, Arrays, Vectors, Sequences

I'm learning Haskell and read a couple of articles regarding performance differences of Haskell lists and (insert your language)'s arrays. Being a learner I obviously just use lists without even thinking about performance difference. I recently…
r.sendecky
  • 8,183
  • 7
  • 30
  • 57
233
votes
4 answers

Comparing Haskell's Snap and Yesod web frameworks

The two Haskell web frameworks in the news recently are Yesod (at 0.8) and Snap (at 0.4). It's quite obvious that Yesod currently supports a lot more features than Snap. However, I can't stand the syntax Yesod uses for its HTML, CSS and Javascript.…
Muchin
  • 4,717
  • 4
  • 21
  • 25
222
votes
7 answers

What is the Haskell response to Node.js?

I believe the Erlang community is not envious of Node.js as it does non-blocking I/O natively and has ways to scale deployments easily to more than one processor (something not even built-in in Node.js). More details at…
gawi
  • 13,050
  • 7
  • 38
  • 74
219
votes
6 answers

Running a Haskell program on the Android OS

Forenote: This is an extension of the thread started on /r/haskell Lets start with the facts: Android is one awesome Operating System Haskell is the best programming language on the planet Therefore, clearly, combining them would make Android…
Robert Massaioli
  • 12,801
  • 6
  • 48
  • 71
216
votes
5 answers

Good examples of Not a Functor/Functor/Applicative/Monad?

While explaining to someone what a type class X is I struggle to find good examples of data structures which are exactly X. So, I request examples for: A type constructor which is not a Functor. A type constructor which is a Functor, but not…
Rotsor
  • 13,009
  • 5
  • 40
  • 57
213
votes
3 answers

Haskell: Converting Int to String

I know you can convert a String to an number with read: Prelude> read "3" :: Int 3 Prelude> read "3" :: Double 3.0 But how do you grab the String representation of an Int value?
Squirrelsama
  • 5,000
  • 4
  • 25
  • 38