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?
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.
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)
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.
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]
© 2022 - 2025 — McMap. All rights reserved.