CISC 260 Lecture Notes - Lecture 3: For Loop, Call Stack

51 views2 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents