Docsity

Functional Programming Lecture 4: Referential Transparency and Higher Order Functions, Study notes of History of Education

Partial preview of the text

Download Functional Programming Lecture 4: Referential Transparency and Higher Order Functions and more Study notes History of Education in PDF only on Docsity!

sakai.rutgers.edu

• Check sakai for in class assignments

• I can manually reset sakai to allow you

to resubmit but waiting to hear if this

can be done automatically

Functional Programming

1. should a function access non-local

variables directly?

2. should a function have side effects?

3. do such languages have recursion?

4. do such languages have assignment

statements and loops?

5. functions as data: does it make

sense to pass a function as a

parameter or have a function as a

return value?

What is the functional programming

paradigm?

  • Referential transparency
    • value of an expression is independent of context where the function application occurs
    • means that all variables in a function body must be local to that function; why?
  • assignment statements?
    • there is no concept of assignment
    • variables are bound to values only through parameter associations
    • no side effects

Pure Functional Languages

  • Control flow accomplished through function application (and recursion) - a program is a set of function definitions and their application to arguments
  • Implicit storage management
    • needs garbage collection
  • Functions are 1st class values!
    • can be returned as value of an expression or function application
    • can be passed as an argument
    • can be put into a data structure and saved
  • Unnamed functions exist as values

Recursion instead of iteration

def tail(list):

return list[1:]

def length (list):

if (list == []):

return 0

else:

return 1 + length(tail(list))

Recursive Functions

length( [1, 2]) should yield 2.

Trace: (length [1 , 2]) --top level call

list = (1 2) (length [2]) --recursive call 1 list = (2) (length []) -- recursive call 2 list = ( ) returns 0 --return for call 2 returns (+ 1 0) =1 --return for call 1 returns (+ 1 1) = 2 --return for top level call

len is a shallow recursive function

atomcount

def atomcount (list):

if (list == []):

return 0

elif isList(list):

return atomcount(head(list)) +

atomcount(tail(list))

else:

return 1

atomcount is a deep recursive function

def head(list): return list[0]

Lecture 4, Functional Programming, CS314 © BGRyder/MGCore

>>> atomcount([1,[2,[3]]])

processing [1, [2, [3]]]

processing 1, return 1

processing [[2, [3]]]

processing [2, [3]]

processing 2, return 1

processing [[3]]

processing [3]

processing 3, return 1

processing [], return 0

processing [], return 0

processing [], return 0

Lecture 4, Functional Programming, CS314 © BGRyder/MGCore

Functions are First Order Values

  • Higher Order Functions take functions

as values to perform computation

  • apply is a higher order function
  • apply(function,list of arguments)
  • e.g., apply(len,[ [1,2,3] ]) = 3
  • def fnc_name(formal_parameters):

associates name with function

  • e.g.,

apply(fnc_name,actual_parameters)

Functional Programming, CS314 © BGRyder, MGCore

Lecture 4, Functional Programming, CS314 © BGRyder/MGCore

map

• map(f,l)

– applies function f to every element

of l (l must be a list)

– return value = list of return values

from each function application

• Example

– map(abs,[-1, 2, -3, -4, 3, 2]] =

[1,2,3,4,3,2]

Functional Programming, CS314 © BGRyder, MGCore

two argument functions

• can use map with two lists

– map(lambda x,y: x+y,[1,1,1,1],[2,3,4,5])

=[3,4,5,6]

– but what if I want to iteratively apply the

function f to the list

– e.g., f(f(f(item1,item2),item3),....) to

sum the numbers in a list

– use reduce function

reduce

• reduce(function,list,initial_value)

• starts from left:

• function(initial_value,list[0]) = r

• function(r1,list[1]) = r

• function(r2,list[2]) = r .....

• reduce assumes operator is ___ associative

• no built-in function for other type of

associativity

• have to write our own

Scheme

• For your personal computer:

  • http://www.plt-scheme.org/software/drscheme/
  • This is an integrated development environment supporting more than one language
  • R5RS is “Revised (5) of the Report on the Algorithmic Language Scheme” (i.e., current standard)

• If you don’t have access to a personal

computer

  • remus.rutgers.edu has a scheme interpreter called “scm”

History

Lisp 1950’s John McCarthy

Scheme 1975 Guy Steele Gerald Sussman

Common Lisp

dynamic scoping lexical scoping functions as first class values

standardized PL