So. I didn't do any DataLog today. I mostly just solved some 4Clojure problems and finished Ch 7
-
Reader form -> textual representation of data structures
-
Text goes to reader -> clojure reads the stream -> produces the data structures -> evaluate the data structure -> prints the textual repr
-
Reader uses a reader macro to transform the anon function into a different representation
-
Reader macro is a set of rules for transforming text into data structures allow for compact representation of data structures
-
Evaluator -> takes DS, process the DS using rules and returns a result
-
Symbols -> use symbols to name functions, macros and other data and evalutates them by resolving them.
-
resolving -> check the local bindings -> look up symbol in the namespace map -> evaluate
-
nested bindings -> most recent one takes precedence
-
map and inc both refer to functions. they're not the functions themselves
-
map is a symbol that refers to a function
(type (read-string "map"))
=> clojure.lang.Symbol
- On their own symbols and referents don't actually do anything. clojure has to evaluate them to produce a result
- Macros are executed in between the reader and the evaluator -> takes the data structure the reader returns and transforms them into a different data structure that is passed to the evaluator.
- when you call a macro, the operands are not evaluated. the symbols are passed along as symbols
- macroexpansion -> process of determining the return value of a macro
-
- data structure returned by a macro is evaluated
-
- data structure returned by a function is not
- The evaluator processes data structures based on their type: symbols are resolved to their referents; lists result in function, macro, or special form calls; and everything else evaluates to itself
- 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)")
- 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
[math]
(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
[s]
(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?
[tree]
(let [rt (fn rev-tree [t]
(if (coll? t)
(list (first t) (rev-tree (nth t 2)) (rev-tree (second t)))
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))
Class
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]
(map
(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)
reverse
(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 -
- 8 4Clojure problems
- Finished Chapter 7