Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Spring 2003: Questions on Functional Programming

Questions, answers, comments?

(Back to Sp2003 Final Exam Review)


1. All apply to lists. Map allows you to apply a function to a list. Filter only applies the function to the things in the list that are asked for in the function. Reduce gathers inputs and analyzes results based on the data the function gathers in the list.
2. In functional programming, a function is data that can be used as an input so that we can write functions that call other functions. We can define a class (a certain object) and create our own methods to execute on that class. You can use functional programming when writing small functions that process one piece of data (using concepts of granularity and modularity). Since this allows you to do a lot in a few lines of code, it is useful when the problem in your code is hard to find so that you can get as far as possible in the least number of lines.
3. People use functional programming when they hope to make the complexity of the problem smaller and want to re-use functions (for example, professional programmers have libraries of functions that they reuse to make programming easier.)
4. Mulitple functions allow you to hide details so that you can ignore them, it makes testing easier, and it helps in writing new programs because you can use trusted and useful functions. Kelly Farrell

No, filters apply to all elements in the list – it's what it returns that is important. Reduce isn't quite right, either. Functional programming doesn't have to do with libraries of functions. Mark Guzdial


Is this any better?
1. Map, filter, and reduce all are functions that take as input a function and a sequence. Map then applies the function to each input in the sequence, and returns each "value" (or whatever the function returns for each). Filter apllies the function to each element in the sequence and then returns the value if the return value is true or skips the value if it is false. Reduce combines all the returned values and returns that value.
Summer McWilliams
Filter doesn't return the value, it returns the element. Mark Guzdial

reasons to use functional programming :
write/rewrite less code
use functions to manipulate other functions
functions can call themselves
Katie Graybeal
Nice, Katie! When would you use functional programming? Mark Guzdial

1. Apply takes a function as input and the inputs to that function in a sequence. Apply literally applies the function to the input. Example:

def hello(someone):
  print "Hello,",someone
>>> hello("Mark")
Hello, Mark
>>> apply(hello,["Mark"])
Hello, Mark
>>> apply(hello,["Betty"])
Hello, Betty

Map is a function that takes as input a function and a sequence. But it applies the function to each input in the sequence, and returns whatever the function returns for each.
Example:

>>> map(hello, ["Mark","Betty","Matthew","Jenny"])
Hello, Mark
Hello, Betty
Hello, Matthew
Hello, Jenny
[None, None, None, None]

Filter also takes a function and a sequence as input. It applies the function to each element of the sequence.

Example:

def rname(somename):
  if somename.find("r") == -1:
    return 0
  if somename.find("r") != -1:
    return 1

>>> rname("January")
1
>>> rname("July")
0
>>> filter(rname, ["Mark","Betty","Matthew","Jenny"])
['Mark']

Reduce takes a function and a sequence as input, like the others. But reduce combines the results. (It works well when used with filter.)

Example:

def add(a,b):
  return a+b
>>> reduce(add,[1,2,3,4,5])
15
In this example, we total all the numbers by adding 1+2, then (1+2) + 3, then (1+2+3)+4, then (1+2+3+4)+5

As usual, lots of info, but more to help understand than to memorize. :)Lauren Biddle
As usual, nicely done, Lauren! Suggestion: Try writing a different function and using it with filter, to get the idea. Like "startsWithA". Mark Guzdial

3: People use functional programming to make testing simpler (when using integration testing). Use should also use functional programming to ease testing and complexity. Is this right? Kelly Farrell

People use all three paradigms (procedural, object-oriented, and functional) when doing integration testing. People use functional when they want to solve really complex problems and use as few lines as possible. It's most popular with people working on artificial intelligence. Mark Guzdial




Link to this Page