diff options
author | bnewbold <bnewbold@ziggy.(none)> | 2010-01-24 05:56:09 -0500 |
---|---|---|
committer | bnewbold <bnewbold@ziggy.(none)> | 2010-01-24 05:56:09 -0500 |
commit | 354e98325fe94f4834d360086a67d0032b83fa20 (patch) | |
tree | 9015fe2528a529ea5c2a5caf21f2d2ea8146beec /software/functional programming.page | |
parent | b234b981acb135ed00a7ecf444dde6fe33d9f0f3 (diff) | |
download | knowledge-354e98325fe94f4834d360086a67d0032b83fa20.tar.gz knowledge-354e98325fe94f4834d360086a67d0032b83fa20.zip |
syntax fixes
Diffstat (limited to 'software/functional programming.page')
-rw-r--r-- | software/functional programming.page | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/software/functional programming.page b/software/functional programming.page index 593cf41..7fa1358 100644 --- a/software/functional programming.page +++ b/software/functional programming.page @@ -24,31 +24,31 @@ or variables in layer after layer of functions and just holding on to the outermost layer. For instance, the typical way to write a ``length`` function in python would be:: ->>> def how-long(x): ->>> l = 0 ->>> while x.has_next() ->>> l = l+1; ->>> x.pop() ->>> return l + def how-long(x): + l = 0 + while x.has_next() + l = l+1; + x.pop() + return l Using recursion, we could do:: ->>> def how-long-recurse(x): ->>> if x.has_next() ->>> x.pop() ->>> return how-long-recurse(x) + 1 ->>> else ->>> return 0 + def how-long-recurse(x): + if x.has_next() + x.pop() + return how-long-recurse(x) + 1 + else + return 0 Using the collector paradigm, we could do:: ->>> def add1(x): return a+1; ->>> def how-long-col(x, col): ->>> if x.has_next() ->>> return col(0) ->>> else ->>> x.pop() ->>> return how-long-col(x, lambda a: col(add1(a))) + def add1(x): return a+1; + def how-long-col(x, col): + if x.has_next() + return col(0) + else + x.pop() + return how-long-col(x, lambda a: col(add1(a))) The first two ways, the plus one operation is actually executed at any given time, while with the collector implementation we're really creating a |