Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate laziness

I was watching an interview with John Hughes and he was asked if he missed laziness when he switched from Haskell to Erlang. As an answer he said yes and he used tools to "emulate" it. My question is: How does one emulate laziness in a strict language ? It Would be nice to see cools examples in mainstream languages.

like image 818
Hai Avatar asked Jun 14 '11 00:06

Hai


People also ask

What is the main cause of laziness?

Studies of motivation suggest that laziness may be caused by a decreased level of motivation, which in turn can be caused by over-stimulation or excessive impulses or distractions. These increase the release of dopamine, a neurotransmitter responsible for reward and pleasure.


1 Answers

The usual trick is to use a lambda (I guess that would be fun in Erlang).

Here is an example using Ruby:

Monad equivalent in Ruby

The basic idea is pretty simple... You take whatever expression you want to make lazy, wrap it in a zero-argument anonymous function, then evaluate that function when you want to force it.

like image 65
Nemo Avatar answered Oct 28 '22 07:10

Nemo