what programming languages support lazy evaluation?
Asked Answered
H

4

9

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?

Hamiltonian answered 19/12, 2013 at 7:26 Comment(3)
Which paradigm do you want? Functional or not?Wolffish
SliceSort, functional probably, but I am not sure. I prefer learning things from trying real programming language rather than reading articles and theory papers.Hamiltonian
cutest avatar ever. :)Profane
F
5

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.

Freehand answered 19/12, 2013 at 7:29 Comment(0)
P
5

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)
Profane answered 7/1, 2014 at 9:41 Comment(0)
D
2

Please, don't forget Clean !( https://wiki.clean.cs.ru.nl/Clean ). Essentially similar to Haskell (laziness, type inference, a good part of syntax). Plenty of interchangeability, see e.g., https://www.mbsd.cs.ru.nl/publications/papers/2010/groj10-Haskell_front_end_Clean.pdf

The type system is a bit different, perhaps less sophisticated than in Haskell (the type class system is "flatter", no monads [unless user implemented], but "unique types" replace monads in a readable and efficient way.)

This is a nice, useful language which should be better known, and taught. In my opinion: for numerical work it is easier to handle complex algorithms than in Haskell. And it is very efficient.

Delphinium answered 2/2, 2024 at 11:38 Comment(0)
A
1

Lazy evaluation is useful in two cases: (1) You work with infinite lists, (2) You need answer before question.

This is normal in functional programming (f.e.: Gofer/Miranda/Haskell).

An infinite list can be seen as a stream (as keyboard input, random numbers, music from radio, ...)

Examples: (a) Natural numbers: [1..] (b) Fibonacci numbers: fibs = 0:1: zipwith (+) fibs (tl fibs)

If you want to get 20 random numbers, you will express this as: take 20 randoms

In "normal" (imperative) coding style, user dialogs are of form: ...->(input->output)->(input->output)->...

In functional programming (FP), the view is changed to: ...->input)->(output->input)->(output->...

This is only possible using lazy evaluation.

If you are running Linux, you can compile and run Miranda from miranda.org.uk. Miranda was commercial. It is much easier than Haskell.

foldr1 (+) [1..36]

Adlay answered 16/1, 2022 at 17:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.