ArticleslgStudy

science

Haskell features

Haskell features is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Haskell features rather than just read about it. In short: This article describes the features in the programming language Haskell. Examples Factorial A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, shown in Haskell: Or in one line: This describes the factorial as a recursive function, with one terminating base case.

Key takeaways

  • Haskell features belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Haskell features to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Haskell features from memory before moving on to harder problems.

Reference excerpt

This article describes the features in the programming language Haskell.

Examples

Factorial A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, shown in Haskell:

Or in one line:

This describes the factorial as a recursive function, with one terminating base case. It is similar to the descriptions of factorials found in mathematics textbooks. Much of Haskell code is similar to standard mathematical notation in facility and syntax. The first line of the factorial function describes the type of this function; while it is optional, it is considered to be good style to include it. It can be read as the function factorial (factorial) has type (::) from integer to integer (Integer -> Integer). That is, it takes an integer as an argument, and returns another integer. The type of a definition is inferred automatically if no type annotation is given. The second line relies on pattern matching, an important feature of Haskell. Note that parameters of a function are not in parentheses but separated by spaces. When the function's argument is 0 (zero) it will return the integer 1 (one). For all other cases the third line is tried. This is the recursion, and executes the function again until the base case is reached. Using the product function from the Prelude, a number of small functions analogous to C's standard library, and using the Haskell syntax for arithmetic sequences, the factorial function can be expressed in Haskell as follows:

Here [1..n] denotes the arithmetic sequence 1, 2, …, n in list form. Using the Prelude function enumFromTo, the expression [1..n] can be written as enumFromTo 1 n, allowing the factorial function to be expressed as

which, using the function composition operator (expressed as a dot in Haskell) to compose the product function with the curried enumeration function can be rewritten in point-free style:

In the Hugs interpreter, one often needs to define the function and use it on the same line separated by a where or let..in. For example, to test the above examples and see the output 120:

or

The GHCi interpreter doesn't have this restriction and function definitions can be entered on one line (with the let syntax without the in part), and referenced later.

More complex examples

Calculator In the Haskell source immediately below, :: can be read as "has type"; a -> b can be read as "is a function from a to b". (Thus the Haskell calc :: String -> [Float] can be read as "calc has type of a function from Strings to lists of Floats".) In the second line calc = ... the equals sign can be read as "can be"; thus multiple lines with calc = ... can be read as multiple possible values for calc, depending on the circumstance detailed in each line. A simple Reverse Polish notation calculator expressed with the higher-order function foldl whose argument f is defined in a where clause using pattern matching and the type class Read:

The empty list is the initial state, and f interprets one word at a time, either as a function name, taking two numbers from the head of the list and pushing the result back in, or parsing the word as a floating-point number and prepending it to the list.

Fibonacci sequence The following definition produces the list of Fibonacci numbers in linear time:

The infinite list is produced by corecursion — the latter values of the list are computed on demand starting from the initial two items 0 and 1. This kind of a definition relies on lazy evaluation, an important feature of Haskell programming. For an example of how the evaluation evolves, the following illustrates the values of fibs and tail fibs after the computation of six items and shows how zipWith (+) has produced four items and proceeds to produce the next item:

fibs = 0 : 1 : 1 : 2 : 3 : 5 : ... + + + + + + tail fibs = 1 : 1 : 2 : 3 : 5 : ... = = = = = = zipWith ... = 1 : 2 : 3 : 5 : 8 : ... fibs = 0 : 1 : 1 : 2 : 3 : 5 : 8 : ...

The same function, written using Glasgow Haskell Compiler's parallel list comprehension syntax (GHC extensions must be enabled using a special command-line flag, here -XParallelListComp, or by starting the source file with {-# LANGUAGE ParallelListComp #-}):

or with regular list comprehensions:

or directly self-referencing:

With stateful generating function:

or with unfoldr:

or scanl:

Using data recursion with Haskell's predefined fixpoint combinator:

Factorial The factorial we saw previously can be written as a sequence of functions:

More examples

Hamming numbers A remarkably concise function that returns the list of Hamming numbers in order:

Like the various fibs solutions displayed above, this uses corecursion to produce a list of numbers on demand, starting from the base case of 1 and building new items based on the preceding part of the list.

Here the function union is used as an operator by enclosing it in back-quotes. Its case clauses define how it merges two ascending lists into one ascending list without duplicate items, representing sets as ordered lists. Its companion function minus implements set difference:

It is possible to generate only the unique multiples, for more efficient operation. Since there are no duplicates, there's no need to remove them:

This uses the more efficient function merge which doesn't concern itself with the duplicates (also used in the following next function, mergesort ):

Each vertical bar ( | ) starts a guard clause with a guard expression before the = sign and the corresponding definition after it, that is evaluated if the guard is true.

Mergesort Here is a bottom-up merge sort, defined using the higher-order function until:

Prime numbers The mathematical definition of primes can be translated pretty much word for word into Haskell:

This finds primes by trial division. Note that it is not optimized for efficiency and has very poor performance. Slightly faster (but still very slow) is this code by David Turner:

Much faster is the optimal trial division algorithm

or an unbounded sieve of Eratosthenes with postponed sieving in stages,

or the combined sieve implementation by Richard Bird,

or an even faster tree-like folding variant with nearly optimal (for a list-based code) time complexity and very low space complexity achieved through telescoping multistage recursive production of primes:

Working on arrays by segments between consecutive squares of primes, it is:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Haskell features

Start with the simplest possible case. Write down what Haskell features claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Haskell features before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Haskell features ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Haskell features

In research
Haskell features appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Haskell features in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Haskell features is common in secondary-school and first-year university syllabi. It links to neighbouring topics Haskell programming language family, so understanding it makes those chapters shorter.
In everyday life
Look for Haskell features outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Haskell features” →

How to study Haskell features in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Haskell features means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Haskell features out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Haskell features in simple terms?

This article describes the features in the programming language Haskell. Examples Factorial A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, shown in Haskell: Or in one line: This describes the factorial as a…

Why does Haskell features matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Haskell features?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Haskell features.

Tags

  • Haskell programming language family

Keep exploring