0% found this document useful (0 votes)
2 views

CSC305_FunctionalProgrammingBriefNotes

The document provides an overview of functional programming concepts, particularly in languages like Scheme, Haskell, and LISP. Key topics include lambda calculus, expressions, functions, data abstraction, lazy evaluation, and various operations on lists and data structures. It also includes examples of recursive functions, conditionals, and exercises for converting C++ code into Scheme.

Uploaded by

2021870426
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC305_FunctionalProgrammingBriefNotes

The document provides an overview of functional programming concepts, particularly in languages like Scheme, Haskell, and LISP. Key topics include lambda calculus, expressions, functions, data abstraction, lazy evaluation, and various operations on lists and data structures. It also includes examples of recursive functions, conditionals, and exercises for converting C++ code into Scheme.

Uploaded by

2021870426
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CSC305 – PROGRAMMING PARADIGMS

FUNCTIONAL PROGRAMMING
Concepts based on Lambda calculus (Mathematical methods)
(define (f1 x y) (+ x y))
(define f2 (lambda (x y)(+ x y)))
(define (f3 x y)(/ (+ x y)(- x y)))
(define f4 (lambda( x y)(/ (+ x y)(- x y))))
Key concepts are (1) expressions, (2) functions, (3) parametric
polymorphism [enables a function to operate on
values of a family of types]→ generic function

(4) data abstraction and (5) lazy evaluation


[means that we evaluate the actual parameter
when the argument is first needed]

Lazy evaluation
int main()
{
int n = 6;

cout << sqr(n+1);


}
int sqr(6+1)
{
return ((6+1)*(6+1));
}
Purpose Computation by methods or functions (predefined
or user defined)
Language Scheme, Haskell, LISP
Special No loop, so recursive function is needed
Notation Cambridge-prefix
Comments ;
Parenthesis > (+ 3 4)
7
> (+ (- 5 2)(* 3 2))
9

