pure or not pure?

Georg Wrede georg at nospam.org
Thu Apr 10 11:35:37 PDT 2008


Steven Schveighoffer wrote:
> I've been reading a bit about FP, and I see now that you are correct that 
> mutable data isn't allowed.  But let's not forget that D is not a functional 
> language, so a loop like:
> 
> for(int i = 0; i < n; i++)
>    statement;
> 
> Is legal inside a D pure function, whereas the same code would be achieved 
> through tail-recursion in a functional language.  Having mutable return 
> values and arguments may not prevent the compiler from considering the 
> function pure (then again, it may, that is my question).  But certainly, 
> comparing D to Haskell doesn't prove either case.

It doesn't. And whether we can have mutable arguments and/or mutable 
return values, is simply up to Walter. Nothing else.

However, I don't see any other way than both being immutable.

If you want to change the the value returned by a pure function, you 
have to make a copy of it. Just plain old COW. And most of the time, 
that feels natural anyway.

> I don't think the intent of bolting functional programming optimization 
> opportunities on top of D is going to mean that during pure functions we 
> will have to use a fully FP style.

That would be disaster. But that's just my opinion. :-)



More information about the Digitalmars-d mailing list