<commit_before>(ns sicp.chapter2.2-2
  (:require [sicp.chapter2.2-1 :refer [reverse']]))

;;;; 2.2  Hierarchical Data and the Closure Property

;;; 2.2.2  Hierarchical Structures

;; Exercise 2.24
(list 1 (list 2 (list 3 4)))
;=> (1 (2 (3 4)))

;; Exercise 2.25
(first (nfirst (nnext '(1 3 (5 7) 9))))

(ffirst '((7)))

(fnext (fnext (fnext (fnext (fnext (fnext '(1 (2 (3 (4 (5 (6 7))))))))))))

;; Exercise 2.26
(def x (list 1 2 3))
(def y (list 4 5 6))

(concat x y)
;=> (1 2 3 4 5 6)

(cons x y)
;=> ((1 2 3) 4 5 6)

(list x y)
;=> ((1 2 3) (4 5 6))

;; Exercise 2.27
(defn deep-reverse [coll]
  (letfn [(rev [coll acc]
            (cond
              (not (seq? coll)) coll
              (empty? coll) acc
              :else (recur (rest coll)
                           (cons (deep-reverse (first coll)) acc))))]
    (rev coll [])))

(def x' (list (list 1 2) (list 3 4)))
(reverse' x')
(deep-reverse x')

;; Exercise 2.28
(defn fringe [tree]
  (letfn [(fri [coll acc]
            (cond
              (not (seq? coll)) (cons coll acc)
              (empty? coll) acc
              :else (recur (rest coll)
                           (fri (first coll) acc))))]
    (reverse (fri tree []))))

(def x'' (list (list 1 2) (list 3 4)))
(fringe x'')
(fringe (list x'' x''))

;; Exercise 2.29
;; TODO

;; Exercise 2.30
;; TODO

;; Exercise 2.31
;; TODO

;; Exercise 2.32
;; TODO<commit_msg>Exercise 2.30, 2.31, 2.32<commit_after>(ns sicp.chapter2.2-2
  (:require [sicp.chapter1.1-4 :refer [square]]
            [sicp.chapter2.2-1 :refer [reverse']]))

;;;; 2.2  Hierarchical Data and the Closure Property

;;; 2.2.2  Hierarchical Structures

;; Exercise 2.24
(list 1 (list 2 (list 3 4)))
;=> (1 (2 (3 4)))

;; Exercise 2.25
(first (nfirst (nnext '(1 3 (5 7) 9))))

(ffirst '((7)))

(fnext (fnext (fnext (fnext (fnext (fnext '(1 (2 (3 (4 (5 (6 7))))))))))))

;; Exercise 2.26
(def x (list 1 2 3))
(def y (list 4 5 6))

(concat x y)
;=> (1 2 3 4 5 6)

(cons x y)
;=> ((1 2 3) 4 5 6)

(list x y)
;=> ((1 2 3) (4 5 6))

;; Exercise 2.27
(defn deep-reverse [coll]
  (letfn [(rev [coll acc]
            (cond
              (not (seq? coll)) coll
              (empty? coll) acc
              :else (recur (rest coll)
                           (cons (deep-reverse (first coll)) acc))))]
    (rev coll [])))

(def x' (list (list 1 2) (list 3 4)))
(reverse' x')
(deep-reverse x')

;; Exercise 2.28
(defn fringe [tree]
  (letfn [(fri [coll acc]
            (cond
              (not (seq? coll)) (cons coll acc)
              (empty? coll) acc
              :else (recur (rest coll)
                           (fri (first coll) acc))))]
    (reverse (fri tree []))))

(def x'' (list (list 1 2) (list 3 4)))
(fringe x'')
(fringe (list x'' x''))

;; Exercise 2.29
;; TODO

;; Exercise 2.30
(defn square-tree [tree]
  (map (fn [t]
         (if (not (seq? t))
           (square t)
           (square-tree t)))
       tree))

;; Exercise 2.31
(defn tree-map [f tree]
  (map (fn [t]
         (if (not (seq? t))
           (f t)
           (tree-map f t)))
       tree))

;; Exercise 2.32
(defn subsets [s]
  (if (empty? s)
    (list ())
    (let [rest' (subsets (rest s))]
      (concat rest' (map (fn [x] (cons (first s) x)) rest')))))
{'fraction_non_alphanumeric': 0.1985705407085146, 'fraction_numerical': 0.06028589185829708, 'mean_word_length': 2.113152804642166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7288439', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1255, 'n_words': 442}