Karadeniz Teknik Üniversitesi
Bilgisayar Mühendisliği Bölümü
2016-2017 Bahar Yarıyılı
Fonksiyonel Programlama Final Sınavı
Tarih: 6 Haziran 2017 Salı Süre: 120 dakika
Answer the following questions for postfix expressions
1. Write a function that generates a sequence of random numbers less than 10.
getRandInt :: [Int]
2. Write a function that takes the number of operators as an argument and randomly generates
arithmetic expressions in postfix notation, using the elements in the list returned by
getRandInt to select an operator.
genPostfix :: Int -> [String]
3. Write a function that converts arithmetic expressions into the following data type.
data E = E E E (Int->Int->Int) | N Int
postE :: [String] -> E
4. Write a function that evaluates the elements of type E (use the integer divison for the "/"
operator).
evalE :: E -> Int
5. Write a function that converts the elements of type E into aritmetic expressions in infix
notation.
toInfix :: E -> String
Some examples for the function outputs.
getRandInt
-> [3,0,2,5,9,7,5,4,6,8,4,2,5,6, ...]
genPostfix 3
-> ["4","3","5","+","2","-","*"]
postE ["4","3","5","+","2","-","*"]
-> E (N 4) (E (E (N 3) (N 5) (+)) (N 2) (-)) (*)
evalE (E (N 4) (E (E (N 3) (N 5) (+)) (N 2) (-)) (*))
-> 24
toInfix (E (N 4) (E (E (N 3) (N 5) (+)) (N 2) (-)) (*))
-> "(4)*(((3+5)-2))"