Clojure's Composite Data Structures (L16) Flashcards

1
Q

What are Clojure’s 4 primary data structures?

A

Maps, vectors, lists, sets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Clojure maps are equivalent to which Python datastructure?

A

Dictionaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are empty maps defined?

A

{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In practice, strings are used as Clojure keys.

A

False - keywords (:label) are often used as map keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Are Clojure’s :keywords case sensitive?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why are :keywords used over strings as map keys?

A

Faster internal lookups.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What Clojure function is used to perform map lookups?

A

get

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write 2 different ways in which the :key keyword is used to obtain a value from a map labelled m1.

A

(get m1 :key)
(:key m1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What function is used to concisely retrieve values from nested maps?

A

get-in

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the following line do?
(get jobs :bill “x”)

A

Returns :bill’s value in the jobs map, or returns “x” if not found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can non keyword keys also be used in the following manner?
(:bill jobs “x”)

A

No, only keywords may because as function invocations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the following return?
(keys m1)
(vals m1)

A

A list of m1s keys and values, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What Clojure function can be used to check for the existence of a key in a map?

A

(contains? m1 :k)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When are commas used in Clojure?

A

Commas are considered to be whitespace. They may be used to make code slightly more readable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Vectors in Clojure are 0-indexed sequences.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In what two ways can vectors be created in Clojure?

A

[1 2 3]
(vector 1 2 3)

17
Q

What functions are used to retrieve vector data at a given index?

A

get or nth.

18
Q

Clojure generates an error when access to a vectors out of bounds index is attempted.

A

False - nil is returned instead.

19
Q

Functions can be put in Clojure vectors.

A

True.

20
Q

In what two ways can lists be created in Clojure?

A

‘(1 2 3)
(list 1 2 3)

21
Q

Below is a valid list.
‘(+ 2 2)

A

True.

22
Q

Below is a valid list.
(2 2 2)

A

False.

23
Q

Clojure lists have indexing.

A

False.

24
Q

What functions can be used to access list elements by their position?

A

nth, but not get.

25
Q

Clojure generates an error when access to a lists out of bounds position is attempted.

A

True.

26
Q

What is generally faster, nth on a list or get on a vector?

A

get on an unmodified vector is O(1), whereas nth on a list is O(n).

27
Q

In what two ways can sets be created in Clojure?

A

#{1 2 3}
(hash-set 1 2 3)

28
Q

Which Clojure function removes the duplicates from a set?

A

There aren’t any, as sets will automatically discard duplicates.

29
Q

How can we create sets from existing lists and vectors?

A

(set ‘(1 1 2 2)) &
(set [1 1 2 2])

30
Q

What are the three ways of searching for an element in a set?

A

(get s1 val)
(:val s1)
(contains? s1 val)

31
Q

What does the following return, if s1 is a set that doesn’t contain 4?
(get s1 4)

A

nil

32
Q

What does the following return, if s1 is a set that contains :val?
(:val s1)

A

:val (it returns itself).

33
Q

What Clojure function adds a value to a sequence?

A

conj

34
Q

(conj [1 2 3] 4) returns [1 2 3 4].

A

True.

35
Q

(conj ‘(1 2 3) 4) returns ‘(1 2 3 4).

A

False - it returns ‘(4 1 2 3).

36
Q

What is returned by the following?
(assoc [1 2 3] 1 0)

A

[1 0 3]

37
Q

What happens when a “change” is made to a vector?

A

A new path-like data structure is created, which reference the original vector’s (tree) indexing structure.

38
Q

What is the complexity of accessing an index on a modified vector?

A

O(log n)

39
Q

If Clojure’s vector’s were augmented with index structures that weren’t kept balanced, would access time still be logarithmic?

A

No.