Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp:lab01 [2022/02/14 15:52] pdmatei |
fp:lab01 [2022/02/24 11:47] (current) pdmatei |
||
---|---|---|---|
Line 13: | Line 13: | ||
* alternatively, you can use [[https://www.sublimetext.com/download | Sublime]], which is a simpler (faster) editor. You can add syntax highlighting plugins for Scala (see: [[https://scalameta.org/metals/docs/editors/sublime/ | scalameta ]]), as well as worksheet/REPL support (see: [[https://packagecontrol.io/packages/SublimeREPL | SublimeREPL]]). | * alternatively, you can use [[https://www.sublimetext.com/download | Sublime]], which is a simpler (faster) editor. You can add syntax highlighting plugins for Scala (see: [[https://scalameta.org/metals/docs/editors/sublime/ | scalameta ]]), as well as worksheet/REPL support (see: [[https://packagecontrol.io/packages/SublimeREPL | SublimeREPL]]). | ||
- | ===== Online exercises ===== | ||
- | Consider the following code: | ||
- | <code scala> | ||
- | def fact(n: Integer): Integer = | ||
- | if (n == 1) 1 | ||
- | else n*fact(n-1) | ||
- | </code> | ||
- | The function ''fact'' is not tail recursive and will quickly fill the call stack. Starting from the code stub below, implement a tail-recursive factorial: | ||
- | <code scala> | ||
- | def fact (n: Integer): Integer = { | ||
- | def aux_fact(n: Integer, acc: Integer): Integer = | ||
- | if (???) acc | ||
- | else ??? | ||
- | aux_fact(n,1) | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | |||
- | ===== Homework - Newton's Square Root method ===== | ||
- | |||
- | * Video (Newton's Square Root solution) | ||
- | |||
- | A very fast way to numerically compute $math[sqrt{2}], is using Newton's Square Root approximation: | ||
- | * Start with $math[x_0 = 1]. | ||
- | * Compute $math[x_n = \displaystyle\frac{1}{2}\(x_n+\frac{1}{x_n}\)] | ||