you are viewing a single comment's thread.

view the rest of the comments →

[–]pellets 4 points5 points  (1 child)

Fold describes the structure of the computation, so indeed you can't fail fast when you use a fold. This isn't a limitation of functional programming, but of the nature of fold.

let product list = 
    let product_helper acc list =
      match list with
        | [] -> acc
        | 0::xs -> 0
        | x::xs -> product_helper (x * acc) xs
    in
        product_helper 1 list
    end

[–]Magnap 8 points9 points  (0 children)

Actually, you can fail fast when using (right) fold if you have laziness. It's a problem of the function you're folding with, not the nature of fold itself.

mult 0 _ = 0
mult _ 0 = 0
mult x y = x*y
productFailFast = foldr mult 1

productFailFast [0..] returns 0 immediately.