Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CSC305 – PROGRAMMING PARADIGMS

SCHEME: LAB 2

===== REVIEW EXERCISE 1 =====

(a) Write a function in Scheme that is able to calculate the area of a rectangle.
[area = width x length]
(b) Write a function in Scheme that is able to calculate the perimeter of a rectangle.
[perimeter = 2 x (length + width)]
(c) Write a function in Scheme that is able to call both functions written in (a) and (b) and
then display their results.

===== LESSON 1: CONDITIONALS =====

The behavior of the conditional expressions is determined by whether objects are true or false.
The conditional expressions count only #f as false. They count everything else, including #t, pairs,
symbols, numbers, strings, vectors, and procedures as true.

Form 1: if predicate consequence [alternative]

Predicate, consequent, and alternative are expressions. An if expression is evaluated as follows:


first, predicate is evaluated. If it yields a true value, then consequent is evaluated and its value is
returned. Otherwise alternative is evaluated and its value is returned. If predicate yields a false
value and no alternative is specified, then the result of the expression is unspecified.

:: Examples ::

(if (> 3 2) 'yes 'no) returns yes


(if (> 2 3) 'yes 'no) returns no
(if (> 3 2)
(- 3 2)
(+ 3 2)) returns 1

Question 1

Write a function in Scheme that is able to display the following message when an age is input by
the user.
Table 1
Age Message
18 and above You are an adult.
Less than 18 You are still a kid.

Question 2

Write a function in Scheme that will receive a word as an input from the user. If the word entered
is “hello”, the function will display ‘o l l e h’ to the user; otherwise it will display the length of
“hello” to the user.

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 2

> (callAWord 'beruang)


5
> (callAWord 'hello)
(o l l e h)
Example of input and output for the function

Form 2: cond clause clause …

Each clause has this form:


(predicate expression ...)

where predicate is any expression. The last clause may be an else clause, which has the form:
(else expression expression ...)

A cond expression does the following:

1. Evaluates the predicate expressions of successive clauses in order, until one of


the predicates evaluates to a true value.
2. When a predicate evaluates to a true value, cond evaluates the expressions in the
associated clause in left to right order, and returns the result of evaluating the
last expression in the clause as the result of the entire cond expression. If the
selected clause contains only the predicate and no expressions, cond returns the value of
the predicate as the result.
3. If all predicates evaluate to false values, and there is no else clause, the result of the
conditional expression is unspecified; if there is an else clause, cond evaluates
its expressions (left to right) and returns the value of the last one.

:: Examples ::

(cond ((> 3 2) 'greater)


((< 3 2) 'less)) returns greater

(cond ((> 3 3) 'greater)


((< 3 3) 'less)
(else 'equal)) returns equal

Question 1

Write a function in Scheme that is able to:


(a) Calculate the average of FIVE (5) marks entered by a student.
(b) Display the following messages based on the calculated average marks.

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 2

Table 2
Average marks Message
>= 80 Excellent
70 - 79 Good
50 - 69 Pass
< 50 Fail

Question 2

Write functions in Scheme that is able to calculate the price after discount based on the given
tables below:

Table 3
Item Name Price Per Unit
Item 1 4.50
Item 2 5.99
Item 3 8.75

Table 4
Price (RM) Discount (%)
More than or equal to 500 10
Between 250 to 500 8
Between 100 to 250 5
Less than 100 2

===== LESSON 2: SIMPLE RECURSION =====

A recursive procedure is a procedure that applies itself.

Question 1

Convert the given factorial formula given in mathematical expression into a function in Scheme.

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 2

Question 2

Write function in Scheme to calculate the length of a list using simple recursion.

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.

You might also like