diff --git a/assignment3/main.oz b/assignment3/main.oz index 20b6e54..de2fd74 100644 --- a/assignment3/main.oz +++ b/assignment3/main.oz @@ -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} + diff --git a/assignment3/report.md b/assignment3/report.md index 29186e7..3d525f5 100644 --- a/assignment3/report.md +++ b/assignment3/report.md @@ -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). +