Clojure Programs (L18) Flashcards
What error is generated if a recursive function never reaches its stopping condition?
StackOverflowError
When can “recur” be used, and what does it do?
Recur can be used when the recursive call is the last expression of a function, and it creates a tail call which saves stack space.
What is the Clojure equivalent to header guards in C, such that an imported namespace is only processed once.
There is no equivalent, as Clojure only processes libraries once by default.
Define the namespace for a file called test.clj.
(ns test)
As there are no public/private keywords, Clojure functions and variables are private by default.
False - they are public by default.
In what way(s) can data be made private in Clojure?
By placing “^:private” between “def” and the data’s label.
In what way(s) can functions be made private in Clojure?
By using “defn-“ instead of “defn”, or by placing “^:private” between “defn” and the function name.
The following is valid if myfunc is defined in test.bar.
(ns test.foo (:require test.bar))
(bar/myfunc x)
False, the function invocation must be fully qualified: (test.bar/myfunc x)
The following is valid if myfunc is defined in test.bar.
(ns test.foo (:require test.bar :as bar))
(bar/myfunc x)
False - syntax error:
(ns test.foo (:require [test.bar :as bar]))
Write code that imports test.bar into test.foo, and make test.bar’s f1 function directly callable (without full qualification).
(ns test.foo
(:require [test.bar :as bar :refer f1]))
How are Java classes imported?
(ns test.foo (:import java.util.Date))
Assume java.util.Date has been imported. Create an instance of this class within Clojure.
(Date.)
Assume java.util.Date has been imported. Invoke the getTime() method from an instance of this class.
(. (Date.) getTime)