CISC 260 Lecture Notes - Lecture 3: For Loop, Call Stack
Document Summary
Recursion no loops so we have to use recursion instead induction is important. Ex: factorial fact :: int -> int fact 0 = 1 fact n = n * fact (n-1) - another way: using an accumulator fact2 :: int -> int fact2 n = fact2h n 1 fact2h 0 a = a fact2h n a = fact2h (n-1) (n*a) - yet another way: fact3 :: int -> int fact3. | otherwise = fact3h 1 n 1 fact3h low n acc -- low is sort of the loop counter . - i believe the above fact3h" is an accumulator. Induction and recursion what you need: base case, recursive / inductive step. Ex: ispowerof2 :: int -> bool ispowerof2 n. | otherwise = false: can have a function in a function, if its just local, and you wont need it anywhere else, do this using where.