|
![]() |
||||||||||||||
List TutorialList ProcessingList elements are usually processed using recursion. The terminal condition is the list emptiness. If the list is not empty, usually the first element of the list is processed, and the computation of the rest of the list is performed by recursive application of the same function. In the following example, the function incList(l) computes a list containing elements of list l increased by 1. Function sumList(l) computes the sum of elements of the list of integers. Function mkList(n,m) computes the list of all integers from the range [n,m]. Source code:{# [1,2,3]->incList(), [1,2,3]->sumList(), mkList(1,5), mkList(1,100)->sumList() #} where{ incList(l) = if l->empty() then [] else (l->hd() + 1):l->tl()->incList(); sumList(l) = if empty(l) then 0 else l->hd() + l->tl()->sumList(); mkList(n,m) = if n <= m then n : mkList(n+1,m) else []; } Result:{# [2,3,4], 6, [1,2,3,4,5], 5050 #}
|
|
||||||||||||||
© 2006 Saša Malkov | |||||||||||||||