Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Stack/Clojure/stack-1.clj
Normal file
20
Task/Stack/Clojure/stack-1.clj
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(deftype Stack [elements])
|
||||
|
||||
(def stack (Stack (ref ())))
|
||||
|
||||
(defn push-stack
|
||||
"Pushes an item to the top of the stack."
|
||||
[x] (dosync (alter (:elements stack) conj x)))
|
||||
|
||||
(defn pop-stack
|
||||
"Pops an item from the top of the stack."
|
||||
[] (let [fst (first (deref (:elements stack)))]
|
||||
(dosync (alter (:elements stack) rest)) fst))
|
||||
|
||||
(defn top-stack
|
||||
"Shows what's on the top of the stack."
|
||||
[] (first (deref (:elements stack))))
|
||||
|
||||
(defn empty-stack?
|
||||
"Tests whether or not the stack is empty."
|
||||
[] (= () (deref (:elements stack))))
|
||||
14
Task/Stack/Clojure/stack-2.clj
Normal file
14
Task/Stack/Clojure/stack-2.clj
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(defprotocol StackOps
|
||||
(push-stack [this x] "Pushes an item to the top of the stack.")
|
||||
(pop-stack [this] "Pops an item from the top of the stack.")
|
||||
(top-stack [this] "Shows what's on the top of the stack.")
|
||||
(empty-stack? [this] "Tests whether or not the stack is empty."))
|
||||
(deftype Stack [elements]
|
||||
StackOps
|
||||
(push-stack [x] (dosync (alter elements conj x)))
|
||||
(pop-stack [] (let [fst (first (deref elements))]
|
||||
(dosync (alter elements rest)) fst))
|
||||
(top-stack [] (first (deref elements)))
|
||||
(empty-stack? [] (= () (deref elements))))
|
||||
|
||||
(def stack (Stack (ref ())))
|
||||
Loading…
Add table
Add a link
Reference in a new issue