1 / 27

The building blocks of functional computing

CS 121 today. data, sequences. The building blocks of functional computing. conditionals. recursion. List Comprehensions. map and applications. functional programming. >>> 'fun' in 'functional' True. representation via list structures (data).

conlan
Download Presentation

The building blocks of functional computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 121 today data, sequences The building blocks of functional computing conditionals recursion List Comprehensions map and applications

  2. functional programming >>> 'fun' in 'functional' True • representation via list structures (data) • leverage self-similarity (recursion) • create small building blocks (functions) Compose these together to solve or investigate problems. Key ideas in functional programming not maximally efficient for the computer… elegant and concise vs.

  3. return to recursion Composing functions into specific applications Creating general functions that will be useful everywhere (or almost…)

  4. return to recursion Composing functions into specific applications Creating general functions that will be useful everywhere (or almost…) building blocks with which to compose…

  5. sum, range defsum(L): """ input: a list of numbers, L output: L's sum """

  6. sum, range defsum(L): """ input: a list of numbers, L output: L's sum """ if len(L) == 0: return 0.0 else: return L[0] + sum(L[1:]) Base Case This input to the recursive call must be "smaller" somehow… if the input has no elements, its sum is zero Recursive Case if L does have an element, add that element's value to the sum of the REST of the list…

  7. sum, range defrange(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ excluding hi

  8. sum, range defrange(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ ifhi <= low: return [] else: return excluding hi

  9. sum, range defrange(low,hi): """ input: two ints, low and hi output: int list from low up to hi """ ifhi <= low: return [] else: return [low] + range(low+1,hi) excluding hi

  10. Recursion: Good News/Bad News Recursion is common (fundamental) in functional programming defdblList(L): """ Doubles all the values in a list. input: L, a list of numbers """ if L == []: return L else: return [L[0]*2] + dblList(L[1:]) But you can sometimes hide it away!

  11. Map: The recursion "alternative" def dbl(x): return 2*x >>> map( dbl, [0,1,2,3,4,5] ) [0, 2, 4, 6, 8, 10] def sq(x): return x**2 >>> map( sq, range(6) ) [0, 1, 4, 9, 16, 25] (1) map always returns a list (2) map(f,L) calls f on each item in L def isana(x): returnx=='a’ >>> map( isana, 'go away!' ) [0, 0, 0, 1, 0, 1, 0, 0] Hey… this looks a bit False to me!

  12. Map ! defdblList(L): """ Doubles all the values in a list. input: L, a list of numbers """ if L == []: return L else: return [L[0]*2] + dblList(L[1:]) Without map def dbl(x): return x*2 defdblList(L): """ Doubles all the values in a list. input: L, a list of numbers """ return map(dbl, L) With map!

  13. Map: a higher-order function In Python, functions can take other functions as input… def map( f, L ): KeyConcept Functions ARE data!

  14. Why use map?

  15. Why use map? More elegant / shorter code, “functional in style” Faster execution in Python – map optimized for operations in lists Avoid rewriting list recursion (build once, use lots)

  16. Mapping without map: List Comprehensions Anything you want to happen to each element of a list name that takes on the value of each element in turn the list (or string) any name is OK! >>> [ dbl(x) for x in [0,1,2,3,4,5] ] [0, 2, 4, 6, 8, 10] input output input >>> [ x**2 for x in range(6) ] [0, 1, 4, 9, 16, 25] output >>> [ c == 'a'for c in 'go away!' ] [0, 0, 0, 1, 0, 1, 0, 0] input output

  17. Mapping without map: List Comprehensions def dbl(x): return 2*x >>> map( dbl, [0,1,2,3,4,5] ) [0, 2, 4, 6, 8, 10] >>> [ dbl(x) for x in [0,1,2,3,4,5] ] [0, 2, 4, 6, 8, 10] def sq(x): return x**2 >>> map( sq, range(6) ) [0, 1, 4, 9, 16, 25] >>>[ x**2 for x in range(6) ] [0, 1, 4, 9, 16, 25] >>> map( isana, 'go away!' ) [0, 0, 0, 1, 0, 1, 0, 0] def isana(x): returnx=='a’ >>> [ c == 'a'for c in 'go away!' ] [0, 0, 0, 1, 0, 1, 0, 0]

  18. List Comprehensions def len(L): if L == []: return 0 else: return 1 + len(L[1:]) len(L) def sajak(s): if len(s) == 0: return 0 else: if s[0] not in 'aeiou': return sajak(s[1:]) else: return 1+sajak(s[1:]) sajak(s) def sScore(s): if len(s) == 0: return 0 else: return letScore(s[0]) + \ sScore(s[1:]) sScore(s) scrabble score implemented via raw recursion

  19. List Comprehensions len(L) LC =[1 for x in L] return sum( LC )

  20. List Comprehensions len(L) LC =[1 for x in L] return sum( LC ) sajak(s) # of vowels LC = [c in 'aeiou' for c in s] return sum( LC )

  21. List Comprehensions len(L) LC =[1 for x in L] return sum( LC ) sajak(s) # of vowels LC = [c in 'aeiou' for c in s] return sum( LC ) sScore(s) scrabble score LC =[ letScore(c) for c in s] return sum( LC )

  22. Write each of these functions concisely using list comprehensions… Write input: e, any element L, any list or string Remember True == 1 and False == 0 output: the # of times L contains e def count(e,L): example: count('f', 'fluff') == 3 W are the winning numbers Write input: Y and W, two lists of lottery numbers (ints) Y are your numbers def lotto(Y,W): output: the # of matches between Y & W example: lotto([5,7,42,44],[3,5,7,44]) == 3 Extra! Write input: N, an int >= 2 output: the number of positive divisors of N def divs(N): example: divs(12) == 6 (1,2,3,4,6,12)

  23. count(e,L) LC =[x==e for x in L] return sum( LC )

  24. lotto(Y,W) LC = [c in Y for c in W] return sum( LC )

  25. divs(N) LC =[ N%c==0 for c in range(1,N+1)] return sum( LC )

  26. count(e,L) LC =[x==e for x in L] return sum( LC ) lotto(Y,W) LC = [c in Y for c in W] return sum( LC ) divs(N) LC =[ N%c==0 for c in range(1,N+1)] return sum( LC )

More Related