1
define > (define PI 3.142)
> PI
3.142
> (define X 10)
> X
10
> (define Y 'Jamal)
> Y
Jamal
> (define suka '(merah hijau hitam))
> suka
(merah hijau hitam)
> (define suka (quote(merah hijau hitam)))
> suka
(merah hijau hitam)

quote and apostrophe ‘ > (quote(A B C))


(A B C)
> (quote A)
A
> (define x '(A B C))
> x
(A B C)

Data structure, lists > (define numbers '(1 2 3 4 5))


define > numbers
(1 2 3 4 5)
> (define char '(a b c d e))
> char
(a b c d e)
> (define kosong '())
> kosong
()

cons - constructing a list (only two arguments)


- first argument can be an atom or lists
- second argument can be a list
> (cons 8 9)
(8 . 9)
> (cons 8 '(9))
(8 9)
> (cons '(8) '(9))
((8) 9)
> (cons 8 '(9 10 11))
(8 9 10 11)
> (cons '(8 9) '(10 11 12))
((8 9) 10 11 12)

car and cdr - CAR returns the first element of the list
- CDR returns the remainder elements of the list

> (car '(1 2 3 4))


1
> (cdr '(1 2 3 4))
(2 3 4)

2
> (car '(1))
1
> (cdr '(1))
()
>(cdr(cons '(adik abang) '(kakak)))
(kakak)
> (car(cons '(adik abang) '(kakak)))
(adik abang)

list - takes a variable number of arguments and


construct a list consisting of those arguments

> (list '(a b) 'c 'd '(e f))


((a b) c d (e f))
> (list '(a b c) 2 3 '(3 4) 'd)
((a b c) 2 3 (3 4) d)
> (list 3 8 9)
(3 8 9)

append - concatenates the second list onto the end of


the first list
> (append '(1) '(2 3))
(1 2 3)
> (append '(aminah) '(binti bakar))
(aminah binti bakar)
> (append '(10 15) '(8) '(6 10 14))
(10 15 8 6 10 14)

type predicate > (define (f1 x) (+ x 2))


> (f1 2)
4> (procedure? f1)
#t
> (define senarai '(a b c))
> (null? senarai)
#f
> (zero? 0)
#t
> (odd? 5)
#t
> (even? 8)
#t
> (boolean? (> 9 3))
#t
> (number? 9)
#t
> (pair? '(a b))
#t
> (pair? '(a))
#t
> (pair? '())
#f
> (symbol? '^)
#t
> (symbol? '9)
#f

3
> (positive? 100)
#t
> (positive? -100)
#f

relational operators > ( > 9 3)


#t
> (< 9 3)
#f
> (>= 9 9)
#t
> (<= 9 4)
#f
> (= 9 9)
#t
> (define x '(uitm))
> (define y '(UITM))
> (equal? x y)
#f

math operations > (max 9 3)


9
> (max 9 3 2 4)
9
> (min 9 3 2 1)
1
> (remainder 9 4)
1
> (modulo 9 5)
4
> (expt 5 2)
25
> (sqrt 36)
6

processing operations > (length '(uitm di hati ku))


4
> (length (append '(1 2 3) '(a b c)))
6
> (reverse '(a b c d e))
(e d c b a)
> (reverse '(1 2 3 4 5 6 7 8 9 10))
(10 9 8 7 6 5 4 3 2 1)
member List the number found and the remainder number
from the list. If not found will return #f.
> (member 4 '(1 2 3 4 5))
(4 5)
> (member 1 '(2 3 4 5))
#f

Formula conversion Opt1 : (define (sum x y)(+ x y))


From math equations to Opt2 : (define sum (lambda (x y)(+ x y)))
scheme expressions
(define (f1 x y) (+ x y))

4
(display "Enter first number : ")
(define x(read))
(display "Enter second number : ")
(define y(read))
(define result(f1 x y))
(display "The summation of ")
(display x)
(display " and ")
(display y)
(display " is ")
(display result)
(define (multiply x y z)(* x y z))

Substitute the formula


(define (f1 x y)(+ x y))
(define (f2 x y)(- x y))
(define (f3 x y)(/ (f1 x y)(f2 x y)))
(display "Enter first number : ")
(define x(read))
(display "Enter second number : ")
(define y(read))
(define result(f3 x y))
(display "The result is ")
(newline)
(display result)

(define currentYear 2023)


(define (age birthYear)(- currentYear
birthYear))
(display "Enter year birth : ")
(define birthYear(read))
(define result(age birthYear))
(display "My age is ")
(display result)

condition, if, cond > (define (f1 x y)


(if (> x y) (display "x greater than x")
(display "y greater than x"))
)
(define (compare x y)
(if (> x y)(x) y))
(display "Enter the first number :")
(define x (read))
(display "Enter second number : ")
(define y (read))
(define result(compare x y))
(display "The larger number is ")
(newline)
(display result)

> (f1 7 8)
y greater than x

5
> (define (f2 x) (cond ((= x 1) (+ x 3))
((= x 2) (+ x 2))
(else (+ x 1))))
> (f2 3)
4

> (define (f3 x) (cond ((= x 1) (+ x 3))


((= x 2) (+ x 2))
((> x 2) (+ x 1))))
> (f3 4)
5

> (define (funct1 marks)


(cond ((and (>= marks 0)(<= marks
20))(display "F"))
((and (>= marks 21)(<= marks
40))(display "D"))
((and (>= marks 41)(<= marks
60))(display "C"))
((and (>= marks 61)(<= marks
80))(display "B"))
((and (>= marks 81)(<= marks
100))(display "A"))
))

(define (calPrice qtty)


(cond ((and (> qtty 10) (< qtty 20)) (* qtty
20))
((and (>= qtty 20) (< qtty 30)) (* qtty
18))
((>= qtty 30) (* qtty 16))
(else (* qtty 24))))

(display "Enter quantity of item that you want


to purchased ")
(define qtty(read))
(define out(calPrice qtty))
(display "The total price is RM ")
(display out)
switch….case > (define (f4 x)
(case x ((A a) (display " I press A"))
((B b) (display " I press B"))
((C c) (display " I press C"))
(else (display " I press
others"))))
> (f4 'a)
I press A

> (define (f1 x y)


(case x ((1) (* y 20))
((2) (* y 30))
((3) (* y 40))
(else (display "wrong option"))))
> (f1 2 4)
120

6
> (f1 7 3)
wrong option

>(define (ticketPrice des qty)


(case des ((1) (* qty 60))
((2) (* qty 45))
((3) (* qty 20))
(else (display "wrong code of
destination"))))
> (ticketPrice 2 2)
90
> (ticketPrice 5 4)
wrong code of destination

> (define (f1 option qty)


(case option ((1) (* qty 3))
((2) (* qty 15))
((3) (* qty 4.5))
((4) (* qty 6.50))
(else (display "wrong option"))
)
)
> (f1 3 4)
18.0
> (f1 5 10)
wrong option

recursive function - A recursive function is a function that calls


itself.
> (define (f5 n)
(if (= n 0) 1
(* n (f5 (- n 1)))
))
> (f5 5)
120

1 + 2 + 3 + 4 + 5 = 15
Stopper = n=1 return 1
n + f(n-1)

(define (summ x)
(if (= x 1) 1
(+ x (summ (- x 1)))))

int sum (int x)


{
if (x == 1)
return 1;
else
return (x + sum(x-1));
}
apply read for input > (define f6 (read))
20
> (define r (read))
25

7
> (define s (read))
30
> (+ s r)
55
let – to locate value as > (let (( a 100) (b 50)) (+ a b))
local variable 150

> (let ((x1 2) (y1 3)) (* x1 y1))


6

8
Exercise 1

Exercise 2

9
Exercise 3

10
Exercise 4

11
Exercise 5

Convert the following C++ Language into Scheme Language.

char symbol;
int numl, num2;
cin » symbol » numl » num2;

switch (symbol)
{
case '+': cout « numl + num2;
break;
case '*': cout « numl * num2;
break
case '-': cout « numl - num2;
break
}

Exercise 6
Convert the following recursive function in C into Scheme Language.

int funct1(int n)
{ if (n == 1)
return 1;
else
return (n*n*n + funct1(n-1));
}

12

You might also like