DOC PREVIEW
UT Arlington CSE 3302 - Lecture 12: Functional Language

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302Lecture 12: Functional Languages12 Oct 2010Nate NystromUniversity of Texas at ArlingtonFunctional languagesKey idea 1:A variable names a value not a location• let x = 3 // x is 3. x will always be 3.• x = 4 // error: re-assignment not allowedWhat does this give you?Functions have no side-effects• Only inputs to a function are its arguments• Only output of a function is its return valueIf a function is called multiple times with the same arguments, it will always return the same value (can memoize)Can run functions concurrently without interferenceNo accidental coupling between components(i.e., thru shared variables)Functional languagesKey idea 2:Functions are values, just like integers, strings, etc.• Can create new functions at run time• Can pass functions to other functions• Can return functions from functions• Can put functions in variablesWhat does this give you?What does this give you?Power!What does this give you?Power!(mwa ha ha ha)What does this give you?Higher-level, more declarative programming styleCan build programs by composing functionsPassing functions to functionsIn Scala:Sum all the elements of a list xs:• xs.foldLeft(0)(_0+0_)Multiply all the elements of a list xs:• xs.foldRight(1)(_0*0_)Sum of squares:• xs.map(x0=>0x*x).foldRight(0)(_0+0_)_ + _ is a function that adds two integers_ * _ is a function that multiples two integersPassing functions to functionsIn OCaml:Sum all the elements of a list xs:• fold_left0(+)000xsMultiply all the elements of a list xs:• fold_left0(*)010xsSum of squares:• fold_left0(+)000(map0(fun0x0?>0x*x)0xs)(+) is a function that adds two integers(*) is a function that multiples two integersSyntactic noteNote that function calls are so ubiquitous in functional languages that in most functional languages the syntax for a function call is juxtaposition:f0xvs.f(x)Passing functions to functionsScala:val0xs0=0List(“a”,0“B”,0“c”,0“D”,0“e”)val0less0=0(x:0String,0y:0String)0=>0x0<0yval0iless0=0(x:0String,0y:0String)0=>0x.toLowerCase0<0y.toLowerCasescala>0xs.sort(less)res4:0List[java.lang.String]0=0List(B,0D,0a,0c,0e)scala>0xs.sort(iless)res5:0List[java.lang.String]0=0List(a,0B,0c,0D,0e)Operators as functionsMost of these languages allow built-in operators to be used as functionsOCaml:(+) is a function that takes two ints and returns their sum(*) is a function that takes two ints and returns their productScala:_0+0_ is shorthand for (x,y)0=>0x+y• But this only works in some contexts(e.g., cannot do val0plus0=0_0+0_)• Why?Some useful functions on lists:These are found in most functional languagesExamples here are OCamlfoldLeft, fold_left, foldlfold_left0(+)000[1;2;3;4;5]00000==0000(((((00+01)0+02)0+03)0+04)0+05)foldRight, fold_right, foldrfold_right0(+)0[1;2;3;4;5]000000==0000(10+0(20+0(30+0(40+0(50+00)))))mapmap0(fun0x0?>0x*x)0[1;2;3;4;5]00==0000[1;4;9;16;25]Some more useful functions on ListsScala (other languages have similar functions):scala>0List(?2,0?1,00,01,02).filter(_0>00)res6:0List[Int]0=0List(1,02)scala>0List(?2,0?1,00,01,02).count(_0>00)res7:0Int0=02scala>0List(?2,0?1,00,01,02).forall(_0>00)res9:0Boolean0=0falsescala>0List(?2,0?1,00,01,02).exists(_0>00)res10:0Boolean0=0truescala>0List(?2,0?1,00,01,02).partition(_0>00)res11:0(List[Int],0List[Int])0=0(List(1,02),0List(?2,0?1,00))Creating functions at run-timeScala• (x:Int)0=>0x+1• (x:Int,0y:Int)0=>0x+y• (x:Int)0=>0(y:Int)0=>0x+yScheme• (lambda0(x)0(+0x01))• (lambda0(x0y)0(+0x0y))• (lambda0(x)0(lambda0(y)0(+0x0y))OCaml• fun0x0?>0x+1• fun0(x,0y)0?>0x+y• fun0(x)0?>0fn(y)0?>0x+yPython◾lambda0x:0x+1◾lambda0x,y:0x+y◾lambda0x:0lambda0y:0x+yFunctions that take functionsScala:def0map[S,T](f:0S0=>0T,0list:0List[S]):0List[T]0=0{0000list0match0{00000000case0Nil0=>0Nil00000000case0head0::0tail0=>0f(head)0::0map(f,0tail)0000}}scala>0map[Int,Int](_+1,0List(1,02,03))res6:0List[Int]0=0List(2,03,04)CurryingAny function that takes multiple args can be written to take the first arg and return a function that takes the restThis is called currying (after Haskell Curry)• nb. should really be schönfinkeling (after Moses Schönfinkel)def0sum(x:0Int,0y:0Int)0=0x0+0yvs.def0curriedSum(x:0Int)0=0(y:0Int)0=>0x0+0yApply the arguments one at a time:sum(1,2)0vs.0curriedSum(1)(2)Curryingdef0curriedSum(x:0Int)0=0(y:0Int)0=>0x0+0ycurriedSum(1)(2) adds 1 and 2curriedSum(1) is a function that adds 1 to its argumentscala>0map[Int,Int](curriedSum(1),0List(1,02,03))res7:0List[Int]0=0List(2,03,04)Currying the map functionScala:def0curriedMap[S,T](f:0S0=>0T)0=0(list:0List[S])0=>0{0000list0match0{00000000case0Nil0=>0Nil00000000case0head0::0tail0=>0f(head)0::0map(f,0tail)0000}}scala>0curriedMap[Int,Int](_+1)(List(1,02,03))res8:0List[Int]0=0List(2,03,04)Currying the map functionScala:def0curriedMap2[S,T](f:0S0=>0T)(list:0List[S])0=0{0000list0match0{00000000case0Nil0=>0Nil00000000case0head0::0tail0=>0f(head)0::0map(f,0tail)0000}}scala>0curriedMap2[Int,Int](_+1)(List(1,02,03))res9:0List[Int]0=0List(2,03,04)Currying the map functiondef0curriedMap2[S,T](f:0S0=>0T)(list:0List[S])0=0{0000list0match0{00000000case0Nil0=>0Nil00000000case0head0::0tail0=>0f(head)0::0map(f,0tail)0000}}Thus:curriedMap2[Int,Int](_+1)is)a)func.on)that)takes)a)List[Int])and)adds)1)to)each)element)of)the)l ist.Currying in other functional languagesCurrying is so common, and so useful, that its the default in most functional languages. Example: OCaml:fun0add0x0y0=0x0+0yis syntactic shorthand for:let0add0=0fun0x0?>0fun0y0?>0x0+0ynot:let0add0=0fun0(x,y)0?>0x0+0yTo call:add0102Not:add(1,2)Some functional languagesLambda calculus• Alonzo Church 1933• a mathematical notation for describing all computationLISP family• LISP - John McCarthy 1958• Scheme - Guy Steele 1980ML family• ML - Robin Milner late 1970s• Standard ML (SML) - 1990• Objective Caml (OCaml) - Xavier Leroy 1996• F# - OCaml on .NET - Don Syme @MSRHaskell• Simon Peyton-Jones (SPJ) and others 1990sLISPFirst functional language• John McCarthy 1958“LISt Processing”• key data structure is linked list (cons cell)Core of the language is the lambda calculusIntroduced:• first-class functions• (lambda (x) (+ 1 x))• garbage collection• eval –!code is dataMany dialects: Common LISP, Emacs LISP, Autolisp, SchemeMLRobin Milner @ Edinburgh late 1970s• “Meta language”• “meta” = “about”• Designed for writing programs that manipulate other programsFeatures:• pattern


View Full Document

UT Arlington CSE 3302 - Lecture 12: Functional Language

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Lecture 12: Functional Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 12: Functional Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 12: Functional Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?