Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"in clojure\" Here is an alternative procedural representation of pairs (but we

ID: 3748356 • Letter: #

Question

"in clojure" Here is an alternative procedural representation of pairs (but we’re missing the rest-proc function).
(def cons-proc
(fn [x y]
(fn [m] (m x y))))
(def first-proc
(fn [z]
(z (fn [p q] p))))
Given this definition of the list-proc type: An expression exp is a list-proc if and only if it satisfies
one of the following two conditions:
(a) exp is nil
(b) exp is of the form (cons-proc x y) and y is a list-proc.
complete the following tasks:
(a) For this representation, verify that (first-proc (cons-proc x y)) yields x for any objects x
and y.
(b) Give the definition of rest-proc. Include a complete set of unit tests that demonstrates that you can
create complex lists and traverse them using these procedures.
(c) Write a function (list-proc? lp) that returns true if lp is a list-proc and false otherwise.
(d) Write a function (list-proc-2-string lp) that takes a list-proc as an argument and returns
a string representation of it using the same formatting Clojure uses for normal lists.

Explanation / Answer


(def test-address
  {:street-address "123 Test Lane"
   :city "Testerville"
   :state "TX"})
(meditations
  "Destructuring is an arbiter: it breaks up arguments"
  (= ":bar:foo" ((fn [[a b]] (str b a))
         [:foo :bar]))
  "Whether in function definitions"
  (= (str "First comes love, "
          "then comes marriage, "
          "then comes Clojure with the baby carriage")
     ((fn [[a b c]] (str "First comes " a ", "
                         "then comes " b ", "
                         "then comes " c " with the baby carriage"))
      ["love" "marriage" "Clojure"]))
  "Or in let expressions"
  (= "Rich Hickey aka The Clojurer aka Go Time aka Macro Killah"
     (let [[first-name last-name & aliases]
           (list "Rich" "Hickey" "The Clojurer" "Go Time" "Macro Killah")]
       (apply str (interpose " aka " (cons (str first-name " " last-name) aliases)))))
  "You can regain the full argument if you like arguing"
  (= {:original-parts ["Steven" "Hawking"] :named-parts {:first "Steven" :last "Hawking"}}
     (let [[first-name last-name :as full-name] ["Steven" "Hawking"]]
       {:original-parts full-name :named-parts {:first first-name :last last-name}}))
  "Break up maps by key"
  (= "123 Test Lane, Testerville, TX"
     (let [{street-address :street-address, city :city, state :state} test-address]
       (str street-address ", " city ", " state)))
  "Or more succinctly"
  (= "123 Test Lane, Testerville, TX"
     (let [{:keys [street-address city state]} test-address]
       (apply str (interpose ", " (list street-address city state)))))
  "All together now!"
  (= "Test Testerson, 123 Test Lane, Testerville, TX"
     ((fn [[first-name last-name] {:keys [street-address city state]}]
        (apply str (interpose ", " (list (str first-name " " last-name) street-address city state))))
      ["Test" "Testerson"] test-address)