Recursion examples in pseudo-code -- T Author: s haynes Dates: Original 2/6/2017 Correction 10/2/2017 (ackermann function code) head (list) returns first element of list tail(list) returns list after first element is removed cons (element, list) returns list with element inserted to front append (list1, list2) returns a list with elements of list2 appended to elements of list1 empty(list) returns true if list is empty, else returns false Example list = [1, 2, 3] head(list) --> 1 tail(list) --> [2, 3] cons(4, list) --> [4, 1, 2, 3] list = [1] head(list) --> 1 tail(list --> [] cons(4, list) --> [4, 1] list1 = [1, 2, 3] list2 = [a, b] append(list1, list2) --> [1, 2, 3, a, b] For notational convenience: [ a ] is a list containing the single element a /************************************************************************/ maximum( list ): Return the maximum element in a list of numbers maximum (list) { if ( empty(list) ) return [] if ( list has one element ) return the element // list has two or more elements return max2 ( head(list), maximum( tail(list) ) ); } number max2 (a, b) { if (a <= b) return b; return a; } /**********************************************************************/ length (list): return length of the list length (list) { if (list == []) return 0; return 1 + length( tail(list) ); } /*********************************************************************/ replicate( n, a): Return a list of a replicated n times replicate( n, a) { if ( n <= 0 ) return []; return cons (a, replicate ( n-1, a ) ); } /**********************************************************************/ reverse (list) returns list in reversed order reverse (list) { if ( list == [] ) return [] if ( list == [a] ) return [a]; // list has two or more elements return append ( reverse (tail (list) ), head (list) ); } /**********************************************************************/ get (n, list): returns the nth element of list get (n, list) { if (list == [] ) error ("can't get on empty list"); if (n == 0) return (head(list)); return get (n-1, tail(list) ); } /*********************************************************************/ snip( a, list): return list that is the original list with the first elements removed until the resulting list starts with a E.g.. snip(3, [1, 2, 3, 4, 5, 3, 2] ) --> [3, 4, 5, 3, 2] snip (a, list) { if (list == [] ) return [] if (a == head (list) ) return ( list ) return snip( a, tail(list) ) } /**********************************************************************/ fib(n) : return the nth Fibonacci number, integer n >= 0 fib (n) { if ( n == 0 ) return 0; if ( n == 1 ) return 1; return fib (n-1) + fib (n-2) } /**********************************************************************/ ack( m, n ) return the result of the Ackermann function on inputs m and n; integer m, n >= 0 ack (m, n) { if ( m == 0 ) return n + 1; if ( n == 0 ) return ack( m - 1, 1 ); return ack ( m - 1 , ack (m, n - 1 ) ); } /**********************************************************************/ infix (root) does an infix traversal of binary tree of nodes visit(node) does whatever operation on node.data is desired infix (root) { if ( root == null ) return; infix ( root.left); visit ( root.data); infix (root.right); } /***********************************************************************/ postfix (root) does a postfix traversal of a binary tree. postfix(root) { if (root == null) return; postfix(root.left); postfix(root.right); visit(root.data); }