You are on page 1of 6

CSC305 – PROGRAMMING PARADIGMS

SCHEME: LAB 1

NAME : ZIYAD ZAHIDIN BIN YUSRI AMRI


STUDENT : 2021894702
NO.
GROUP : RCS1105A

===== LESSON 1: ARITHMETIC OPERATIONS =====

Scheme employs prefix notation even for common arithmetic operations

1 - Convert the following arithmetic expressions into Scheme expressions and evaluate them:

Example: 1.5 - ½  (- 1.5 (/ 1 2))  1.0

a. 1.2 × (2 - 1/3) + 8.7  (+ (* 1.2 (- 2 (/ 1 3))) 8.7)  10.7


b. (2/3 + 4/9) ÷ (5/11 - 4/3)  ( / ( + ( / 2 3 ( / 4 9 ) ) ) ( - ( / 5 11 ( / 4  -1
3)))) 23/87
c. 1 + 1 ÷ (2 + 1 ÷ (1 + 1/2))  (/(+11)(/(+12)(/12)))  1 3/8
d. 1 × 2 × 3 × 4 × 5 × 6 × 7  (* 1 2 3 4 5 6 7)  5040

2 - Convert the following Scheme expressions into arithmetic expression and evaluate them.

a. (+ (+ 2 2) (+ 2 2))  (2 +2) + (2+2)  8


b. (- 2 (* 4 (/ 1 3)))  2 – 4 * (1 / 3)  2/3
c. (* 2 (* 2 (* 2 (* 2 2))))  2*(2 *(2*(2*2)))  32
d. (/ (* (/ 6 7) (/ 7 2)) (- 4.5 1.5))  ((6/7)*(7/2)) / (4.5  1.0
- 1.5)

===== LESSON 2: LIST MANIPULATION =====

The first element of a list is often called the "car" of the list, and the rest of the list is often called the
"cdr" of the list. The “cdr” of a list with one element is (), the empty list.

Example:

