Wednesday, February 27, 2019

SICP in Clojure: Chapter 1, Exercise 3

This is my Clojure solution to Chapter 1, Exercise 3:

(ns sicp.ch1.ex03)

(defn f
  [x y z]
  (reduce #(+ %1 (* %2 %2))
          (drop 1 (sort [x y z]))))

Sunday, February 24, 2019

SICP in Clojure: Chapter 1, Exercise 2

This is my Clojure solution to Chapter 1, Exercise 2:

(ns sicp.ch1.ex02)

(/ (+ 5 6 (- 2 (- 3 (+ 6 (/ 4 5)))))
   (* 3 (- 6 2) (- 2 7))) ; => -7/25

Friday, February 22, 2019

SICP in Clojure: Chapter 1, Exercise 1

I am reading Hal Abelson's, Jerry Sussman's and Julie Sussman's Structure and Interpretation of Computer Programs but I am doing the exercises using Clojure. Of course, I'm not the first person to try this. This is my solution to Chapter 1, Exercise 1:


(ns sicp.ch1.ex01)

10 ; => 10

(+ 5 3 4) ; => 12

(- 9 1) ; => 8

(/ 6 2) ; => 3

(+ (* 2 4) (- 4 6)) ; => 6

(def a 3)

(def b (+ a 1))

(+ a b (* a b)) ; => 19

(if (and (> b a) (< b (* a b)))
  b
  a) ; => 4

(cond (= a 4) 6
      (= b 4) (+ 6 7 a)
      :else 25) ; => 16

(+ 2 (if (> b a) b a)) ; => 6

(* (cond (> a b) a
         (< a b) b
         :else -1)
   (+ a 1)) ; => 16