levo

Fundamentals of Functional Programming

Lists in functional programming

  • 3 things to know
  • Paper 2

Overview

Recall questions on the head and tail of a list, the empty list, what happens when the head of an empty list is requested, and the seven basic list operations.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Can a list be empty, and how is the empty list written?

Evaluation

Describe and give an example of prepending an item to a list.

Key term

In the Haskell pattern (x:xs), what do x and xs stand for, and why is the naming convention useful?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    How is a list represented as a concatenation of a head and a tail?

    Model answer

    Any non-empty list is treated as a single element stuck on the front of another list. That leading element is the head and everything behind it is the tail, and the tail is itself a list, which is what makes the idea recursive. Haskell writes the split with a colon, so [4, 3, 5] is 4:[3, 5] — the head is 4 and the tail is [3, 5]. Peel again and the tail becomes 3:[5], then 5:[], and finally the empty list, which has no head at all and is where a recursion over a list stops.

  2. 2

    Is the head of a list an element or a list? What about the tail?

    Model answer

    The head is an element of the list — a single item, not a list containing that item. The tail is a list — the remainder of the list once the head has been removed. So for [4, 3, 5] the head is 4 and the tail is [3, 5], not 4 and 3. Confusing the two is the commonest error here, and it matters because an operation expecting a list cannot be given an element, or the other way round.

  3. 3

    For the list [8], what are the head and the tail?

    Model answer

    The head is the element 8 and the tail is the empty list, written []. The tail is still a list even though it has nothing in it, which is consistent with the rule that the tail of any list is a list.

  4. 4

    Can a list be empty, and how is the empty list written?

    Model answer

    Yes, a list can be empty. In Haskell the empty list is written []. It is a perfectly good list; it simply has no elements, so it has no head and no tail. It is what remains after the last element has been removed, and it is the value a construction of a list starts from.

  5. 5

    What happens if you ask for the head of an empty list, and why?

    Model answer

    It is an error, because there is no first element for the operation to return. The same applies to asking for the tail of an empty list. This is why any function that works its way through a list must test for the empty list before taking the head, and it is why the test for an empty list is one of the basic operations.

  6. 6

    List the seven basic list operations.

    Model answer

    Three of them inspect a list without changing it: take its head, take its tail, and report its length. One asks a question about it: is it empty? The remaining three build lists: make an empty one to start from, join an item onto the front, and join an item onto the end. The two joining operations differ in cost as well as position — putting an item on the front is cheap because the existing list becomes the tail untouched, whereas putting one on the end means working the whole way down first.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Functional Programming, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.