(ns clojure-arithm-operations.core)


;tool: absolute value to apply over a difference
;Shlomi (2016) 
;https://stackoverflow.com/questions/21753243/absolute-value-of-a-number-in-clojure
(defn abs [n] (max n (-' n)))



;;bank with the four oprations
(def my-operations   ["+" "-" "*" "/" ]); #'clojure-arithm-operations.core/my-operations

(comment
	(my-operations 0); +
	(my-operations 2); *
 (my-operations (rand-int 4)); -
)



;; an operation is chosen at random
(defn choose-operation []
       "an operation is chosen at random"
       (my-operations (rand-int 4)))

(comment
  (choose-operation); *
  (choose-operation); +
)



; constant is defined
(def target-operation (choose-operation))

(comment  
 (println target-operation); +
 (println target-operation);+
 (println target-operation);+
)





;seed-code to be evolved by hand is defined

(def seed-code1 '(+ 1 2))
(def seed-code2 "(+ 2 3)")

(comment
  
  (eval seed-code1);3
  (eval (binding [*read-eval* false] (read-string seed-code2)));5
  
 )



(comment; evaluation after replacement
   
;replace: replaces all instances of a match in a string with the replacement string.
(defn hello-world []
   (println (clojure.string/replace "The tutorial is about Groovy" #"Groovy"
      "Clojure")))
(hello-world);The tutorial is about Clojure


;evaluation after replacements
(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 2 1) #"plus"    "-"))));1

(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    (choose-operation)))));4
(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    (choose-operation)))));12
(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    (choose-operation)))));8
(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    (choose-operation)))));3

