Guest User

Untitled

a guest
May 28th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns calculator
  2.   (:require [clojush]
  3.             [clojure.contrib.math])
  4.   (:use [clojush]
  5.         [clojure.contrib.math]))
  6.  
  7.  
  8. (in-ns 'clojush)
  9.  
  10. (def push-types '(:exec :integer :float :code :boolean :auxiliary :tag :zip :string :symbol))
  11. (define-push-state-structure)
  12.  
  13. ;(defn isoperator?
  14. ;  "Is operator?"
  15. ;  [x]
  16. ;  (try (do (apply (ns-resolve *ns* (symbol (str x))) [1 1]) true)
  17. ;       (catch Exception e false)))
  18.  
  19. (defn recognize-literal
  20.   "If thing is a literal, return its type -- otherwise return false."
  21.   [thing]
  22.   (cond
  23.     (integer? thing) :integer
  24.     (number? thing) :float
  25.     (string? thing) :string
  26.     (or (= thing true) (= thing false)) :boolean
  27.     true false))
  28.  
  29. (in-ns 'calculator)
  30.  
  31. (defn toInt
  32.   "convert integer to string"
  33.   [x]
  34.   (if (string? x)
  35.     (try (Integer/parseInt x)
  36.          (catch Exception e nil))
  37.     nil))
  38.  
  39.  
  40.  
  41.  
  42. (defn findargs
  43.   [x]
  44.   (:arglists (meta x)))
  45.  
  46.  
  47. (defn tostr
  48.   "Convert a list characters to a string"
  49.   [x]
  50.   (apply str x))
  51.  
  52.          
  53. (defn strToStack
  54.   "Puts variables on correct stack"
  55.   [state]
  56.   (let [x (reverse (:string state))
  57.         floats (atom ())
  58.         symbs (atom ())];A list of all keys pressed in correct order
  59.    
  60.     (loop [stk x]                                    
  61.       (if (not (empty? stk));If not empty list/stack
  62.         (if (number? (toInt (first stk)));If next entry is a number
  63.           (do (reset! floats (not-lazy (cons
  64.                                          (toInt
  65.                                            (tostr
  66.                                              (take-while #(number? (toInt %)) stk))) @floats)))
  67.               (recur (not-lazy (drop-while #(number? (toInt %)) stk))));Add number to number list
  68.           (do (reset! symbs (not-lazy (cons (symbol (first stk)) @symbs)))
  69.               (recur (not-lazy (rest stk)))))));;If not, add oper to oper list
  70.    
  71.    
  72.     ;Push input onto relevant stacks
  73.     (let [state1 (loop [stt @floats st state]
  74.                    (if (not (empty? stt))
  75.                      (recur (rest stt) (push-item (first stt) :integer st))
  76.                      st))];Push numbers onto stack
  77.      
  78.       (loop [stt1 @symbs st1 state1];Put symbols onto stack
  79.         (if (not (empty? stt1))
  80.           (recur (rest stt1) (push-item (first stt1) :symbol st1))
  81.           st1)))))
  82.  
  83. ;
  84. ;(defn call
  85. ;  "apply type to top 2 integer stack"
  86. ;  [type]
  87. ;  (fn [state]
  88. ;    (let [t1 (top-item type state)
  89. ;          i1 (stack-ref :integer 0 state)
  90. ;          i2 (stack-ref :integer 1 state)]
  91. ;
  92. ;      (push-item (apply (ns-resolve *ns* (symbol (str t1))) [i1 i2])
  93. ;                 :integer
  94. ;                 (pop-item type
  95. ;                           (pop-item :integer
  96. ;                                     (pop-item :integer state)))))))
  97.  
  98.  
  99.  
  100.  
  101. (defn call
  102.   "apply type to top 2 integer stack"
  103.   [type]
  104.   (fn [state]
  105.     (let [t1 (top-item type state)
  106.           i1 (stack-ref :integer 1 state)
  107.           i2 (stack-ref :integer 0 state)]
  108.  
  109.       (push-item (apply (ns-resolve *ns* (symbol (str t1))) [i1 i2])
  110.                  :integer state))))
  111.  
  112. (define-registered symbol_call (call :symbol))
  113.  
  114.  
  115. (defn simulate
  116.   "Simulates capturing data from keyboard and putting in stack"
  117.   [x]
  118.   (loop [n (dec (count x)) state (make-push-state)]
  119.     (if (< n 0)
  120.       state
  121.       (recur (dec n) (not-lazy (push-item (str (nth x n)) :string state))))))
  122.  
  123. (define-registered in
  124.                    (fn [state] (strToStack state)))
  125.  
  126. (defn f1
  127.   [x ans]
  128.   (fn [program]
  129.     (doall
  130.       (list
  131.         (let [state (run-push program
  132.                               (not-lazy (strToStack (simulate x))))
  133.               top-num (top-item :integer state)]
  134.           (if (number? top-num)
  135.             (abs (- top-num ans))
  136.             100))))))
  137. ;
  138. ;(isoperator? "^")
  139. ;(isoperator? 1)
  140. ;(isoperator? "shit")
  141.  
  142.  
  143. (pushgp
  144.   :error-function (f1 "8+3" 11)
  145.   :atom-generators (concat
  146.                      (registered-for-type :integer)
  147.                      (list
  148.                        'symbol_call
  149.                        'in))
  150.                      
  151.   :max-points 50
  152.   :evalpush-limit 1000)
Add Comment
Please, Sign In to add comment