2 ms·
That's only a problem if your language doesn't allow separating computation from executing effects. With monads it's pretty straightforward: def doD(b: Int
by lmm 3y ago
That's only a problem if your language doesn't allow separating computation from executing effects. With monads it's pretty straightforward:
def doD(b: Int, c: Int) = puts(b / c)
def doCD(b: Int) = getInt >>= {c => doD(b, c) >> puts(c)}
def doBCD(a: Int) = getInt >>= {tb => val b = tb+a; doCD(b) >> puts(b)}
getInt >>= {a => doBCD(a) >> puts(a)}
- lex-lightning 3y agoIf I wanted to deeply pedantic, I think the blocks associated with those monady bits keep the parameters in scope longer than needed. But I never got into Haskell so I don’t really know.
- lmm 3y ago> If I wanted to deeply pedantic, I think the blocks associated with those monady bits keep the parameters in scope longer than needed. I don't think so? If you write it as a "flat" block using do notation then anything from earlier in the block is accessible later in the block. But local functions are real functions with lexical scope, they don't get hoisted up to the containing scope or anything. (And FWIW that's Scala-style syntax, although the ideas are very Haskell-like)
- lex-lightning 3y agoDope. Thanks. Have you written much Prolog? I was thinking about in Prolog terms and I think it’s the hardest paradigm to avoid the scoping problem in
- lmm 3y agoI've never done any Prolog.