;;Fibonacci Sequence
;;# 26 **
; This problem has to do with Fibonacci sequences. It is a series of integers where the next
; number is found by adding up the two numbers before it:
; 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…

;Write a function which returns the first X Fibonacci numbers.
; (= (__ 3) '(1 1 2))
; (= (__ 6) '(1 1 2 3 5 8))
; (= (__ 8) '(1 1 2 3 5 8 13 21))

#_(fn fib [x]
(defn helper [n]
(cond (= n 0) 0
(= n 1) 1
:else (+ (helper (- n 1))
(helper (- n 2)))))
	(map helper (range 1 (inc x))))

; (println (#(take %
;   (map first
;     (iterate (fn [[a b]] [b (+ a b)]) [1 1]))) 5))


; Get the Caps
; #29 **
; Write a function which takes a string and returns a new string containing only the capital
; letters.
; (= (__ "HeLlO, WoRlD!") "HLOWRD")
; (empty? (__ "nothing"))
; (= (__ "$#A(*&987Zf") "AZ")

 #_(defn fc[s]
     (#(apply str (re-seq #"[A-Z]" %)) s))
 
 #_(fn [s]
    (apply str (filter #(Character/isUpperCase %) s)))
;  (println (fc "HeLlO, WoRlD!"))

     
; Intro to some
; ** #48
; The some function takes a predicate function and a collection. It returns the first logical
; true value of (predicate x) where x is an item in the collection.
; (= __ (some #{2 7 6} [5 6 7 8]))
; (= __ (some #(when (even? %) %) [5 6 7 8]))



; Factorial Fun
; #42 **
; A factorial is a product of all positive integers less than or equal to n. An example is:
; Write a function which calculates factorials.
; (= (__ 1) 1)
; (= (__ 3) 6)
; (= (__ 5) 120)
; (= (__ 8) 40320)

(defn fact [n]
 (cond (== n 1) 1
 :else 
 (* n (fact (- n 1)))))

#_(defn fact
 ([n] (fact n 1))
 ([n result]
  (cond 
        (== n 1) result 
        :else 
        (recur (- n 1) (* result n)))))
; (println (fact 5))

;;==================================================================DAY 2======================

; Problem 83
; Write a function which takes a variable number of booleans.
; Your function should return true if some of the parameters are true, 
; but not all of the parameters are true. Otherwise your function should return false.
; #(not (apply = %&))


; Problem 66
; Given two integers, write a function which returns the greatest common divisor.
(defn gcd[a b]
    (if (zero? b)
        a
     (gcd b (rem a b)))
 )

; (println (gcd 2 4))

; Problem 107
; Lexical scope and first-class functions are two of 
; the most basic building blocks of a functional language like Clojure.
; When you combine the two together, you get something very powerful called lexical closures. 
; With these, you can exercise a great deal of control over the lifetime of your local bindings,
; saving their values for use later, long after the code you're running now has finished.
; It can be hard to follow in the abstract, so let's build a simple closure.
; Given a positive integer n, return a function (f x) which computes xn. Observe
; that the effect of this is to preserve the value
; of n for use outside the scope in which it is defined.


#(fn [x]
   (apply * (repeat % x)))

((#(fn[x]
     (apply * (repeat % x))) 2) 16)
 
 ;; EXERCISES SIMILAR TO PRB 107 CLOSURES 
 
#_(def times-two 
 #(let [x 2]
  (* % x))) 
;; or 

#_(def times-two
 (let [x 2]
  #(* % 2)))
#_(println 
 (times-two 5))

; Functions returning closures  EXAMPLE

(defn times-n[n]
 (let [x n]
   (fn[y](* y x)))) ;; Example (timed-n 4) returns a closure to be used another operation 
;;Closing over parameters EXAMPLE

;; here we close over [n] defined in the functions closure 
(defn times-n [n]
 #(* % n))

;; a number is divisible if the rem between numerator and denomenator is zero 
(defn divisible 
    [denom]
 #(zero? (rem % denom))
 )

;;Passing closures as functions EXAMPLES 

(println (filter (divisible 4)(range 10)))

; It’s common to define a closure right on the spot where it’s used,
; closing over whatever local context is needed
(defn filter-div [denom s]
 (filter (fn[num](zero? (rem num denom)))s))

(defn filter-div2[denom s]
 (filter #(zero? (rem % denom)) s))

;; back to 4clojure example 
((#(fn[x] 
 (apply * (repeat % x)))8)2)



;;Sharing closure context PG. 192 


;; THEN DO 
; Cartesian Product
; 90
; Write a function which calculates the Cartesian product of two sets.
; The online version has nice unicode hearts and symbols, which unfortunately don’t show
; up in the printed code. The following code has the symbols substituted with text:



 







Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: