Re: [stack] An Overlooked Paradigm in Functional Programming
John Nowak <[email protected]> Sat, 30 Jan 2010 18:37:52 -0500
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <[email protected]> |
On Jan 30, 2010, at 6:13 PM, John Nowak wrote:
> Or, alternatively, I'd rather write:
>
> fact = [zero?] [pop 1] [[] [1 -] bi fact *] ifte
For what it's worth, this is essentially identical to the FP version:
fact = zero? -> ~1; * . <id, fact . pred>
The only real difference is that constant (~) is used instead of pop
and that it's made explicit that 'fact' is applied only to the result
of 'pred'. The latter is an advantage, although it's one that goes
away when the stack-based version is written out visually (which is
something I'll be harassing you all with soon enough).
You can do the same thing in Haskell of course:
bi f g x = (f x, g x)
ifte c f g x = if c x then f x else g x
pred = flip (-) 1
fact = ifte ((==) 0) (const 1) (uncurry (*) . bi id (fact . pred))
... but it's not amazingly pretty.
- jn