Ch 7 + Hella 4Clojure

Jul 11, 2020


Chapter 7

(type (read-string "map"))
=> clojure.lang.Symbol

Chapter exercises

  1. Use the list function, quoting, and read-string to create a list that, when evaluated, prints your first name and your favorite sci-fi movie.
(eval (list 'println (read-string "'(\"Mani\" \"Event Horizon\")")))

(read-string "(1 + 2 * 3)")
  1. Create an infix function that takes a list like (1 + 3 * 4 - 5) and transforms it into the lists that Clojure needs in order to correctly evaluate the expression using operator precedence rules.
(defn infix
  (if (= (count math) 3)
    (list (second math) (first math) (last math))
    (list (second math) (first math) (infix (drop 2 math)))))

(infix (read-string "(1 + 4 * 3 - 2)"))


I am now upto 83 4Clojure Problems! Big Milestone!

No. 147 Pascal’s Trapezoid

Write a function that, for any given input vector of numbers, returns an infinite lazy sequence of vectors, where each next one is constructed from the previous following the rules used in Pascal’s Triangle. For example, for [3 1 2], the next row is [3 4 3 2].

(defn pascal-trap
  (let [pscl (fn [n] (cons (first n) (map #(apply +' %) (partition-all 2 1 n))))]
    (cons s
          (lazy-seq (pascal-trap (pscl s))))))

No. 146 Trees into tables

(defn t-to-t [m]
  (into {} (mapcat (fn [[k mm]]
                     (for [y mm]
                       [[k (first y)] (second y)])) m)))

No. 51 Advanced Destructuring

(= [1 2 [3 4 5] [1 2 3 4 5]] (let [[a b & c :as d] __] [a b c d]))

[1 2 3 4 5]

No. 96 Beauty is Symmetry Let us define a binary tree as “symmetric” if the left half of the tree is the mirror image of the right half of the tree. Write a predicate to determine whether or not a given binary tree is symmetric. (see To Tree, or not to Tree for a reminder on the tree representation we’re using).

(defn tree-sym?
  (let [rt (fn rev-tree [t]
             (if (coll? t)
               (list (first t) (rev-tree (nth t 2)) (rev-tree (second t)))
    (= (second tree) (rt (nth tree 2)))))

(tree-sym? '(:a (:b nil nil) nil))

No. 126 Through the Looking Class Enter a value which satisfies the following:

(let [x __]
  (and (= (class x) x) x))


No. 173 Intro to Destructuring 2 Sequential destructuring allows you to bind symbols to parts of sequential things (vectors, lists, seqs, etc.): (let [bindings* ] exprs*) Complete the bindings so all let-parts evaluate to 3.

(= 3
  (let [[__] [+ (range 3)]] (apply __))
  (let [[[__] b] [[+ 1] 2]] (__ b))
  (let [[__] [inc 2]] (__)))

a c

No. 43 Reverse Interleave Write a function which reverses the interleave process into x number of subsequences.

(fn [s n]
   (fn [i] (map #(nth % i) (partition n n s)))
   (range n)))

No. 44 Rotate Sequence Write a function which can rotate a sequence in either direction.

(defn seq-rotate [n s]
  (let [m (mod n (count s))]
    (->> s
         (split-at m)
         (apply concat))))

(seq-rotate 5 [:a :b :c])


I had fun solving the 4Clojure problems this time. Especially the somewhat hard ones. I didn’t get to work on DataLog today. It’s looking that’s gonna be an activity for tomorrow.

Today’s tally -