For Programmers: Free Programming Magazines  


Home > Archive > Functional > July 2007 > Re: Newbie Question: Is it allowed for the function to change the









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Newbie Question: Is it allowed for the function to change the
Dan Bensen

2007-07-24, 7:08 pm

beginner wrote:
> I am really new to functional programming. I am just wondering if it
> is theoretically allowed to for the function to change the value of
> the parameters?


No, not in pure functional programming. In pure FP, you're not supposed
to change any variable values once you bind them.

> def f(list):
> list.append("a")
> will change the list passed in. Is this generally acceptable practice
> in functional programming?


In pure FP, your function should return list + ["a"].
That would be functional, because it creates a new value without
changing the old ones.

> How about this:
> list=[]
> f= lambda x: list.append(x)


Same thing. That has a side effect, so it's not pure FP.

--
Dan
www.prairienet.org/~dsb/
Bruno Desthuilliers

2007-07-26, 4:17 am

beginner a écrit :
(snip)
> Then I guess it is pretty difficult to go pure FP in python,


Indeed. Unless you don't give a damn about perfs and resources.

Python has enough to let you get a first taste of FP (mostly: functions
as first class objects, closures, and generators), but while it
sometimes draw inspiration from functional languages (and idioms), it's
definitively not one.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com