tupling

tupling

A program transformation where several results are returnedfrom a single traversal of a data structure. E.g.

mean l = sum l / length l

==>

mean l = s/nwhere(s,n) = sumLen l

sumLen [] = (0,0)sumLen (x:xs) = (s+x, n+1)where(s,n) = sumLen xs

In procedural languages this technique is known ashorizontal loop combination because it uses one loop tocalculate several results.

Another form of tupling transformation is used to avoidrepeated evaluation where a function generates severalidentical calls to itself. By analysing the pattern ofrecursion (see descent function) it is possible to arrangefor these identical calls to share results. E.g.

fib 0 = 1fib 1 = 1fib n = fib (n-1) + fib (n-2)

==>

fib n = v where (_,v) = fibt nfibt 0 = (1,1)fibt n = (u+v,u) where (u,v) = fibt (n-1)