task 5 - embedding and lazy generation
This commit is contained in:
@@ -95,3 +95,10 @@ end
|
||||
{System.show {{Quadratic 3 2 1} 2}}
|
||||
|
||||
|
||||
% task 5
|
||||
fun {LazyNumberGenerator StartValue}
|
||||
StartValue|(fun {$} {LazyNumberGenerator StartValue+1} end)
|
||||
end
|
||||
{System.show 'should be 5:'}
|
||||
{System.show {{{{{{LazyNumberGenerator 0}.2}.2}.2}.2}.2}.1}
|
||||
|
||||
|
||||
@@ -139,3 +139,23 @@ end
|
||||
|
||||
*: Quadratic is of arity 3, but could be thought of as arity 4 if you consider the argument of it's returned function as well. all functions of arity greater than 1 can be thought of as functions returning functions composed with each other: currying. named after Haskell Curry.
|
||||
|
||||
## task 5
|
||||
|
||||
### a)
|
||||
|
||||
this embedded function simulates lazy function evaluation and represents an infinite list.
|
||||
|
||||
```oz
|
||||
fun {LazyNumberGenerator StartValue}
|
||||
StartValue|(fun {$} {LazyNumberGenerator StartValue+1} end)
|
||||
end
|
||||
```
|
||||
|
||||
### b)
|
||||
|
||||
the idea is that the lazy number generator should return both a current number, and a function that knows how to get to the next number, based on that current number. embedding these two into a data structure allows for lazy evaluation, i.e. "getting the thing you want just when you need it, and not a moment earlier".
|
||||
|
||||
this can be useful to save on memory usage by deferring calculations until you need the results.
|
||||
|
||||
i suppose a limitation with my above implementation is that it uses recursion as its driving force, thus it uses quite a lot of stack memory for each new number. this could in theory be mitigated by using tail recursion (as we will see in the next task).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user