FinalExamCLC 2022 Ans
FinalExamCLC 2022 Ans
- Functional Programming uses functions as the basic elements. OOP uses objects/classes as the
basic elements. Student should give an example.
(i) Lambda expression can be used todefine a function without naming. Let give an example
(ii) Curried function receives parameters one-by-one. Let give an example
(iii)Higher-order function receives another function as its parameter. Let give an example
Question 3 (L.O.4): Consider the following Haskell script:
ztn :: ([Int], Int) -> Int
ztn ([], x) = 0
ztn ((a:as), x) = (if a == x then 1 else 0) + ztn(as,x)
Answer the following questions:
(i) The function ztn counts the number of times that x appears in the array
(ii)
ztn [1,2,3] 2 error
ztn [1,2,3,2] 2 error
ztn ([1,2,3],2) 1
ztn ([1,2,3,2],2) 2
Question 4 (L.O.4): There are some ways to define the function:
listinsert :: Int [Int] [Int]
For example: listinsert 3 [1,2,8,9] will return [1,2,3,8,9]. Just follow the knowledges in the
courses to write your script
For example:
removeIndex 0 [7, 5, 3, 8] -> [5, 3, 8]
removeIndex 3 [7, 5, 3, 8] -> [7, 5, 3]
Notice that you must compare the integer number and the length of the list.
Just follow the knowledges in the courses
Question 6 (L.O.3, L.O.4): Implement the game of nim in Haskell (interactive program) as the exampe in
class.
Hint: Represent the board as a list of five integers that give the number of stars remaining on each
row. For example, the initial board is [5,4,3,2,1].
Consider an example of the output of the example program (the numbers after ? are the users’ inputs.
Assume that all users’ inputs are valid, thus you do not need to check the validity of the input):
1: * * * * *
2: * * * *
3: * * *
4: * *
5: *
Player 1 is playing …
Row? 2
How many stars? 4 [[Note: Player 1 will remove 4 stars in row 2]]
How many stars? 2 [[Note: Player 2 will remove 2 stars in row 3]]
[If one player is the winner then displays the winner, for example:
“Player 1 wins!!!”]