CS 201: Collatz

See collatz.rkt

The Collatz Function

Let's try it out.

Recursion

We can have collatz call its own result.

Having a function call itself is known as recursion. We can generate the series based on Collatz using recursion.

The function c-series calls itself until the result converges to 1. The function uses let to introduce a local variable next

We can generate the series based on Collatz using recursion as above, but without the temporary variable next

Trace

We can trace the execution of the recursive functions using the trace function, which prints out each call to the given function.

When writing recursive code, you will often find it useful to use trace to see what is going on underneath the hood.

We define a c-series3 which does not print out results.

Not really too useful, unless we trace it.

End of Collatz notebook.