Docsity

Exam 1 Sample Solution - Introduction to Computer Science | CIS 252, Exams of Computer Science

Toggle sidebar
bg1
1. (28 points) Suppose that the following definitions are made:
q, t:: Int
q = 10
t = 13
lon, ms :: [Int]
lon = [3, 18, 17, 21, 6, 9]
ms = [ x*10 | x <- lon, odd x, x > 15]
css :: [String]
css = ["abc", "def", "ghij"]
enigma :: [a] -> Int
enigma [] = 5
enigma (a:as) = 10
enigma (b:c:ds) = 33
(a) (18 points) Give the values of the following expressions.
(i). [q..t]
[10,11,12,13]
(ii). [6,16..40]
6,16,26,36
(iii). ms
[170,210]
(iv). snd(3.21,"abc")
"abc"
(v). [t,q]
[13,10]
(vi). [css]
[["abc","def","ghij"]]
(vii). []++css
["abc","def","ghij"]
(viii). enigma css
10
(ix). enigma [[]]
10
(b) (10 points) Give the types of the following expressions (for example, the type of
[False] is [Bool]).
(i). fst("hi", [q])
String (or [Char])
(ii). [ even (length x) | x <- css ]
[Bool]
pf3
pf4

Partial preview of the text

Download Exam 1 Sample Solution - Introduction to Computer Science | CIS 252 and more Exams Computer Science in PDF only on Docsity!

  1. (28 points) Suppose that the following definitions are made:

q, t:: Int q = 10 t = 13

lon, ms :: [Int] lon = [3, 18, 17, 21, 6, 9] ms = [ x*10 | x <- lon, odd x, x > 15]

css :: [String] css = ["abc", "def", "ghij"]

enigma :: [a] -> Int enigma [] = 5 enigma (a:as) = 10 enigma (b:c:ds) = 33

(a) (18 points) Give the values of the following expressions.

(i). [q..t] [10,11,12,13] (ii). [6,16..40] 6,16,26, (iii). ms [170,210] (iv). snd(3.21,"abc") "abc" (v). [t,q] [13,10] (vi). [css] [["abc","def","ghij"]] (vii). []++css ["abc","def","ghij"] (viii). enigma css 10 (ix). enigma [[]] 10 (b) (10 points) Give the types of the following expressions (for example, the type of [False] is [Bool]).

(i). fst("hi", [q]) String (or [Char]) (ii). [ even (length x) | x <- css ] [Bool]

(iii). ([t],enigma ms) ([Int],Int) (iv). concat css String (v). [True]:[] [[Bool]]

  1. (9 points)

(a) (4 points) Give a Haskell expression that has the type ([Char], Bool, Int). ("a string", False, 17) (b) (5 points) Fill in the blanks below to generate the list [(20,30),(40,50),(60,70),(80,90)].

(x10, (x+1)10) , even x, x < 10 [ ____________ | x <- [1 .. 10] ___________________________ ]

  1. (15 points) Zeke’s Unix home directory (/home/cats/zeke) contains two subdirecto- ries: - cis252, which contains the text files hw1.hs, lab2.hs, and hw2.trans, as well as the postscript file syllabus.ps - manifesto, which contains the text file mice.txt, the hidden file .secrets, and the postscript file birds.ps

For each of the following tasks, assume that Zeke starts out in his home directory (thus, each task is completely independent of the others). What series of commands does Zeke need to type to complete each of the following tasks?

(a) Check the queue of the printer felix lpq -Pfelix (b) List all of the files in the directory manifesto ls -a manifesto (c) Print the postscript file birds.ps to the printer yeti lpr -Pyeti manifesto/birds.ps (d) Get a long listing (e.g., information about file permissions and creation dates) of the directory cis ls -l cis (e) Create a subdirectory treats in manifesto and then place a copy of birds.ps into treats

  1. (12 points) Write a Haskell function

double :: [a] -> [a] such that (double xs) returns a list containing two consecutive copies of every element of xs. For example, (double [1,5,3,5]) returns [1,1,5,5,3,3,5,5]. Sample answer:

double [] = [] double (x:xs) = x: x: double xs

  1. (12 points) Write a Haskell function

isDecreasing :: [Int] -> Bool

such that (isDecreasing ns) determines whether or not the list of integers ns is in strictly decreasing order (i.e., each element in the list should be less than its prede- cessors). Special cases: The empty list and any singleton list are considered to be in decreasing order. For example, the function should have the following behavior:

Main> isDecreasing [317,67,12,-3,-5,-10] True Main> isDecreasing [317,67,67,12,-3,-5,-10] False

Sample answer:

isDecreasing [] = True isDecreasing [x] = True isDecreasing (x:y:zs) = (x > y) && isDecreasing (y:zs)

  1. (12 points) Write a Haskell function

myTake :: Int -> [a] -> [a]

such that (myTake n xs) returns a list containing the first n elements of xs; if n is greater than the number of elements in xs, then the entire list xs is returned. You may assume that n is nonnegative. For example, (myTake 3 [2,4,6,8,10]) returns [2,4,6], while (myTake 3 [2,4]) returns [2,4]. Important Note: You must not use Haskell’s built-in functions take or splitAt. Sample answer:

myTake 0 _ = [] myTake n [] = [] myTake n (x:xs) = x : myTake (n-1) xs