{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04-FUN

# 04-FUN - Course Outline Part 1 Functional Language...

This preview shows pages 1–8. Sign up to view the full content.

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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 I’ll use the actual mathematical operators for clarity; when showing concrete code listings I’ll 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; let’s review some basics to make sure we’re 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
fun Features To define fun , we’ll need to define the following language features: Multi-Parameter 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
Multi-Parameter Functions: ( λ x 1 .. x k . e ) We’ve already done this in our informal notation, now we’ll 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 Sch¨ onfinkeling). The power of currying is that it allows partial application (aka specialization ). If we’re 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. Ben Hardekopf () Defining FUN Spring 2011 5 / 30

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Integers: Z We’ll actually define the natural numbers { 0, 1, 2, . . . } and leave integers for a homework assignment. We’ll use the strategy for defining natural numbers called Church numerals , which is inspired by the inductive definition we’ve already seen: n N ::= 0 | succ ( n ) Ben Hardekopf () Defining FUN Spring 2011 6 / 30
Integers: Z We’ll actually define the natural numbers { 0, 1, 2, . . . } and leave integers for a homework assignment. We’ll use the strategy for defining natural

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern