News for hackers. Stuff that matters.
Re:Seemingly impossible functional programs (2007) by brianberns on Tuesday November 17, 2015 @11:36AM (#10582171)
I don't understand how this program can ever terminate, since there is no base case. Every invocation of find_i starts by calling forsome, and every invocation of forsome calls find_i. How can it possibly end?

Perhaps this relies subtly on Haskell's laziness? If so, I don't think there is a "quick translation" to ML or OCaml.

Re:Seemingly impossible functional programs (2007) by cousin_it on Tuesday November 17, 2015 @11:45AM (#10582249)
Not every invocation of forsome calls find_i. It calls p(find_i(...)), but p can return a value without actually evaluating the inner find_i. So yeah, it relies on laziness, though you can translate it into a strict language by inserting lambdas in a bunch of places.
Re:Seemingly impossible functional programs (2007) by brianberns on Tuesday November 17, 2015 @11:56AM (#10582343)
Thank you. How does Haskell manage to avoid evaluating find_i(...) before invoking p? Since the inner value is the argument to p, it's hard to imagine how p can do anything until it has that value.

EDIT: I think I see the light. The result of find_i(...) is not what's sent to p. Instead, Haskell passes a thunk that might never need to be evaluated.

Re:Seemingly impossible functional programs (2007) by tome on Tuesday November 17, 2015 @12:26PM (#10582592)
Exactly. If I write

    let f x = 1

    print (f (g somethingelse))
then the call to g is never evaluated. f returns 1 immediately.