recursion Flashcards
what is a recursive function
a function that calls itself
base case
the condition that stops the recursion
recursive case
what the recursion is doing
why is the base case important
it stops the function from continuing infinitely which would be bad as it would eventually cause stack overflow
why do we need recursion in haskell
it is purely functional and enforces immutability therefore we wouldn’t be able to change i in a for loop
head recursion
each recursive call waits for the next call
cant get any actual value until the base case has been reached
what are negatives of head recursion
deep stack usage
large inputs can lead to stack overflow
what makes a function tail recursive
the recursive call is the last operation in the function
how does tail recursion work
each time the function is called the result gets accumulated
what are two benefits of tail recursion
the same stack frame can be used instead of creating new ones
optimises memory usage by avoiding deep stack calls