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?
> 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.
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.]
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.
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".