1 / 7

Programs as Functions

Programs as Functions. Some programs act like mathematical functions Associate a set of input values from the function ’ s domain with a set of output values from the function ’ s range E.g., written as y = f(x) or f : X → Y …

kramera
Download Presentation

Programs as Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programs as Functions • Some programs act like mathematical functions • Associate a set of input values from the function’s domain with a set of output values from the function’s range • E.g., written as y = f(x) or f : X → Y … … where X is the domain, Y is the range, xX is the independent variable, and yY is the dependent variable • No assignment, so no loops in purely functional code • Instead, rely on recursion to perform iteration • Referential transparency • Function’s value depends only on arguments (+ global state) • Value semantics • No local state

  2. Intro to Lambda Calculus • Syntax expression → constant | variable | ‘(‘ expression expression ‘)’ | ‘(‘ ‘λ’ variable ‘.’ expression ‘)’ • Lambda expressions: function, parameters, arguments • E.g., (λx. + 1 x), written (lambda (x) (+ 1 x)) in Scheme • Can apply a lambda expression (“beta reduction”) • E.g., (λx. + 1 x) 2 replaces x with 2, evaluates to 3 • Can parameterize an expression (“beta astraction”) • E.g., can parameterize expression (+ 1 2) as (λx. + 1 x) 2 • Can also rename variables (“alpha conversion”) and remove redundant expressions (“eta conversion”)

  3. Intro to Scheme (a Dialect of Lisp) • General syntactic structure of Scheme is very simple expression → atom | ‘(‘ {expression} ‘)’ atom → literal | symbol literal → number | string | character | boolean • Atomic literals evaluate to themselves • 100, “world”, #\z, #T, #f, • Symbols are treated as identifiers • Which are then looked up in a symbol table for the current environment, replaced by the values that are found there • If a symbol names a function, it is applied to other values

  4. Expressions in Scheme • All are either special forms or function applications • Special forms begin with a Scheme keyword (e.g., car, cdr, cond, cons, define, display, if, lambda, let, letrec, quote, etc.) • Function application (call) is prefix: name then arguments • Predefined operators for many basic functions • Such as + (addition), * (multiplication), / (division), etc. • Selection expressions for if, if-else, and if-elseif logic • Use if form for single selection, vs. cond form for multiple • Binding lists (using the let or letrec keywords) • Associate values with variables before applying a function • Lambda expressions (using the lambda keyword) • Define formal parameter lists for (anonymous) functions

  5. Data Structures in Scheme • Basic construct is a list node (box and arrow notation) • Lists are concatenations of list (or list of list …) nodes • Functions car and cdr select the head vs. the rest of a list • The cons function constructs a list from two others • Concatenates them with the first in front of the second • The null? primitive tests whether or not a list is empty • Useful for recursive operations on lists (“cdr down, cons up”) L car L cdr L car cdr L cdrcdr L “hello, ” “world!”

  6. Evaluation, Type Checking, and Scopes • Scheme uses applicative order evaluation • Arguments evaluated first, then function is applied to them • Recursively, expression tree is evaluated leaves-to-root • Special forms use delayed evaluation • E.g., else part of a cond expression may not evaluate at all • Can prevent evaluation using the quote keyword • Identifies a list as being a literal rather than an expression • Scheme type checks only when absolutely necessary • E.g., just before a primitive function is applied • Scoping is static in Scheme, with hiding with nesting • E.g., an inner let hides an outer let for same symbol name

  7. Today’s Studio Exercises • We’ll explore ideas from Scott Chapter 10.1-3 • Looking at basic functional programming features in C++ • Using them in ways similar to Scheme/Lisp features • Today’s required exercises are again in C++ • Please take advantage of the on-line tutorial and reference manual pages that are linked in the lab machines • As always, please ask us for help as needed • When done, email your answers to the course account with subject line “Functional Programming Studio I” • Send to cse425@seas.wustl.edu

More Related