48 Comments
- alwaysthere, on 10/12/2007, -2/+23I always thought lazy programming was " I will add comments to this code later. "
- zephc, on 10/12/2007, -1/+14I've long thought it would be called Procrastivaulation
It's just putting things off til it really really has to do it, like paying a parking ticket, going to the doctor, or feeding a child.
You heard it here first, folks. - tekwiz2k4, on 10/12/2007, -11/+23I HATE Scheme. too many parentheses!!
- Wisgary, on 10/12/2007, -1/+11Anything can be done in perl with a one liner, so what you're saying is redundant. In fact, I can mash a bunch of crap on my keyboard into a perl interpreter right now and it will probably create something useful.
- m1th, on 10/12/2007, -2/+9Haskell has the whole lazy process business built in. It's called "lazy evaluation".
- pauldonnelly, on 10/12/2007, -0/+6I hate English; there's a space between every damn word!
If you're looking at the parentheses, you haven't got the hang of it yet. - anitab83, on 10/12/2007, -0/+6I must be lazy after reading that article, time for a nap
- dlsspy, on 10/12/2007, -0/+5If you don't get it, you don't have to complain about it. I always write several times more java code to do the same thing as any I write in most other languages I use. Brevity is not java's strength.
This is not just some technique this guy came up with. Lazy evaluation is deferred memoization of idempotent methods. It's demonstrated in scheme because it's a built-in language feature. Java 1.5's Future interface is roughly the same thing.
You can surely implement such things yourself in other languages (I implemented a Promise object in java before 1.5), but you'd be implementing more stuff and making it look like more work to people who aren't familiar with the concepts.
People need to learn more programming languages. I often see people complain about scheme's parens, or python's whitespace, or ocaml's ``weird syntax'' or whatever. Then I see people complain about how something isn't popular, so it's a waste of time or whatever. Meanwhile, I learn stuff, and I work with people who learn stuff, and I can employ techniques that help me get my work done more easily and understandably. - Wisgary, on 10/12/2007, -1/+6I used to dislike scheme/lisp and think "aww all those parentheses are *****!", until I started working with it a bit more. With a good IDE, that AT LEAST highlights the stuff inside a set of parentheses, that problem is all but gone, and with those parentheses separating everything explicitly, coupled with prefix notation, code that used to look like crap starts to become really readable, unambiguous, and fun.
- AhmedB, on 10/12/2007, -1/+5Scheme is not lazy evaluation, scheme uses applicative order evaluation!
- Itkovian, on 10/12/2007, -0/+4I guess all those slandering the Lazy idea are not really getting the point. It actually is a very powerful technique, see e.g., http://citeseer.ist.psu.edu/karczmarczuk97generating.html. It's quite useful for stream processing, etc. It guarantees that your program will never do more work than is required to actually get the result you want. Intermediate values and data structures are never built, unless they are required on the path that leads to the program result.
- Tetraca, on 10/12/2007, -2/+6I always thought it was GOTOs.
- inmatarian, on 10/12/2007, -0/+4A good subject to look up on is Memoization, where in you buffer the results of computation for future use. In a language that supports first-class functions, such as Lua, you can implement Lazy Evaluation via the same techniques of Memoization.
- Bibimbap, on 10/12/2007, -2/+5LISP = Lots of Insanely Silly Parentheses
- Itkovian, on 10/12/2007, -0/+3@Tweakers: But does your language do that automagically? Take a look at Haskell, you might pick up some good ideas on the matter.
- AhmedB, on 10/12/2007, -0/+3I don't understand why I'm being dugg down, I'm stating a fact, but oh well, again Scheme uses applicative order evaluation 'normally' except for 'special forms' also called "keywords" in the latest Scheme standard (like cond, if and such) those use 'delayed' evaluation, everything else uses applicative order evaluation, parameters are evaluated before invoking the operator (function) on them.
- gammasts, on 10/12/2007, -2/+5And I thought that Scheme was pointless when I was learning it. In theory, this sounds like a good procedure, but it doesn't seem that every language is capable of it.
- gtlogic, on 10/12/2007, -3/+6Unfortunately, this is exactly why LISP and SCHEME will never be widely adopted like C++. There exist only a finite number of paratheses in the universe.
- koko775, on 10/12/2007, -1/+3Ahmed: But programming a Scheme evaluator in Scheme is trivial; changing the order of evaluation and coming out with a complete evaluator is pretty easy as well, relative to writing an evaluator in a lower-level language. Also, PLT Scheme supports different Scheme evaluators, I believe.
just got done with cs61a @ berkeley, so this stuff is fresh on my mind. - pauldonnelly, on 10/12/2007, -0/+2@Tangential
If I'm understanding you right, this isn't the same thing. Lazy evaluation isn't just doing it later -- it's putting off the evaluation of a function until it is actually needed, whenever that may be. Rather than evaluating your function at some arbitrary time, you can wait until the value needs to be used somewhere. If you're lucky it will never be needed so it will never be run. On the other hand, this *can* bite you in the ass if you're trying to adhere to some sort of schedule. If you need the results of hundreds or thousands of calculations all at once you'll be wishing you had done some of them sooner. - AhmedB, on 10/12/2007, -0/+2More information about the default behavior of Scheme, good reading I think:
http://mitpress.mit.edu/sicp/full-text/sicp/book/node85.html
Also this one, bullet 22:
http://www.cs.arizona.edu/~collberg/Teaching/520/2005/Html/Html-9/index.html - kaelyiesta, on 10/12/2007, -1/+2I always called it lazy algorithms, as in "man, Fibonacci heaps have bad ass lazy algorithms!"
- inmatarian, on 10/12/2007, -0/+1Look at the Java example, the idea basically goes like this
int object::thing()
{
if (!m_value) m_value = make_calculation();
return m_value;
}
This is object level buffering of values. The calculation isn't performed in the Constructor, which eliminates the front loading, but rather the first time when it's needed. - omaryak, on 10/12/2007, -0/+1Lazy Headline, Lazy Comment
- FearlessFreep, on 10/12/2007, -1/+2Done it in Python and Smalltalk ....
- jwm0z, on 10/12/2007, -0/+1The Java code is essentially an example of the Singleton pattern.
- OneAndOnlySnob, on 10/12/2007, -0/+1"Maybe I'm just really dense, but I've been using this technique (creating functions, calling them for computation only when needed or calling functions for computation and storing the results for when needed) for at least the past twenty years in dBase code, then C, now Tcl."
And people who have used LISP since it was invented in 1958, and Scheme by extension, have been doing it since ... 1958, newbie. - trigger0219, on 10/12/2007, -1/+2I read an article about doing this in java (Dr. Dobbs)... difficult though...
- hansamurai, on 10/12/2007, -0/+1I studied lazy evaluation in school, totally forgot about it until now.
Also thanks for reminding me of my scheme days. -_- - geezusfreeek, on 10/12/2007, -0/+1Basically everything in Io is based on this. It's great.
- dlsspy, on 10/12/2007, -0/+1Java 1.5 added Futures, which is roughly equivalent to a promise, except they may be evaluated concurrently in an execution service while you're waiting for the result. Note that the execution service doesn't have to be in another thread, so it's perfectly possible to model the promise behavior with an appropriate execution service implementation (I've done it a lot in unit testing).
- AhmedB, on 10/12/2007, -1/+2More specifically 'lazy evaluation' and 'Pass by Name' are special names for 'Normal Order evaluation' from a programming languages point of view, as opposed to the 'applicative order evaluation'.
- theevilmonkey, on 10/12/2007, -0/+0[deleted]
- tekwiz2k4, on 10/12/2007, -0/+0actually, I do understand Scheme, and quite well actually. I've written an HTTP server in my Principles of Programming class. Pointless but educational.
I just find the parentheses annoying. - synae, on 10/12/2007, -1/+1definitely. I love doing this stuff in python.
- waenyinepu, on 10/12/2007, -0/+0The D Programming Language implements a form of Lazy Evaluation in the form of "Lazy Parameters" where 'lazy' is used as a storage-type for function parameters. See: http://www.digitalmars.com/d/function.html -- section "Function Parameters." (Or just search the page for "lazy.")
Also, someone mentioned Memoization of functions, another sometimes useful concept. The following is a very quick-and-dirty function "caching" (memoize) template I tossed together one day for a discussion on the D newsgroup.
# import std .traits ;
#
# struct TCachedFunc (alias Func) { static:
#
# alias ReturnType !(Func) Ret ;
# alias ParameterTypeTuple !(Func) Params ;
#
# static if (is(typeof(Params[1]))) {
# // multiple parameters
#
# private struct Node {
# Params params ;
# }
#
# private Ret[Node] p_cache ;
#
# Ret opCall (Params args) {
# Node node ;
# Ret* result ;
#
# foreach (i, x; args) {
# node.params[i] = x;
# }
# result = node in p_cache;
#
# if (!result) {
# p_cache[node] = Func(args);
# result = node in p_cache;
# }
# return *result;
# }
# }
# else {
# // single parameter
#
# alias Params[0] Param ;
#
# private Ret[Param] p_cache ;
#
# Ret opCall (Param arg) {
# Ret* result = arg in p_cache;
#
# if (!result) {
# p_cache[arg] = Func(arg);
# result = arg in p_cache;
# }
# return *result;
# }
# }
# }
Given any function 'foo' one can then cache/memoize it as 'foo_cached' by writing:
alias TCachedFunc!(foo) foo_cached ; - sfultong, on 10/12/2007, -2/+2I love haskell. That means I'll be dugg up, right?
- pauldonnelly, on 10/12/2007, -0/+0Too late to edit, but credit for half the above post goes to Kenny Tilton. All sarcasm is my own work.
- 404notfound, on 10/12/2007, -2/+1I just finished a course at Berkeley taught in Scheme, and lazy evaluation was a significant portion of it. The interesting thing is that a delayed procedure is just that--a procedure. You just leave the procedure alone, and when you need it, simply call it. (delay foo) is nothing but syntactic sugar for (lambda () foo). Neat stuff.
- tangential, on 10/12/2007, -2/+1Meh, it's easy to do lazy initialization in c++ or java, just fire off a timer event for 0/10/etc seconds to process the rest of initialisation of the object. It'll queue it up in the event queue, and allow necessary messages like repaints to filter through before it. It's a common trick we do in Qtopia to get a responsive interface to the end user, while continuing to do the background work in the background. It's not really rocket science to code up, especially in a decent toolkit like Qt, QTimer::singleShot(0, this, SLOT(init()));. No threads, minimal overhead, and in a language that's likely to be used in a proffesional environment.
- Ghostal, on 10/12/2007, -2/+1I, for one, welcome this new buzz word to the front page and beyond.
- ghlmtz, on 10/12/2007, -3/+2This sort of thing can be done in Perl as well (probably because it's so multi-paradigmed). See M.J. Dominus' article here for more information on the use of lazy evaluation for streams (infinite lists):
http://perl.plover.com/Stream/stream.html - inactive, on 10/12/2007, -5/+2This would have been a good article had it used some practical language like C or Java, not dealt solely with scheme/lisp, a language which
- inactive, on 10/12/2007, -5/+1Brilliant! I love it!
- lhnz, on 10/12/2007, -8/+3I hate haskell.
- hambend, on 10/12/2007, -7/+2@tekwiz2k4
That just goes to show you don't get it. First class everything is a crazy way to program. - inactive, on 10/12/2007, -7/+1this is pretty retarded. you can structure your programs to only perform calculations when necessary without this ***** design pattern. why the hell did he use scheme? *****.
- GTPilot, on 10/12/2007, -7/+1I have always heard it referred to as "lazy loading" .. let's settle on a buzz word shall we? =D


What is Digg?