1 / 8

Functional Programming

Functional Programming. Functional programming languages are characterized by the use of functions (non-void methods) as the dominant control structure. Functional Languages. APL. CLOS. FP. ML. LISP. Scheme. Scheme. LISP. • circa 1975 by Steele & Sussman.

gaston
Télécharger la présentation

Functional Programming

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. Functional Programming Functional programming languages are characterized by the use of functions (non-void methods) as the dominant control structure. Functional Languages APL CLOS FP ML LISP Scheme Scheme LISP • circa 1975 by Steele & Sussman • circa 1960 by John McCarthy (MIT) • LISP with many extensions • Simple structures: data - list control - selection & recursion • see www.drscheme.org • Usually interpreted (not compiled) • Code and data have the same form (a list) • A typeless language

  2. Scheme EBNF This is a Pure LISP-like subset. <oneList> ( <itemSequence> ) | <dottedPair> <itemSequence> | <oneItem> | <oneItem>  {} <itemSequence> <dottedPair> ( <oneItem> . <oneItem> ) <oneItem><quotedItem> | <unquotedItem> <quotedItem> ' <unquotedItem> <unquotedItem><identifier> | <integerConstant> | <realConstant> | <booleanConstant> | <otherConstant> | <oneList> | <dottedPair>

  3. Scheme EBNF (cont'd) <identifier>is a valid identifier (case insensitive) <integerConstant>is a valid integer literal <realConstant>is a valid real (decimal) literal <booleanConstant> #t |#f <otherConstant>is a string, character, etc. literal NOTE: additional white space is permitted except within constants and identifiers

  4. Lists in Scheme/LISP Lists are essentially singly-linked lists, denoted via parentheses. '(a b c d) '( I (am ((a ghost) (help me)) ) )

  5. Dot Notation Lists are built from 2-part cells • the 1st part points at a list item • the 2nd part points at the subsequent list cell An alternative is a dotted pair (2-part cell consisting of two items) '( x . y ) Note that '(1 2 3 4) is the same as '(1. (2 . (3 . (4 . () ) ) ) ) Avoid dot notation when possible. Null Pointer Notation In Scheme --- In LISP ---

  6. Pure LISP Functions car number of arguments: 1 precondition: argument must be a list of length 1 or more postcondition: result == first item from the list cdr number of arguments: 1 precondition: argument must be a list of length 1 or more postcondition: result == the argument list less the first item cons number of arguments: 2 postcondition: result == dotted pair of items (like prepending first to second list) eq number of arguments: 2 postcondition: result == arguments are equal (Scheme syntax: equals? ) atom number of arguments: 1 postcondition: result == argument is a list (Scheme syntax (negation): list? )

  7. Example Scheme Functions (car '(a b c)) (cdr '(a b c)) evaluates to evaluates to (car '((x y) (z))) (cdr '((x y) (z))) evaluates to evaluates to (cdr '()) evaluates to (cons 'a '(a b)) evaluates to (cons 'x 'y) evaluates to (equal? 'a '(a)) evaluates to (list? 'a) evaluates to (list? '(a)) evaluates to

  8. Quoting Arguments are quoted to denote a literals. 'a is an abbreviation for (quote a) '(a b) is an abbreviation for (list 'a 'b) (cdr (cdr '(isnt that special)) ) (car (cdr '(isnt that special)) ) (car 'jack) (car (car '((isnt that) special)) ) (car (cons 'a '(b c)) ) (cons (cons '1 '(2 3)) (cdr '(4 5 6)) ) (cons (car '(Wow can)) (cons (cdr '(this get)) (cons (equal? 'Messy 'Messy) '() )) )

More Related