0% found this document useful (0 votes)
47 views22 pages

Chapter 15

Functional programming languages define functions as expressions with parameters rather than procedures. Some key aspects include: - Functions are defined using a name and parameters, like cube(x) = x * x * x, and applied like cube(2). - Lambda notation, like λ(x) x * x * x, defines anonymous functions. - Functions can be composed, like defining b(x) = f(g(x)) where f(x) = x + 2 and g(x) = 3x. - Functions like mapcar and apply apply a function to each element of a list. - Primitive functions include arithmetic, square root, lists operations, and conditional evaluation.

Uploaded by

Hazem Nizar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views22 pages

Chapter 15

Functional programming languages define functions as expressions with parameters rather than procedures. Some key aspects include: - Functions are defined using a name and parameters, like cube(x) = x * x * x, and applied like cube(2). - Lambda notation, like λ(x) x * x * x, defines anonymous functions. - Functions can be composed, like defining b(x) = f(g(x)) where f(x) = x + 2 and g(x) = 3x. - Functions like mapcar and apply apply a function to each element of a list. - Primitive functions include arithmetic, square root, lists operations, and conditional evaluation.

Uploaded by

Hazem Nizar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Functional Programming Languages

Function

definition are written as a function name, followed by a list of parameters in parentheses, followed by the mapping expression.

Cube (x) = x * x * x ,x is real number Ex: cube(2.0) yields the value 8.0.

Lambda

Notation is used for defining nameless functions.


(x) x * x * x Ex: ( (x) x * x * x) (2) yields the value 8.

= g where is function composition.


Ex: f(x) = x + 2, g(x) = 3 * x Then b is defined as b(x) = f(g(x)), or b(x) = (3 * x) + 2

Apply

to all

Let b(x) = x * x Then (b, (2,3,4)) yields (4,9,16)

Construction

[f, g ]

f(x) = x * x * x, g(x) = x + 3 [ f, g ](4) yields ( 64, 7)

Primitive numeric functions

Basic arithmetic operations +, -, * and /

Examples Expression 42 (* 3 7) (+ 5 7 8) (- 5 6) (- 15 7 2) (- 24 (* 4 3))

Value 42 21 20 -1 6 12

SQRT returns the square root of its numeric parameter.

DEFINE

is used to bind a name to a value

Ex: (DEFINE pi 3.14159) (DEFINE two_pi (* 2 pi))

Or

to bind a name to lambda expression

Ex: (DEFINE (square number) (* number number)) (square 5) displays 25

Output

Functions

(DISPLAY expr) (NEWLINE)

Numeric

predicate functions

Return a Boolean value (true #t or false #f ()) =, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?

Ex:

Factorial function f(n) = 1 if n=0 n* f(n-1) if n>0 Two way selector, If (only 1 predicate is true)

It has three parameters (predicate expr, then_expr, else_expr) Ex: (DEFINE (factorial n) (If (= n 0) 1 (* n (factorial (- n 1))) ))

(DEFINE

( compare x y) (COND ((> x y) (DISPLAY x is greater than y)) ((< x y) (DISPLAY y is greater than x)) (ELSE (DISPLAY x and y are equal)) )

QUOTE

takes one parameter, returns it without evaluation.


(QUOTE A) return A (QUOTE (A B C)) returns A B C = (A B C)

There

are two primitive list selectors:

CAR returns the first element of a given list. CDR returns the remainder of a given list after CAR is removed.

Examples

on CAR

(CAR (A B C)) returns A (CAR ((A B) C D)) returns (A B) (CAR A) is an error because A is not a list (CAR ()) is an error (CAR (A)) returns A

Examples

on CDR

(CDR (A B C)) returns (B C) (CDR ((A B) C D)) returns (C D) (CDR A) is an error because A is not a list (CDR (A)) returns () empty list

(DEFINE

(second lst) (CAR (CDR lst))

(second (A B C))

returns B

CONS builds a list from its two arguments It inserts the first parameter as a new CAR of its second parameter Examples on CONS

(CONS A ()) returns (A) (CONS A (B C)) returns (A B C) (CONS () (A B)) returns (() A B) (CONS (A B) (C D)) returns ((A B) C D) (CONS A B) returns (A . B)

LIST

is a function that constructs a list from a variable number of parameters


(LIST apple orange grapes) Returns (apple orange grapes)

EQ?

takes two parameters, returns #t if both are the same, otherwise () Examples:

(EQ? A A) returns #t (EQ? A B) returns () (EQ? A (AB)) returns () (EQ? (A B) (A B))returns #t

LIST?

Returns #t if its argument is a list and () otherwise Examples:


(LIST? (X Y)) returns #t (LIST? X) returns () (LIST? ()) returns #t

NULL?

tests its parameter to determine whether it is empty list? Examples:


(NULL? (A B)) returns () (NULL? ()) returns #t (NULL? A) returns () (NULL? ( () )) returns () list containing an empty list

Member

takes an atom and a simple list, returns #t if the atom is in list.


(member B (A B C)) returns #t (member B (A C D E)) returns ()

LET

it allows names to be temporarily bound to the values of sub expressions


(alpha 7)) (* 5 alpha))

(LET(

yields 35

Same

as ((LAMBDA (alpha) (* 5 alpha)) 7)

1) Functional Composition

(CDR (CDR (A B C))) -> (CDR (B C)) returns (C) (CAR (CAR ((A B) B C))) -> (CAR (A B)) returns A (CDR (CAR ((A B C) D))) -> (CDR (A B C)) returns (B C) (NULL? (CAR (() B C))) -> (NULL? ()) returns #t (CONS (CAR (A B)) (CDR ( A B))) -> (CONS (A (B))) returns (A B)

2) An apply to all functional form mapcar has two parameters (function, list) It applies the given function to each element of the given list, and it returns a list of the results. Ex: (mapcar (LAMBDA (num) (* num num num)) (3 4 2 6)) Returns (27 64 8 216)

SET!

Binds or rebinds a value to a name.


Replaces the CAR of the list. Replaces the CDR part of a list.

SET_CAR! SET_CDR! Ex:


(SET_CAR! A (X Y Z)) returns (A Y Z) (SET_CDR! (A) (X Y Z)) returns (X (A))

You might also like