# AQA Quick Reference The following is taken from the AQA syllabus: Be familiar with representing a list as a concatenation of a head and a tail. Know that the head is an element of a list and the tail is a list. Know that a list can be empty. Describe and apply the following operations: - return head of list - return tail of list - test for empty list - return length of list - construct an empty list - prepend an item to a list - append an item to a list. Have experience writing programs for the list operations mentioned above in a functional programming language or in a language with support for the functional paradigm | Syllabus | Haskell Example | | --- | --- | | Create a list | let xs = \[1,2,3,4,5\] | | return head of list | head xs | | return tail of list | tail xs | | test for empty list | null xs | | return length of list | length xs | | construct an empty list | xs = \[\] | | prepend an item to a list | element : xs | | append an item to a list | xs ++ \[element\] | Going beyond the specification, there are many more list examples here: [https://wiki.haskell.org/How\_to\_work\_on\_lists](https://wiki.haskell.org/How_to_work_on_lists) # Using Lists Haskell lists are homgenous: all the elements must be the same type. - \[1,2,3\] OK - \['a','b','c'\] OK - \[1,'b',2\] X Not allowed A string is simply a list of characters "This" = \['T','h','i','s'\] Haskell lists have a **head** and **tail**, they also have an **init** and a **last** (see below for examples of these). You can prepend an element to a list (add it to the front) using the **:** operator, and concatenate (join) two lists using the **++** operator. Use the : operator for preference in Haskell: it's much faster than ++ Here are some examples to illustrate the above ```haskell Prelude> let xs = [1,2,3,4,5] Prelude> head xs 1 Prelude> tail xs [2,3,4,5] Prelude> init xs [1,2,3,4] Prelude> last xs 5 Prelude> tail xs ++ init xs [2,3,4,5,1,2,3,4] Prelude> head xs ++ last xs <interactive>:8:1: error: ``` # List Exercise 1. Write a list containing the days of the week 2. Find the head of the list 3. Find the tail of the list 4. Find the last element of the list 5. Find the last but one element of the list 6. A new day is invented: Haskellday. Prepend this to the list Remember that a string is a list of characters. Let name = <your name\> 1. Find the first character in your name 2. Find the last character 3. Find the length of your name. 4. Find all the characters but the last. 5. What output will the following produce? ```haskell let ls = [1,2,3,4,5] last ls:init ls ++ tail ls ++ [head ls] ++ [ls!!3] ``` Why is \[head ls\] written as a list, and not as an element, eg head ls? # Next - [[A Brief Diversion - List Comprehensions and Ranges]]