Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what programming languages support lazy evaluation?

I would like to try out lazy expression evaluation, but I do not want to dive into Haskel right now. Please, can you help to find what other languages have this feature?

like image 255
exebook Avatar asked Dec 19 '13 07:12

exebook


Video Answer


2 Answers

You could simulate it in many languages. this is a generic lazy evaluator for C++, for example. As the article says, its also supported by .NET languages (Thats what the article is trying to emulate).
C++ expression templates are other form of lazy evaluation, for example.

like image 139
Manu343726 Avatar answered Sep 18 '22 20:09

Manu343726


Python's generators are lazy.

Any dataflow language is lazy.

There's also Lazy Racket. Racket is a Scheme derivative.

SWI Prolog has freeze predicate, which suspends evaluation on a logical variable until it is needed, making possible e.g. this:

fibs(X):- X = [0,1|Z], genfibs(X, Z).

genfibs([A|Y], Z):- Y = [B|Z], freeze(Z, (C is A+B, Z = [C|W], genfibs(Y, W))).

Testing:

13 ?- fibs(X), length(A,15), append(A,_,X), writeln(A).
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
freeze(_G2517, (_G2582 is 233+377, _G2517=[_G2582|_G2595], genfibs([377|_G2517], _G2595))).

This is a translation of Haskell's

fibs = x where x = 0 : 1 : (gen x)
               gen (a: y@(b:_)) = (a+b) : (gen y)
like image 32
Will Ness Avatar answered Sep 18 '22 20:09

Will Ness