The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Side-Effects and Functional Programming

7 replies on 1 page. Most recent reply: Sep 22, 2008 8:16 AM by david david

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Side-Effects and Functional Programming Posted: Sep 12, 2008 6:01 PM
Reply to this message Reply
Advertisement

Among the key motivators behind a resurgent interest in functional programming is potentially more predictable code. In functional programming, a "pure" function does not produce side-effects, which makes testing such functions more predictable, and also renders the code easier to read.

Another key idea of functional programming, lazy evaluation, however, can offset that initial gain, writes Matthew Podwysocki in a recent blog post, Side Effects and Functional Programming.

Podwysocki notes that a pure function has the following characteristics:

  • Evaluate with the same result given the same input, and perform no state change.
  • Evaluation of the given function does not cause observable side effects (write to console, write to database, etc)

While these are desirable features in terms of code clarity and reliability, few modern programing languages make it easy to take full advantage of pure functions. Podwysocki notes that that is hardly a fault of the languages themselves, but is rather an indication of the difficulty of combining the concept of pure functions with other functional, and even imperative, programming constructs.

One such notion is lazy evaluation of functions, a necessary ingredient of functional languages:

Things get even more clouded as we bring lazy evaluation to the table. One of the great things that came to C# in 2.0 and especially in 3.0 with LINQ was the idea of lazy evaluation through the yield keyword. This gave us the ability to defer work until it was absolutely needed. That's one of the hallmarks of functional programming, especially a lazy language such as Haskell.

Working with lazily evaluated functions, however, can make it more difficult to ensure that functions also stay as pure as possible, writes Podwysocki, quoting examples from C# 3.0 and F#.

Other challenges to function purity arise from exception handling, resource management, and closures, all in the context of lazy function evaluation. Podwysocki notes that there is no magical solution to these problems, and developers instead should carefully analyze their code for potential side-effects:

Functional programming and side effects are important topics... The ideas in functional programming is to strive towards a side effect free style. When we start introducing lazy evaluation, concurrency and other issues, we need to be mindful of side effects and how we manage them.

Do you agree with Podwysocki that lazy function evaluation makes working with pure functions harder?


Maarten Hazewinkel

Posts: 32
Nickname: terkans
Registered: Jan, 2005

Re: Side-Effects and Functional Programming Posted: Sep 13, 2008 8:42 AM
Reply to this message Reply
> One such notion is lazy evaluation of functions, a
> necessary ingredient of functional languages

I would note that there is absolutely no requirement for lazy evaluation in a functional language.

The relationship between pure functions and laziness is that pure functions allow you to use lazy evaluation, since for a pure function it doesn't matter when it is evaluated, the result is always the same.

Raoul Duke

Posts: 127
Nickname: raoulduke
Registered: Apr, 2006

Re: Side-Effects and Functional Programming Posted: Sep 15, 2008 12:51 PM
Reply to this message Reply
i know haskell has its own problems, but it bugs me that people say things like "purity is good, side effects are bad, even in C#" and then don't say "which sorta makes one stop and think: why are we still using bloody C#?" [i know the main answers to that, but it still irks me a lot.]

Jules Jacobs

Posts: 119
Nickname: jules2
Registered: Mar, 2006

Re: Side-Effects and Functional Programming Posted: Sep 17, 2008 5:19 AM
Reply to this message Reply
> Do you agree with Podwysocki that lazy function
> evaluation makes working with pure functions harder?

I don't, but Podwysocki doesn't say that. He says that lazy evaluation makes working with impure "functions" harder.

david david

Posts: 16
Nickname: david21001
Registered: Mar, 2007

lazy and impure? Posted: Sep 18, 2008 1:39 PM
Reply to this message Reply
Does this mean: return (lazyProgrammers == impure) ? 1 : 0

;)

Megan Gay

Posts: 2
Nickname: meg557
Registered: Sep, 2008

Re: Side-Effects and Functional Programming Posted: Sep 20, 2008 2:48 PM
Reply to this message Reply
Hi, I was wondering if anyone could help me figure out how to do these problems in Python. I am so lost! Please help me if you are able to.

1) Suppose you see the following program

def main():
num = input("Enter a number: ")
for i in range(5):
num = num / 2
print num

main()

Suppose the input to this program is 1024, what is the output? Do it first without a computer, and then run it to verify (or correct) your answer.

2) In a program, you find
x=input("Please enter a number")
a) Some user gets a little literal and types in a number What happens?
b) A slightly smarter user realizes that just typing in a number won’t work; you need quotes around text. Said user types in "a number". Now what happens?

3) Why does
x=x+1
work in Python, but
x+1=x
not work?

4)
>>> x, y, z = 1, 2,3
>>> x, y = y, z
>>> y,z = z, x
>>> z, y = x, y
>>> print x, y, z

First try to predict what the answer will be, then verify.

Jules Jacobs

Posts: 119
Nickname: jules2
Registered: Mar, 2006

Re: Side-Effects and Functional Programming Posted: Sep 21, 2008 2:40 PM
Reply to this message Reply
You're probably going to fail that class.

david david

Posts: 16
Nickname: david21001
Registered: Mar, 2007

for megan Posted: Sep 22, 2008 8:16 AM
Reply to this message Reply
You need to go on Amazon and order Learning Python by Lutz and Ascher that will help you out tremendously. The first couple of chapters can easily answer your questions.

However, I don't think this is an appropriate place to get help on programming questions like yours.

Some would rather make snide comments to students instead of "teaching them how to fish".

Kind regards,

Flat View: This topic has 7 replies on 1 page
Topic: The Adventures of a Pythonista in Schemeland, Part 2 Previous Topic   Next Topic Topic: The Adventures of a Pythonista in Schemeland, Part I

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use