(car '(a b c))  a


(cdr '(a b c))  (b c)
(cdr '(a))  ()

Evaluate the given expressions in Scheme:

PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU


Page |1
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.
CSC305 – PROGRAMMING PARADIGMS
SCHEME: LAB 1

a. (car (cdr '(a b c)))  b


b. (cdr (cdr '(a b c)))  (c)
c. (car '((a b) (c d)))  (a b)
d. (cdr '((a b) (c d)))  (c d))

The procedure “cons” constructs list. It takes two arguments. The second argument is usually a list,
and in that case “cons” returns a list.

Example:

(cons 'a '())  (a)


(cons 'a '(b c))  (a b c)

Evaluate the given expression in Scheme:

a. (cons '(a b) '(c d))  ((a b) c d)


b. (car (cons 'a '(b c)))  a
c. (cdr (cons 'a '(b c)))  (b c)
d. (cons (car '(a b c)) (cdr '(d e f)))  (a e f)
e. (cons (car '(a b c)) (cdr '(a b c)))  (a b c)

Other procedures in list manipulation:

Procedure Description Example


list Returns a list of its arguments (list 'a (+ 3 4) 'c)  (a 7 c)
(list)  ()
list? Returns #t if object is a list, (list? '(a b c))  #t
otherwise returns #f (list? '())  #t
length Returns the length of list (length '(a b c))  3
(length '(a (b) (c d e)))  3
(length '())  0
null? Returns #t if object is the (null? '(a . b))  #f
empty list; otherwise (null? '(a b c))  #f
returns #f (null? '())  #t

append Returns a list consisting of (append '(x) '(y))  (x y)


the elements of the (append '(a) '(b c d))  (a b c d)
first list followed by the (append '(a (b)) '((c)))  (a (b) (c))
elements of the other lists. (append)  ()

reverse Returns a newly allocated list (reverse '(a b c))  (c b a)


consisting of the top-level (reverse '(a (b c) d (e (f))))  ((e (f)) d (b c) a)
elements of list in reverse
order.

===== LESSON 3: VARIABLES AND LET EXPRESSIONS =====

Example of Scheme’s let syntactic form:


PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU
Page |2
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.
CSC305 – PROGRAMMING PARADIGMS
SCHEME: LAB 1

(let ((x 2))(+ x 3))  5


(let ((y 3))(+ 2 y))  5
(let ((x 2) (y 3))(+ x y))  5

===== LESSON 4: AND / OR EXPRESSION =====

“and” and “or” are called special form in Scheme.

and

The expressions are evaluated from left to right, and the value of the first expression that evaluates to
a false value is returned. Any remaining expressions are not evaluated. If all the expressions evaluate
to true values, the value of the last expression is returned. If there are no expressions then #t is
returned.

or

The expressions are evaluated from left to right, and the value of the first expression that evaluates to
a true value is returned. Any remaining expressions are not evaluated. If all expressions evaluate to
false values, the value of the last expression is returned. If there are no expressions then #f is returned.

Example:

(and (= 2 2) (> 2 1))  #t

(or (= 2 2) (> 2 1))  #t

Evaluate the given expression in Scheme:

a. (and (= 2 2) (< 2 1))  #f


b. (and)  #t
c. (or (= 2 2) (< 2 1))  #t
d. (or #f #f #f)  #f

===== LESSON 5: EQUALITY AND IDENTITY =====

PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU


Page |3
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.
CSC305 – PROGRAMMING PARADIGMS
SCHEME: LAB 1

Scheme provides three primitives for equality and identity testing:

1. “eq?” is pointer comparison. It returns #t if its arguments literally refer to the same objects in
memory. Symbols are unique ('fred always evaluates to the same object). Two symbols that look
the same are “eq”. Two variables that refer to the same object are “eq”.
2. “eqv?” is like “eq?” but does the right thing when comparing numbers. “eqv?” returns #t if its
arguments are “eq” or if its arguments are numbers that have the same value. “eqv?” does not
convert integers to floats when comparing integers and floats though.
3. “equal?” returns true if its arguments have the same structure. Formally, we can
define “equal?” recursively. “equal?” returns #t if its arguments are “eqv”, or if its arguments are
lists whose corresponding elements are equal (note the recursion). Two objects that are “eq” are
both “eqv” and equal. Two objects that are eqv are equal, but not necessarily eq. Two objects that
are equal are not necessarily eqv or eq.eq is sometimes called an identity comparison and equal is
called an equality comparison.

Example:

(eq? 'clam 'clam)  #t


(eq? 10 10)  #t
(eqv? 10 10)  #t
(eqv? 10.0 10.0)  #t
(eqv? 10.0 10)  #f
(equal? '(1 2 3) '(1 2 3))  #t

===== LESSON 6: DEFINING SCHEME FUNCTIONS =====

“define” is a special form used to define other functions. Typically, you equate a function symbol and
a set of parameters with a parameterized expression. When you invoke the specified function, you are
really evaluating the expression associated with the function name.

Example 1: A simple function to print a message to the user.

(define (printMessage)
(display "Hello world."))

Example 2: A simple function to convert a weight in kg to pound.

(define (weightConversion weightKG)


(* weightKG 2.2))

PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU


Page |4
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.
CSC305 – PROGRAMMING PARADIGMS
SCHEME: LAB 1

Example 3: A simple function to calculate area of a rectangle.

(define (areaRectangle width height)


(* width height))

1 - Write a simple function in Scheme that is able to calculate the total salary of an employee when
given its current monthly salary and bonus received.

(define (calcTotalSalary monthlySalary bonus)

(+ monthlySalary bonus))

2 - Write a simple function in Scheme that is able to calculate and display the average marks for five
quizzes.

(let ((total (+ q1 q2 q3 q4 q5)))


(display "Quiz score: ")
(display q1)
(newline)
(display q2)
(newline)
(display q3)
(newline)
(display q4)
(newline)
(display q5)
(newline)
(display "Average mark: ")
(newline)
(display (/ total 5.0))))

PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU


Page |5
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.
CSC305 – PROGRAMMING PARADIGMS
SCHEME: LAB 1

PREPARED BY: NYCJ@KPPIM, UITM CAWANGAN PERLIS KAMPUS ARAU


Page |6
SOURCE:
[1] Dybvig, R. K. (2009). The SCHEME programming language. Mit Press.
[2] Hanson, C. (2001). MIT Scheme Reference Manual.

You might also like