Functional Languages A function is an “association of a certain object from one set (the range) with each object from another set (the domain) A function is an expression and its associated values, where the expression provides a method or rule for making the association between domain and range values.
Functional languages Ways of expressing functions –Age(Amalia) = 7 –(Age Amalia) –(age Amalia 7) function / applicative Functions are single valued – for each domain value there is exactly one associated range value.
Functional Languages Distinguishing Features –Programs are a composition of functions –Functions are supported as first-class objects Functions can be arguments to other functions –There are relatively few side effects –Simple semantics –Recursive control prevalent
Functional Languages LISP –Typeless –Primary data structure is the list Scheme – a dialog of LISP –typeless ML = Meta Language –Strongly typed APL – not purely functional –Primary data structure is the array FP
Relations vs Functions Relation is –Ordered association of elements of ten listed as a tuple Function –Given an ordered tuple (argument list of elements) returns a value Composition of functions example (+ (* w x) ( - y z))
First Order Functions A function is first-order if it takes individuals as arguments and returns an individual value –Numbers –Strings –Records –Etc. An individual is a function of order 0
Side Effects A function (f x y z) is said to have a side effect if x,y, and/or z change in the calling environment during application of the function. –GetData (x,y,z) –(GetData Getx Gety Getz) A function defined with all value parameters, and where no assignments are made to global variables, has no side effects.
Clean Semantics Language means what it says Language is not ambiguous Results of a program can be verified
LISP – a functional language History –First major AI workshop summer 1956 – at Dartmouth –John McCarthy – late 50’s – MIT Kleene’s - recursive function theory Alonzo Church’s – lambda calculus –IBM 704 few primitives Punched cards –Good for AI areas of Robotics & knowledge engineering –Standardization attempt: Common LISP
Knowledge Engineering Expert Systems –Public knowledge Textbooks –Private knowledge Heuristics (rules of thumb) –Examples DENDRAL – data analysis MACYSYMA – symbolic calculus EXPERT – medical MYCIN – infectious disease diagnosis & prescription
LISP – a functional language McCarthy’s original LISP – 6 functions cons, cond, car, cdr, eq, atom Today’s LISP – many more functions –Arithmetic –Input –Output
LISP – a functional language Simple Data Type –Atom Number –Use built in hardware for integer & real arithmetic String Structured Data Type –List Cam be empty () Can contain an ordered list
LISP – a functional language LISP manages pointers for you –ordinary Lisp lists are singly linked lists When dealing with recursion –Need a base case (stopping case) –Need a recursive case Common Lisp provides macros for push and pop (xlisp doesn’t)
LISP – a functional language (setf pair (cons ‘a ‘b)) –returns the dotted pair (A.B) –(A.B) is a two part data structure –A is the car of the cons –B is the cdr of the cons