Skip to content

Instantly share code, notes, and snippets.

@pepellou
Created September 13, 2011 10:57
Show Gist options
  • Save pepellou/1213590 to your computer and use it in GitHub Desktop.
Save pepellou/1213590 to your computer and use it in GitHub Desktop.
FP4OO - Set 6 of exercises
;; Set 1 - exercise 1
; documentation of functions
(doc take)
(doc distinct)
(doc keep)
(doc concat)
(doc interleave)
(doc drop)
(doc drop-last)
(doc flatten)
(doc partition)
(doc every?)
(doc replicate)
(doc remove)
;; Use remove to remove all odd numbers from a list
(def lista (range 0 10))
(remove odd? lista)
;; Use partition and interleave to turn a pair of parallel lists into a list of pairs
(def names ["david" "brian" "pepe"])
(def surnames ["calavera" "marick" "doval"])
(partition 2 (interleave names surnames))
;; Set 2 - exercise 1
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Implement and add method to add one point to other
;; a) without using shifted
(defn add [this other]
(Point
(+ (:x this) (:x other))
(+ (:y this) (:y other))
)
)
;; b) using shifted
(defn add [this other]
(shifted
this
(:x other)
(:y other)
)
)
;; Set 4 - exercise 1
(defn map-concat [func seq]
(apply concat (map func seq))
)
;; Set 1 - exercise 3
;; Implement (prefix-of? [candidate seq]) such as:
;;
;; user> (prefix-of? '(1 3) [1 2 3])
;; false
;; user> (prefix-of? '(1 2) [1 2 3])
;; true
;; user> (prefix-of? [1 2] [1 2 3])
;; true
(defn prefix-of? [candidate seq]
(=
(take (count candidate) seq)
candidate
)
)
;; Implement (tails [seq]) such as:
;;
;; user> (tails '(1 2 3 4))
;; ((1 2 3 4) (2 3 4) (3 4) (4) ())
(defn tails [seq]
(map
drop
(range (inc (count seq)))
(replicate (inc (count seq)) seq)
)
)
;; Set 2 - exercise 2
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Implement function 'a' to create points like: (a Point 1 2)
(defn a [constructor arg1 arg2]
(constructor arg1 arg2)
)
;; Set 4 - exercise 2
(def my-list-of-integers [1 1 2 3 5])
(map (partial + 2) my-list-of-integers)
;; Set 1 - exercise 3
;; Make up problems roughly as difficult as prefix-of and tails
;; (suffix-of? [candidate seq])
(defn suffix-of? [candidate seq]
(=
(drop (- (count seq) (count candidate)) seq)
candidate
)
)
;; (heads [seq])
(defn heads [seq]
(map
drop-last
(range (inc (count seq)))
(replicate (inc (count seq)) seq)
)
)
;; Set 2 - exercise 3
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Enhance function 'a' to work with any number of arguments,
;; for example to create triangles
(defn a [constructor & args]
(apply constructor args)
)
; Sample use
(a Triangle
(a Point 1 2)
(a Point 1 3)
(a Point 3 1)
)
;; Set 4 - exercise 3
(def my-list-of-integers [1 1 2 3 5])
(map (fn [n] (+ n 2)) my-list-of-integers)
;; Set 2 - exercise 4
; If you finish before other people, look around to
; help other people
; This is BEST exercise so far. Reasoning and talking
; about solutions is a very powerful group learning
; technique. Even if you are ahead your partner
; talking about solutions is good to consolidate
; your knowledge.
;; Set 4 - exercise 4
(def myfun
(let [x 3]
(fn [] x)
)
)
;; x would return an error, since the scope of 'x' is the (let ···) expression
;; (myfun) would return x, as one can expect
;; Set 2 - exercise 5
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Reject points that don't make up a triangle
(defn slope [p1 p2]
(if
(= (:x p1) (:x p2))
:NaN
(/ (- (:y p1) (:y p2)) (- (:x p1) (:x p2)))
)
)
(defn same-line? [p1 p2 p3]
(=
(slope p1 p2)
(slope p1 p3)
)
)
(defn Triangle [p1 p2 p3]
(let [points [p1 p2 p3]]
(cond
(not (= (distinct points) points))
(throw (new Error "Ooops! Duplicated points!"))
(apply same-line? points)
(throw (new Error "Ooops! Points in the same line!"))
:else
{:point1 p1, :point2 p2, :point3 p3}
)
)
)
;; Another rejecting version, based on triangular rule
;; Just to practice some maths in Clojure...
(defn sqrt-step [target try]
(/ (+ try (/ target try)) 2)
)
(defn square-root [n]
(if
(= n 0)
0
(double (last (take 10 (iterate (partial sqrt-step n) (/ n 2)))))
)
)
(defn square [n] (* n n))
(defn distance [p1 p2]
(square-root
(+
(square (- (:y p1) (:y p2)))
(square (- (:x p1) (:x p2)))
)
)
)
(defn triangular-rule? [p1 p2 p3]
(let [
edge1 (distance p1 p2)
edge2 (distance p1 p3)
edge3 (distance p3 p2)
]
(and
(< edge1 (+ edge2 edge3))
(< edge2 (+ edge1 edge3))
(< edge3 (+ edge1 edge2))
)
)
)
(defn Triangle [p1 p2 p3]
(let [points [p1 p2 p3]]
(cond
(not (apply triangular-rule? points))
(throw (new Error "Ooops! Points don't fit the triangular rule!"))
:else
{:point1 p1, :point2 p2, :point3 p3}
)
)
)
;; Set 1 - exercise 1
; documentation of functions
(doc take)
(doc distinct)
(doc keep)
(doc concat)
(doc interleave)
(doc drop)
(doc drop-last)
(doc flatten)
(doc partition)
(doc every?)
(doc replicate)
(doc remove)
;; Use remove to remove all odd numbers from a list
(def lista (range 0 10))
(remove odd? lista)
;; Use partition and interleave to turn a pair of parallel lists into a list of pairs
(def names ["david" "brian" "pepe"])
(def surnames ["calavera" "marick" "doval"])
(partition 2 (interleave names surnames))
;; Set 1 - exercise 3
;; Implement (prefix-of? [candidate seq]) such as:
;;
;; user> (prefix-of? '(1 3) [1 2 3])
;; false
;; user> (prefix-of? '(1 2) [1 2 3])
;; true
;; user> (prefix-of? [1 2] [1 2 3])
;; true
(defn prefix-of? [candidate seq]
(=
(take (count candidate) seq)
candidate
)
)
;; Implement (tails [seq]) such as:
;;
;; user> (tails '(1 2 3 4))
;; ((1 2 3 4) (2 3 4) (3 4) (4) ())
(defn tails [seq]
(map
drop
(range (inc (count seq)))
(replicate (inc (count seq)) seq)
)
)
;; Set 1 - exercise 3
;; Make up problems roughly as difficult as prefix-of and tails
;; (suffix-of? [candidate seq])
(defn suffix-of? [candidate seq]
(=
(drop (- (count seq) (count candidate)) seq)
candidate
)
)
;; (heads [seq])
(defn heads [seq]
(map
drop-last
(range (inc (count seq)))
(replicate (inc (count seq)) seq)
)
)
;; Set 2 - exercise 1
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Implement and add method to add one point to other
;; a) without using shifted
(defn add [this other]
(Point
(+ (:x this) (:x other))
(+ (:y this) (:y other))
)
)
;; b) using shifted
(defn add [this other]
(shifted
this
(:x other)
(:y other)
)
)
;; Set 2 - exercise 2
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Implement function 'a' to create points like: (a Point 1 2)
(defn a [constructor arg1 arg2]
(constructor arg1 arg2)
)
;; Set 2 - exercise 3
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Enhance function 'a' to work with any number of arguments,
;; for example to create triangles
(defn a [constructor & args]
(apply constructor args)
)
; Sample use
(a Triangle
(a Point 1 2)
(a Point 1 3)
(a Point 3 1)
)
;; Set 2 - exercise 4
; If you finish before other people, look around to
; help other people
; This is BEST exercise so far. Reasoning and talking
; about solutions is a very powerful group learning
; technique. Even if you are ahead your partner
; talking about solutions is good to consolidate
; your knowledge.
;; Set 2 - exercise 5
(def point {:x 1, :y 2})
(def Point
(fn [x y]
{:x x, :y y}))
(def shifted
(fn [this xinc yinc]
(Point (+ (:x this) xinc)
(+ (:y this) yinc))))
(def Triangle
(fn [point1 point2 point3]
{:point1 point1, :point2 point2, :point3 point3}))
;; Reject points that don't make up a triangle
(defn slope [p1 p2]
(if
(= (:x p1) (:x p2))
:NaN
(/ (- (:y p1) (:y p2)) (- (:x p1) (:x p2)))
)
)
(defn same-line? [p1 p2 p3]
(=
(slope p1 p2)
(slope p1 p3)
)
)
(defn Triangle [p1 p2 p3]
(let [points [p1 p2 p3]]
(cond
(not (= (distinct points) points))
(throw (new Error "Ooops! Duplicated points!"))
(apply same-line? points)
(throw (new Error "Ooops! Points in the same line!"))
:else
{:point1 p1, :point2 p2, :point3 p3}
)
)
)
;; Another rejecting version, based on triangular rule
;; Just to practice some maths in Clojure...
(defn sqrt-step [target try]
(/ (+ try (/ target try)) 2)
)
(defn square-root [n]
(if
(= n 0)
0
(double (last (take 10 (iterate (partial sqrt-step n) (/ n 2)))))
)
)
(defn square [n] (* n n))
(defn distance [p1 p2]
(square-root
(+
(square (- (:y p1) (:y p2)))
(square (- (:x p1) (:x p2)))
)
)
)
(defn triangular-rule? [p1 p2 p3]
(let [
edge1 (distance p1 p2)
edge2 (distance p1 p3)
edge3 (distance p3 p2)
]
(and
(< edge1 (+ edge2 edge3))
(< edge2 (+ edge1 edge3))
(< edge3 (+ edge1 edge2))
)
)
)
(defn Triangle [p1 p2 p3]
(let [points [p1 p2 p3]]
(cond
(not (apply triangular-rule? points))
(throw (new Error "Ooops! Points don't fit the triangular rule!"))
:else
{:point1 p1, :point2 p2, :point3 p3}
)
)
)
;; Set 4 - exercise 1
(defn map-concat [func seq]
(apply concat (map func seq))
)
;; Set 4 - exercise 2
(def my-list-of-integers [1 1 2 3 5])
(map (partial + 2) my-list-of-integers)
;; Set 4 - exercise 3
(def my-list-of-integers [1 1 2 3 5])
(map (fn [n] (+ n 2)) my-list-of-integers)
;; Set 4 - exercise 4
(def myfun
(let [x 3]
(fn [] x)
)
)
;; x would return an error, since the scope of 'x' is the (let ···) expression
;; (myfun) would return x, as one can expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment