CSCE 314 Lee Final Exam Review Spring 2022

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/24

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

25 Terms

1
New cards

Type of ['a','b','c'] :: ?

[Char] - a list with Chars; elements of a list must be of the same type.

2
New cards

Type of ('a','b','c') :: ?

(Char, Char, Char) - a tuple with three components, where each component is Char. A tuple can have different types, so it encodes the number of components as well.

3
New cards

Type of [(False, '0'),(True, '1')] :: ?

[(Bool, Char)] - a list with tuples, each tuple is type (Bool, Char).

4
New cards

Type of ([False,True],['0','1']) :: ?

([Bool],[Char]) - a tuple with two components, one a list of Bools and the other a list of Chars.

5
New cards

Type of [tail, init, reverse] :: ?

[[a] -> [a]] - A list of functions, where each function is a list handling function of type [a] -> [a].

6
New cards

write `min` using if conditional

min x y = if x <= y then x else y

7
New cards

write `min` using guards

min x y

| x <= y = x

| otherwise = y

8
New cards

[1,[2,3],4] is NOT a valid Haskell list

The elements of a list must all be the same.

9
New cards

Given the Haskell function definition

f n = n * n

what is the result of:

f 2 + 1

(f 2) + 1

(2 * 2) + 1 = 5

10
New cards

What is the result of the following Haskell expression:

takes 3 [3..10] !! 2

takes 3 [3..10] = [3,4,5]

[3,4,5] !! 2 = 5

(!! means get at index of)

11
New cards

The Haskell expression `take 3` has type:

[a]->[a] -

12
New cards

THESE ALL ARE TRUE:

In Haskell, ALL type checking and inference is done at runtime.

Haskell's pureness is what helps guarantee referential transparency.

Haskell functions are NOT all total functions.

Miranda is a lazy functional programming language developed before Haskell, so Haskell CANNOT be the first lazy functional programming language

13
New cards

Given the following definitions:

f x = 3

g x = g (x-1)

what is f (g 0)

3 - Since f of any x is 3, we don't even evaluate the argument `g 0` because of Haskell's laziness.

14
New cards

What is the result of

(head . tail) [1..]

head (tail [1..]) = head [2..] = 2

15
New cards

What is the type of the following expression: [([]++)]

[[a] -> [a]] - the given expression is a list of a section of the append (++) operator with its left operand list ([]) already provided. The type of section ([] ++) is [a] -> [a] since it needs the right operand list as the argument to put the two lists together, which will be the final result.

16
New cards

Which of the following statements about Haskell `foldr` is false?

foldr is a curried function

foldr is a recursive function

foldr is a higher-order function

foldr is a polymorphic function

foldr is an overloaded function

foldr is an overloaded function.

foldr is a curried function - it takes more than one argument possibly at a time.

foldr is a recursive function - its definition involves recursion.

foldr is a higher-order function - it takes function as an argument.

foldr is a polymorphic function - its definition involves type parameters that are instantiated per application.

foldr is an overloaded function - it does NOT involve any overloaded operations that require type constraints.

17
New cards

What is the type of the following Haskell expression? "Hi,":"all!":[]

[[Char]]

18
New cards

Haskell's ___ is what helps guarantee referential transparency.

pureness

19
New cards

Haskell's __ is what helps avoid unnecessary computation and ensures that programs terminate whenever possible

laziness

20
New cards

Haskell's pureness prohibits or confines

side-effects

21
New cards

Consider a function `safetail :: [a] -> [a]` that behaves in the same way as the library function `tail`, except safetail maps the empty list to the empty list, whereas tail gives an error in this case. Define safetail using

a) a conditional expression

b) guarded equations

c) argument pattern matching

a)

safetail xs = if null xs then [] else tail xs

b)

safetail xs

| null xs = []

| otherwise = tail xs

c)

safetail [] = []

safetail xs = tail xs

22
New cards

fz xs = sum [ x | (z,x) <- zip [1..] xs, z==x]

what is fz [1..4]

sum [ x | (z,x) <- zip [1..] [1..4], z==x] = sum [1,2,3,4] = 10

23
New cards

product $ map (div 3) [1,2,3]

= product (map (div 3) [1,2,3])

= product [(div 3 1), (div 3 2), (div 3 3)]

= product [3,1,1]

= 3

24
New cards

fun p = foldr (\x xs -> if p x then x + xs else xs) 0

what is fun (> 0) [-1, -2, 0, 1]

= foldr (\x xs -> if (>0) x then x + xs else xs) 0 [-1,-2,0,1]

let f = (\x xs -> if (>0) x then x + xs else xs)

= foldr f 0 ((:) -1 ((:) -2 ((:) 0 ((:) 1 []))))

= f -1 (f -2 (f 0 (f 1 0))) -- substitute [] with 0 and each (:) with f

= f -1 (f -2 (f 0 ( if 1 > 0 then 1 + 0 else 0)))

= f -1 (f -2 ( if 0 > 0 then 0 + 1 else 1))

= f -1 (if -2 > 0 then -2 + 1 else 1)

= if -1 > 0 then -1 + 1 else 1

= 1

25
New cards

Given the following grammar in BNF

::= | + | -