Skip to content
UoL CS Notes

Lecture 6-1

COMP105 Lectures

List Ranges

A list range lets us write a long list in a compact way:

> [1..10]
> [1,2,3,4,5,6,7,8,9,10]

If the start is bigger than the end then you will get an empty list. This means you can’t count down in a list in this way. Using .. also works with letters to fill in the letters in between.

To give a step size give the first two elements and then a number to stop before or on:

> [3,6..20]
> [3,6,8,12,15,18]

To count backwards use the a step size of -1:

> [5,4..2]
> [5,4,3,2]

Infinite Lists & Functions

  • You can also use this notation to define an infinite list:

      > [1..]
      > [1,2,3,4,5...
    

    This will last forever and you can pass then on to functions. This may be useful if you want some number of elements in an infinite list take 3 [1..].

  • To make a list of an an infinitely long value you can use repeat.

      > repeat 'a'
    
  • To make a list of an infinite cycle you can use the cycle function.

      > cycle "abc"
    

Exercises

  1.  onetox x = [1 .. x]
    
  2.  evensupto x = [0, 2 .. x]
    
  3.  countdown x = [x, x - 1 .. 0]