CPSC449 Midterm Questions

0.0(0)
Studied by 3 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/36

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:36 AM on 10/25/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

37 Terms

1
New cards

What is a Program?

Must be Executable by a computer

2
New cards

What kind of Programming Language is C?

C is an imperative language.

3
New cards

In imperative programming languages, what is the scope of variables

Variables are local to block/function/subroutine

4
New cards

What is functional programming?

Evaluation of expressions rather than running commands

5
New cards

Do pure functional languages include loops?

NO. keyword is PURE. they use recusion or higher-order functions

6
New cards

Can you convert Imperative functions into Haskell?

Yes, using recursion and higher order functions

7
New cards

what does the “.” operator do in Haskell? How many parameters does it take?

The dot operator is the composition function. (f((g)x). It takes 3 parameters: the input, the function f, and the function g.

8
New cards

What kind of evaluation does Haskell do? What does it do?

Haskell does “lazy” evaluation. It evaluates the same as PEMDAS/BEDMAS, doing the smallest operation that it needs to do.

9
New cards

What happens to your programs in GHCi if you quit the program?

it disappears, making you lose your functions

10
New cards

What format should modules and filenames be?

ALWAYS starts with a capital letter. Correct Declaration:
MyModule.hs
import MyModule

11
New cards

What format should subdirectories be?

Also must start with a capital letter:
import myModule

import ModSub.SubModule

12
New cards

What do literals evaluate to (ex. ghci> True)

itself! Literals evaluate to themself. In Haskell, literals have types determined by context (5 can be any type, instance of Num)

13
New cards

if-then-else statements in imperative languages can be represented in Haskell using…?

Guards!

14
New cards

in ASCII, digits 0 to 9 occupy a code block of numbers __ to __

48 to 57

15
New cards

to “infix” a function, what do you do? (ex: 1 plus 1)

backticks! 

16
New cards

How do you make a Ternary Operator (z ? x : y) into an if-else?

`if z, do x. else, y.

17
New cards

write the function between, if intended usage is to find the “middle” element

between :: Int -> Int -> Int -> Bool
between x y z
	| x >= y && y >= z = True
	| x <= y && y <= z = True
	| otherwise        = False

18
New cards

What is Mutual Recursion?

When two programs call each other recursively
Example:

isEven :: Int -> Bool
isEven 0 = True
isEven n = isOdd (n - 1)

isOdd :: Int -> Bool
isOdd 0 = False
isOdd n = isEven (n - 1)


19
New cards

In haskell, how are lists stored in code?

a linked list! unlike contiguous memory in C/C++/Java, Haskell has poor data locality (worst case O(n) time)

20
New cards

What do the list functions (head, tail, take, drop) do?

head (returns the first element)

head [2, 3, 4, 5] —- returns 2

tail (returns all elements except head)

tail [2, 3, 4, 5] - - returns [3, 4. 5]

take (returns with the first n elements)

take 3 [2, 3, 4, 5] -- returns [2, 3, 4]

drop (returns the list after removing first n elements)

drop 3 [2, 3, 4, 5] -- returns [5]

21
New cards

what do the list functions length, : , ++ , and !! do?

length (returns number of element)

length [1, 2, 3, 4, 5] -- returns 5

: (adds a single element to the front)

3:[2,3] -- returns [3, 2, 3]

++ (join two lists CONCAT)

[1, 2, 3]++[4,5,6] -- returns [1, 2, 3, 4, 5, 6]

!! (gets the nth element (starting from 0). O(n) time)

[14, 7, 3] !! 1 -- returns 7

22
New cards

what does reverse, zip, unzip, zipWith, and sum do?

reverse (reverses order of list)

reverse [3, 2, 1] -- returns [1, 2, 3]

zip (takes pair of lists into list of pairs)

zip [1, 2, 3] [4, 5, 6] -- returns [(1, 4), (2, 5), (3, 6)]

unzip (undoes a zip)

no example needed hopefully

zipWith (does an operation between corresponding elements in a list)
zipWith (+) [1, 2, 3] [4, 5, 6] -- returns [4, 4, 4]

sum (sums elements of a list

sum [4, 5, 6] -- returns 15

23
New cards

can lists be used as parameters?

yes!!

isEmpty :: [a] -> Bool
isEmpty a = 
	case a of
	[] -> True
	_ -> False

24
New cards

What is the general form of a list comprehension? what part is the generator? what part is the predicate (condition that filters elements)

[ Expression involving a variable | var <- old list, condition, condition]

generator would be the var <- old list 
predicate would be the conditions

25
New cards

what is where?

creates local bindings. Ensure proper indentation for correct compilation

lstFactors n :: [x | x<- [1..n], isFactor x n]
	where
	isFactor x n = n `mod` x == 0

26
New cards

What is considered a “Higher Order Function”?

When it takes 1 (or more) functions as a parameter, and returns a function.

27
New cards

What is a Lambda Function?

A local function defined in brackets with form:
<arg-1> .. <arg-n> -> <expression>

example:
\x y -> x + y

the arrow separates parameters from the function body

28
New cards

can lambda functions include other functions inside it?

YES
y = filter (\x -> (mod) x 2 == 0) [1..10]

29
New cards

When using polymorphic functions, what do you need to ensure?

That the expression has a valid type

30
New cards

What is “Folding”?

It reduces a list to a single value by applying a function between values

foldr (with empty list, returns inital value). Takes 3 parameters, a function f, a value z, and a list xs.

foldr (+) [1..5] -- is (1 + (2 + (3 + (4))))

foldl (folds from left)
foldl (-) [1..5] -- is ((((1) - 2) - 3) - 4)

foldr1 (folds right, but errors when applied to an empty list)

foldr (+) [] -- throws error

map uses fold to compute its output!

31
New cards

What are the differences in type between foldr and foldr1?

foldr1 type:
foldr1 :: (a -> a -> a) -> [a] -> a

foldr type:

foldr :: (a -> b -> b) -> b -> [a] -> b

NOTE THE DIFFERENT TYPES a and b!!!! means accumulator and final result can be different type than list elements.

32
New cards

How do you change foldr1 to foldr?

add an additional argument to foldr1, and return that if empty. 

33
New cards

Recall that snoc appends an element to the end of a list

snoc x xs = [xs] ++ [x].
why does it put brackets around the x?

you can’t concatenate a list with an element!

34
New cards

how can foldr be used to sort a list? (specifically, insertion sort!)

insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x (y:ys)
	| x > y     = insert x ys
	| otherwise = x : y : ys
iSort :: [Int] -> [Int]
iSort xs = foldr insert [] xs

35
New cards

Recall that flip will flip the arguments. Does the following function myFlip represent a composite function?

myDouble :: Int -> Int -> Int
myDouble m n = 2*m*n

myFlip :: (a -> b -> c) -> (b -> a -> c)
myFlip f = \y x -> f x y

main = do
	print ((myFlip myDouble) 1 2)

No. the lambda function returns a different function, it doesn’t combine two functions. It is a higher order function.

36
New cards

what other word exists (in other programming languages) for fold?

reduce . Works like foldl1.

37
New cards

There exists list comprehension functions from Python (filter, map, reduce)

convert the following Python code into Haskell":

squared = [x * x for x in [1, 2, 3, 4, 5]

squared :: Int -> [Int] -> [Int]
squared a xs = map (\x -> x*x) xs

many other ways to solve it, this is just 1.

Explore top flashcards

flashcards
Cô Yến 5/12/2024
22
Updated 480d ago
0.0(0)
flashcards
EXAM 2 - part 6
22
Updated 251d ago
0.0(0)
flashcards
Einheit 1 Freunde
75
Updated 229d ago
0.0(0)
flashcards
Biology Honors Evolution
51
Updated 1096d ago
0.0(0)
flashcards
Matiekos egzas
73
Updated 819d ago
0.0(0)
flashcards
Livy 2.10 Vocab
20
Updated 1215d ago
0.0(0)
flashcards
Cô Yến 5/12/2024
22
Updated 480d ago
0.0(0)
flashcards
EXAM 2 - part 6
22
Updated 251d ago
0.0(0)
flashcards
Einheit 1 Freunde
75
Updated 229d ago
0.0(0)
flashcards
Biology Honors Evolution
51
Updated 1096d ago
0.0(0)
flashcards
Matiekos egzas
73
Updated 819d ago
0.0(0)
flashcards
Livy 2.10 Vocab
20
Updated 1215d ago
0.0(0)