Download - Free Documents

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Combinatory logic wikipedia , lookup

Lisp (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Tail call wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Lambda calculus wikipedia , lookup

Currying wikipedia , lookup

Anonymous function wikipedia , lookup

Lambda lifting wikipedia , lookup

Standard ML wikipedia , lookup

Transcript
Content
y Functional Programming
Introduction y Scheme y Questions on scheme
y
Functional Programming
y Functional programming language is based on mathematical functions. y functional
languages are applicative because they achieve their effect by the applications of functions y
In purely functional programming, the concept of assignment does not exist. y In function
programming there is no problem of side affects.
y Advantage
yy
It doesnt have aliasing problem. It can be easily used for parallel machines.
y Disadvantage
y
There is no dedicated hardware, generally implemented with interpreters.
Scheme
y Scheme is dialect of Lisp. y It is a pure functional programming language. y It was
developed in MIT in by Sussman and
Steele. y Scheme is generally used for teaching purposes. y It uses static scoping y Scheme
permits two types of objects,
yy
Atoms Lists
y Atoms can be either,
y Numeric atoms
. y Quoted string atoms Any String
y List is a sequence of atoms separated by blanks and
parentheses. a b c d y Simple list is a list that doesnt have a sublist.
eg a b c
Numeric atoms and Primitive Functions
y Primitive arithmetic functions such ,, and / are
available in scheme. y Scheme can perform arithmetic on the contents of atoms.
y
y Scheme does not have any precedence rules for its
math operations instead to control the order of computation by using parentheses.
A function call is written as
operator arg arg argn For example means
The operator and other arithmetic operators can take any number of arguments.
The same as
y qoute function causes the interpreter not to evaluate
the data, return the data without a change. quote becomes a list we can use A B instead of
quote A B y Comments begin with a semicolon and continue until the end of line. define pi .
bind a variable to a value y Scheme ignores the distinction between uppercase and lower
case. y Names in scheme can have letters, digits, special characters except parenthesis they
must not begin with a digit.
Covert these mathematical functions to scheme
y In scheme we write this like this. o
y b b ac a o / b sqrt sq b a c a y Define functions by lambda expression. o Cube x
x x x x y OR else can define like this. o Define cube x x x x
Functions
y To define a function,
Define functionname parameters expression
y Output functions,
Display expression or NEWLINE
yIF Condition
IF predicate thenexpression elseexpression Define factorial n If n n factorial n
yCOND function
this is a multiple selector based on mathematical expressions Cond predicate expression
predicate expression .. predicaten expression Else expression
Eg Define Compare x y Cond gt x y x is greater than y lt x y x is lesser than y Else x and y
are equal
Use of CAR and CDR
y CAR function
y Returns the first element in a given list
eg Car A B C Car A B C D
returns A returns A B
y CDR function
y Returns the remainder in a given list after the car has been
removed eg Cdr A B C Cdr A B C D
returns B C returns C D
yCONS function
yy
Builds a list from two arguments, first is either an atom or a list , the second is usually a list.
Insets its first parameter as the new car of its second parameters.
Cons A returns A Cons A B C returns A B C Cons Car lis Cdr lis out put is identical to lis
Cons A B returns A.B
yLIST function
y
Constructs a list from a variable number if parameters.
List apple orange grape returns apple orange grape
yEQ function
y
Takes two symbolic atoms as parameters .It returns T if both parameters are the same.
Other wise it returns F.
EQ A A returns T EQ A B returns F EQ A A B returns F
yEQV Function
yy
Test two atoms for equality when it is not known whether they are symbolic or numeric . EQ
or is faster than EQV
yLIST Function
y
Returns T if its single argument is a list F otherwise
yNULL Function
y
Tests if its parameters to determine whether it is empty and returns T if it is, F other wise.
Null A B returns F Null returns T Null A returns F Null returns F
Questions
Write a scheme function for Eg. Define factorial x cond x x elsex factorial x
factorial.
Base case Stop at this point
Recursion is used here
Write a scheme function for Fibonacci.
Eg. Define fab n cond n else fib n fib n
y Write a function in scheme called sum that computes the addition of numbers to n.
define sum lambda val cond val else val sum val Eg. sum Because
y Write a scheme function compare x y.This compares two numeric types atoms and
displays the appropriate phrase.
Eg. compare ygtx define compare x y cond lt x y Display x is less than y gt x y Display x is
greater than y else Display x and y are equal
y Write an exponential function in scheme.
Eg. exp x y
yx
define exp x y cond y x y else x exp x y
y This is a scheme function to get the SUM of the list.
define sum list if null list car list sum cdr list y Define a function to return list of numbers that
squares all the numbers in the list. define mysquaremember list if null list cons car list car list
mysquaremember cdr list
y Scheme function called member takes an atom and a
simple list, It returns T if the atom is in the list, otherwise it returns F. Write scheme code to
implement this function. y e.g member is scheme is simple function language returns T.
define member atom list
condnull list F atom car list T else member atom cdr list
y Define a function named addnums which returns a new
list of numbers containing all the numbers in the original list increased by and none of the
non numbers. y Hint Assume that number list is taken from numberP function. Define
addnums list condnull list numberP car list cons car list addnums cdr list else addnumscdr list
y Define length function.
Eg.
Define length list cond null list else length cdr list
y Write a recursive function to return the last item in a
given list. Hint make use of the length function define last list cond length list car list else last
cdr list
y Define a Scheme function named totalreverse that will take a list as the parameter and
returns a list in which all elements are reversed including the nested lists. For example if the
function totalreverse is applied on the list a b c d e the resulting list would be e d c b a. You
may assume that the definitions for the list and append functions are already available.
y Define totalreverse list
cond null list list car list cons totalreversecdr listtotalreversecar list else constotalreverse cdr
list car list
Write a scheme function to calculate how many non zero values are there in a given list.
define nonzero l cond null l else if car l nonzero cdr l e.g. nonzero
y Write a scheme function to append two lists and return
a list. Eg. ,, ab, c,d ,,,ab,c,d Define append list list cond null list list else conscar list append
cdr listlist
y Write a function to interleave two lists and return a
list. Eg. myinterleave a b c d Output should be like this. a b c d Define myinterleave list list if
null list list cons car list myinterleave list cdr list
y Give the scheme definition of a function called insert
rightst that takes arguments. The function searches the first occurrence of the second
argument in the input list and inserts the first argument to its right.
Define insertrightst a b list Cond car list b Cons car list Cons a cdr list else Cons car list
insertrightst a b cdr list Eg insertrightst not does my dog does have fleas returns my dog
does not have fleas
Group members,
y DITM
P.H.C.L.Premachandra y DITM D.A.Y.Thantriwattage y DITM J.H.A.D.K.Jayasekara y DITM
S.D.Gunaratne
Thank you