pure and (fully) lazy?

J. Shimizu tamayo89 at gmail.com
Thu Oct 14 11:51:47 PDT 2010


I have heard of D once or twice, so in a paroxysm of profligacy in a bookstore
recently, I picked up Dr Alexandrescu's very enjoyable book "The D Programming
Language."  One thing he does not discuss therein, however, is lazy parameters.

In the book, Dr Alexandrescu most properly deplores the teaching of the
Fibonacci function as a direct recursion.  He goes on of course to show how
the D language is willing to call any procedure that has no observable
side-effects (for stringent values of "observable") a pure function, by
showing that the D compiler accepts the iterative version of the Fibonacci
function as pure.

He and the compiler are right, naturally, but there is one thing that he did
not mention and that the compiler could not be expected to discover.  The
iterative Fibonacci function is mechanically derivable from its recursive
expression by means of dynamic programming, and dynamic programming can be
simulated by memoization.

>From what I understand from the web site, however, the lazy attribute on a D
function parameter makes it call-by-name, not call-by-need.  Consequently, I
see no way to implement a memoized recursive version of the Fibonacci function
(or any other pure function) and still have the compiler believe me when I
call that function "pure".  I respectfully hope I might be disabused of that fear.


More information about the Digitalmars-d mailing list