RPAL Function definitions Module 10.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
Topics RPAL Function definitions Functions are first-class objects let, where constructs Nesting and scopes Simultaneous definitions Cascading definitions @: infix use of a function. Functions are first-class objects
Function definitions – let D in E Example (simple definition): let X=3 in Print(X,X**2) // Prints (3,9) Example (function form): let Abs N = N ls 0 -> -N | N in Print(Abs (-3)) // Prints 3 X=3 is not an assignment. Equivalent to: (fn X. Print(X,X**2))3 Equivalent to: let Abs = fn N. N ls 0 -> -N | N in Print (Abs (-3)) (fn Abs. Print(Abs (-3)) (fn N. N ls 0 -> -N | N) Print((fn N. N ls 0 -> -N | N) (-3)))
Nesting Definitions Scope of ‘let’ parameter is expression after ‘in’. Nested scopes are as expected. let X = 3 in let Sqr X = X**2 Print (X, Sqr X, X * Sqr X, Sqr (X+2)) Scope of X=3, except for Scope of Sqr
Function definitions – E where D Example (simple definition): Print(X,X**2) where X=3 // Prints (3,9) Example (function form): Print(Abs (-3)) where Abs N = N ls 0 -> -N | N // Prints 3 X=3 is not an assignment. Equivalent to: (fn X. Print(X,X**2))3 Equivalent to: (fn Abs. Print(Abs (-3))) (fn N. N ls 0 -> -N | N) Print((fn N. N ls 0 -> -N | N) (-3))
Nesting definitions vs. let X = 3 in let Sqr X = X**2 in Print (X, Sqr X, X * Sqr X, Sqr (X+2)) vs. ( Print (X, Sqr X, X * Sqr X, Sqr (X+2)) where Sqr X = X**2 ) where X = 3 Parentheses required ! Otherwise Sqr X = (X**2 where X=3)
Simultaneous Definitions Join definitions with ’and’. Example: let X=3 and Y=5 in Print(X+Y) Keyword and: not a boolean operator (we have &). Both definitions come into scope at once, in Print(X+Y). Different from let X=3 in let Y=5 in Print(X+Y) let X=3 in let Y=5+X in Print(X+Y) Scope of X=3
cascading function definitions ’within’: make one definition visible inside other(s). Example: let c=3 within f x = x + c in Print(f 3) vs. let c=3 in let f x = x + c Scope of c=3 Primitive form of information hiding
The ‘@’ operator Allows infix use of a function. Example: let Add x y = x + y in Print (2 @Add 3 @Add 4) Equivalent to: in Print (Add (Add 2 3) 4) Useful for concatenating strings: Print(’a’ @Conc ’b’ @Conc ’c’)
Functions are first-class objects Can do (almost) anything with a function. Name it: let Inc x = x+1 in Inc (3) Pass it as a parameter: let f g = g 3 in let h x = x + 1 in Print(f h) Return it from a function: let f x = fn y. x+y in Print (f 3 2) Print it! let Inc x = x+1 in Print (Inc)
Functions are first-class objects Select it using a conditional: let B=true in let f = B -> (fn y.y+1) | (fn y.y+2) in Print (f 3) Store it in a tuple: let T=((fn x.x+1),(fn x.x+2)) in Print (T 1 3, T 2 3) Apply it to a tuple (n-ary function): let Add (x,y) = x+y in Print (Add(3,4))
summary RPAL Function definitions Functions are first-class objects let, where constructs Nesting and scopes Simultaneous definitions Cascading definitions @: infix use of a function. Functions are first-class objects