(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    target-operation))));3
(eval (binding [*read-eval* false] (read-string (clojure.string/replace '(plus 6 2) #"plus"    target-operation))));3
(eval (binding [*read-eval* false] (read-string (clojure.string/replace "(plus 6 2)" #"plus"    target-operation))));3
(eval (binding [*read-eval* false] (read-string (clojure.string/replace "(plus 6 2)" #"plus"    target-operation))));3

target-operation;*
 (defn value [target-operation]
     (eval (binding [*read-eval* false] (read-string (clojure.string/replace "(plus 6 2)" #"plus"    target-operation)))))
 (value target-operation);12

 
 (defn value [target-operation x]
              (let [code (clojure.string/replace "(plus six 2)" #"six"    x)]
                 (eval (binding [*read-eval* false] (read-string (clojure.string/replace code #"plus"    target-operation))))))
   (value target-operation "8"); 16
);end of comment;end of comment;end of comment;end of comment;end of comment;end of comment




 ;target-operation is evaluated over x y
(defn result [target-operation x y]
         (let [code (clojure.string/replace "(plus six 2)" #"plus"    target-operation)
               code (clojure.string/replace code #"six"    x)
               code (clojure.string/replace code #"2"    y  )                           
               ]
           (println target-operation)
            (eval (binding [*read-eval* false] (read-string code)))))
 (result target-operation "8" "4");32
 
 


;;;;;;;;Comment: conditional-loops ;;;;;;;;;;

(comment 
        
; example of a  loop with when
(defn loop-example []
      (loop [error 10]
         (when (> error 0 )
            (println error)
            (recur (- error 2)))))

(loop-example); 10 8 6 4 2 nil


;1 + 2 +    + 10
 (loop [iter 1
          acc  0]
     (if (> iter 10)
       (println acc)
       (recur (inc iter) (+ acc iter))));55


; example   of a   loop with while and atom
(defn Example []
      (def x (atom 1))
      (while ( < @x 5 )
         (do
            (println @x)
            (swap! x inc))))
   (Example);1 2 3 4
   
   
 ;example of dotimes
(defn do-n-times []
         (dotimes [n 5]; seq 0...4
         (println n)))
(do-n-times);0 1 2 3 4
  

(defn do-n-times [target-operation]
   (dotimes [n 2] 
      (let [x (rand-int 15)
            y (rand-int 15)]
        (println (str "x " x))
        (println (str "y " y))  )))
(do-n-times target-operation)
;x 0
;y 14
;x 2
;y 6


(defn do-n-times [target-operation]
       (dotimes [n 2] 
            (let [x (str (rand-int 15))
                  y (str (rand-int 15))
                  result1 (result target-operation x y)          
                  ]
              (println (str "target " target-operation))
              (println (str "result1 " result1))
            )))
    (do-n-times target-operation)
;#'clojure-arithm-operations.core/do-n-times
;*
;target *
;result1 54
;*
;target *
;result1 168
nil



(defn matching-trial  [target-operation]
   "A trial operation is chosen at random. 
    Target and trial operation are compared by their outputs.
    Differences are added together to a total"
   (def total (atom 0))
   (let
     [trial-operation (choose-operation)]
     (dotimes [n 2] 
       (let [x (str (inc (rand-int 15)))
             y (str (inc (rand-int 15)))
             result1 (result target-operation x y)                
             result2 (result trial-operation x y) 
             difference (abs (- result1 result2))]
         ;Warning! 
         ;variables inside (let []) that were already used
         ; are error prone upon reuse!
         (println (str "target " target-operation))
         (println (str "x " x " y " y))
         (println (str "result1 " result1))
         (println (str "trial " trial-operation))
         (println (str "result2 " result2))           
         (swap! total + difference) ); difference is brand new for use
       ))
   @total
   )
(println (matching-trial target-operation))
;#'clojure-arithm-operations.core/matching-trial
;-
;+
;target -
;x 2 y 6
;result1 0
;trial +
;result2 12
;-
;+
;target -
;x 3 y 8
;result1 -5
;trial +
;result2 11
;28
;nil
;nil


;We need to insert the function matching-trial iside a loop 
;that guesses the target operation. 
;The next is a good example to be followed: 

 (ns number-guessing)
         
   
   ;Number Guessing Game in Clojure
   ;redlibrarian (2015)
   ;https://gist.github.com/redlibrarian/9551dfbd7babb2eb2491
   
   
   (def random-number-max 100)
   (def random-number-min 1)
   
   (defn read-guess []
     (let [guess (read)]
       guess))
   
   (defn print-greeting []
     (println (str "I've picked a random number between: " 
                   random-number-min " and " 
                   random-number-max     ". Can you guess it?")))
   
   (defn guessed-wrong [direction]
     (println (str "Sorry, guess again but " direction))
     false)
   
   (defn guessed-right [times]
     (println (str "You got it! It took you " times " attempts"))
     true)
   
   (defn evaluate [answer guess times]
       (cond
             (> answer guess) (guessed-wrong "higher")
             (< answer guess) (guessed-wrong "lower")
             :else (guessed-right times)))
   
   (defn game
     []
     (print-greeting)
     (let [answer (inc (rand-int random-number-max))]
       (loop [times 1]
         (if (= true (evaluate answer(read-guess) times))
         -1
         (recur (inc times))))))
      
  ; Paste to a REPL and to run it print   (game)   at the end
    
    






;;;;;;;;;;;;;;;  A false try

(defn matching-trial  [target-operation]
   "A trial operation is chosen at random. 
    Target and trial operation are compared by their outputs.
    Differences are added together to a total"
   (def total (atom 0))
   (let
     [trial-operation (choose-operation)]
     (dotimes [n 2] 
       (let [x (str (inc (rand-int 15)))
             y (str (inc (rand-int 15)))
             result1 (result target-operation x y)                
             result2 (result trial-operation x y) 
             difference (abs (- result1 result2))]
         ;Warning! 
         ;variables inside (let []) that were already used
         ; are error prone upon reuse!
         (println (str "target " target-operation))
         (println (str "x " x " y " y))
         (println (str "result1 " result1))
         (println (str "trial " trial-operation))
         (println (str "result2 " result2))           
         (swap! total + difference) ); difference has not been used
       ))
   @total
   )


(defn task [target-operation]
  (loop [times 1]
    (let [error (matching-trial target-operation)]
    (if ( = error 0) (println "ERROR = 00000000000000000000000000000000000000"))
     (recur (inc times)))))

;when this code is run with (task target-operation) an infinite loop appears.



;New idea:
;1 + 2 +    + 10
 (loop [iter 1
          acc  0]
     (if (> iter 10)
       (println acc)
       (recur (inc iter) (+ acc iter))));55

);;;;;;;;;;;;;end of comment;end of comment;end of comment;end of comment;end of comment;end of comment



(defn matching-trial  [target-operation]
  "A trial operation is chosen at random. 
      Target and trial operation are compared by their outputs.
      Differences are added together to acc"
  (let
    [trial-operation (choose-operation)]
    (loop [iter 1
           acc  0]
      (if (> iter 20) (double  acc);return value
        (let [x (str (inc (rand-int 10)))
              y (str (inc (rand-int 10)))
              result1 (result target-operation x y)                
              result2 (result trial-operation x y) 
              difference (abs (- result1 result2))]
          ;Warning! 
          ;variables inside (let []) that were already used
          ; are error prone upon reuse!
          ;(println (str "target " target-operation))
          ;(println (str "x " x " y " y))
          ;(println (str "result1 " result1))
          ;(println (str "trial " trial-operation))
          ;(println (str "result2 " result2))
          
          (recur (inc iter) (+ acc difference)))))))
   
;(matching-trial target-operation)
 


(comment; target operation is matched
   
(defn pretask [target-operation]
  (loop [times 1]
    (let [error (matching-trial target-operation)]
      (println str "Times " times  "Error " error)
    (if ( = error 0.0) (println "Target-operation " target-operation "   ERROR = 00000000000000000000000000000000000000  times" times)
     (recur (inc times))))))

;(pretask target-operation)

)


    
    
    
;;The target operation must be matched,
;;and the corresponding code must be publsihed:
    
 

(defn report [target-operation times]
           (let [code "(defn matching-operation [x y]
            (plus x y))" ]
             
             
           (println "Target-operation " target-operation "   ERROR = 00000000000000000000000000000000000000  times" times)
           
           (println "The operation has been matched. \nThe corresponding Clojure code is " )
            (clojure.string/replace code #"plus"    target-operation))
           )
         
         
         
         (defn task [target-operation]
           (loop [times 1]
             (let [error (matching-trial target-operation)]
               (println str "Times " times  "Error " error)
             (if ( = error 0.0) (report target-operation   times)
              (recur (inc times))))))
         
         ;run with 
         ;(task target-operation)
         
         
         
 (comment ;comment;comment;comment;comment;comment;comment
    (task target-operation)
 #'clojure-arithm-operations.core/report
 #'clojure-arithm-operations.core/task
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
 /
#<core$str clojure.core$str@657cd117> Times  1 Error  0.0
 Target-operation  /    ERROR = 00000000000000000000000000000000000000  times 1
 The operation has been matched. 
  The corresponding Clojure code is 
 (defn matching-operation [x y]
          (/ x y))
 => (defn matching-operation [x y]
             (/ x y))
 #'clojure-arithm-operations.core/matching-operation
 => (matching-operation 9 3)
3
);end of comment;end of comment;end of comment;end of comment;end of comment
 
 
 ;run with 
 ;(task target-operation)