This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Course Outline Part 1: Functional Language Interpreter I Defining a language: syntax and semantics I Starting small: the lambda calculus I Building up: defining fun I Exploring functional programming topics Part 2: Adding a Type System Part 3: Exploring Memory Management Part 4: State and OOP Ben Hardekopf () Defining FUN Spring 2011 1 / 30 Defining fun We will use the calculus to define the language fun (for functional). All of these language constructs can be defined in terms of the calculus. Definition (Syntax of fun ) n Z bval { true , false } x Variable e Exp ::= n  bval  x  x . e  e 1 e 2  e 1 + e 2  e 1 e 2  e 1 e 2  e 1 e 2  e 1 && e 2  e 1 k e 2  ! e  e 1 e 2  if e 1 then e 2 else e 3  nil  nil? e  e 1 :: e 2  head e  tail e  let x = e 1 in e 2  letrec x = e 1 in e 2 (Note: when discussing the language Ill use the actual mathematical operators for clarity; when showing concrete code listings Ill use the standard ASCII operators, e.g., *, /, <=, , etc) Ben Hardekopf () Defining FUN Spring 2011 2 / 30 More About Lists Lists are a common data structure in functional programming; lets review some basics to make sure were all on the same page. Constructing lists : 1 :: 2 :: 3 :: 4 :: nil Deconstructing lists : I head 1 :: 2 :: 3 :: 4 :: nil = 1 I tail 1 :: 2 :: 3 :: 4 :: nil = 2 :: 3 :: 4 :: nil Testing lists : I nil? nil = true I nil? 1 :: 2 :: 3 :: 4 :: nil = false Ben Hardekopf () Defining FUN Spring 2011 3 / 30 fun Features To define fun , well need to define the following language features: MultiParameter Functions: ( x 1 .. x k . e ) Integers: Z Arithmetic Operators: +, , , Boolean Values: true and false Boolean Operators: , , , Conditionals: if .. then .. else .. List Operators: nil , nil? , cons (i.e., ::), head , tail Name Binding: let and letrec Note that we can easily encode other datatypes as well; for example, we could encode strings as lists of ASCII numbers. Ben Hardekopf () Defining FUN Spring 2011 4 / 30 MultiParameter Functions: ( x 1 .. x k . e ) Weve already done this in our informal notation, now well formalize it. The process of turning a function with multiple parameters into a series of nested functions, each with one parameter, is called currying . Definition (Currying) ( x 1 .. x k . e ) ( x 1 . ( .. . ( x k . e ))) This technique is named after logician Haskell Curry (it was originally invented by Moses Sch onfinkel, but no one wanted to call it Schonfinkeling). The power of currying is that it allows partial application (aka specialization ). If were going to apply a function multiple times and the first few arguments are always the same, we can partially apply the original function to the constant arguments to get a new, specialized function....
View Full
Document
 Spring '08
 Staff
 Functional Programming

Click to edit the document details