[erlang-questions] currying and lazy evaluation questions (just looking for some hacks for purposes of porting)

Richard A. O'Keefe ok@REDACTED
Mon Sep 22 06:32:14 CEST 2008


On 21 Sep 2008, at 2:32 am, Eli Liang wrote:
> I read a note on the Internet that said "You can do lazy evaluation  
> in Erlang (observe Erlang QuickCheck), but you have to be explicit  
> about it."
>
> Can some one explain to me how to "explicitly" use lazy evaluation  
> in Erlang, in the general?

Use closures.  "Structure and Interpretation of Computer  
Programs" (which is available on the web
these days) explains the idea.  Basically, instead of some value, you  
return a function to compute it.
E.g.

	lazy_append(L1, L2) ->
	    case L1()
	      of [] -> L2
	       ; [H|T] -> fun () -> [H | lazy_append(T, L2)] end
	    end.

This is the "don't evaluate YET" part of lazy evaluation.
Lazy evaluate also adds "don't evaluate AGAIN", which is fairly
straightforward to do in Scheme and SML (thanks to mutable boxes),
but sufficiently tricky in Erlang that I've never bothered.

>
> I know that Erlang doesn't support implicit currying. If I wanted to  
> "explicitly" curry, is there some way to do that? Or at least  
> emulate currying?

curry(F) when is_function(F, 2) ->
     fun (X) -> fun (Y) -> F(X, Y) end end.

> Since neither of these are naturally supported in the language, I  
> suppose I'm just looking for some hacks which will let me port a few  
> haskell programs to Erlang without massive rewrite.

As a regular Haskeller, I'd say they'd have to be fairly trivial  
Haskell.




More information about the erlang-questions mailing list