(defn big [st n] (if ( > (count st) n) true false)) ; usage (big "is length of this string longer than 15?" 15)
Day 1: Function that identifies collection.
(defn collection-type [col]
(cond
(vector? col) (str "vector:")
(list? col) (str "list:")
(map? col) (str "map:")))
(collection-type [1])
(collection-type (list 1 2))
(collection-type {:chewie :wookie :lea :human})Day 2: An unless with an else condition using macros.
(defmacro unless [test body] (list 'if (list 'not test) body)) (macroexpand '(unless condition body)) (unless (> 1 10) (println "Did you really think that 1 > 10?"))
Day 2: Type using defrecord that implements a protocol.
(defprotocol Person
(writeName [c][/c])
(writeAge [c][/c]))
(defrecord PersonImpl [name age]
Person
(writeName [_] (println name))
(writeAge [_] (println age))
Object
(toString [this] (str "Name=" (writeName this) "& Age=" (writeAge this) )))
(def andriy (PersonImpl. "Andriy" 25))
(writeName andriy)
(writeAge andriy)
(println andriy)Hey! Stop scrolling! :)
Day 3: Sleeping barber problem.
Problem called “sleeping barber.” was created by Edsger Dijkstra in 1965. It has these characteristics:
- A barber shop takes customers.
- Customers arrive at random intervals, from 10 to 30 milliseconds.
- The barber shop has three chairs in the waiting room.
- The barber shop has one barber and one barber chair.
- When the barber’s chair is empty, a customer sits in the chair, wakes up the barber, and gets a haircut.
- If the chairs are occupied, all new customers will turn away.
- Haircuts take 20 milliseconds.
- After a customer receives a haircut, he gets up and leaves.
Write a multithreaded program to determine how many haircuts a barber can give in 10 seconds.
My solution:
(def counter (agent 0)) (def chairs (agent 0)) (def workingTime (agent 1)) (defn haircut [x] (do (Thread/sleep 20) (println "...haircutting...") (+ 1 x))) (defn customer [x] (if (< x 3) (do (send counter haircut) (println "I'm next!") (+ 1 x)) (do (println "I went home. This barber shop sucks!") (- 1 x)))) (defn waitUntilEndOfTheDay [x] (do (Thread/sleep ( * 10 1000) ) (println "[ Clock ticked end of working day ]") (dec x) )) (send workingTime waitUntilEndOfTheDay) (while (pos? @workingTime) (do (Thread/sleep ( + 10 (rand-int 21) )) (send chairs customer))) (println "Barber has finished the day giving " @counter " haircuts.")
…
…haircutting…
I went home. This barber shop sucks!
…haircutting…
I’m next!
…haircutting…
I’m next!
I’m next!
[ Clock ticked end of working day ]
…haircutting…
Barber has finished the day giving 365 haircuts.
nil
| Markdown | Result |
|---|---|
| *text* | text |
| **text** | text |
| ***text*** | text |
| `code` | code |
| ~~~ more code ~~~~ |
more code |
| [Link](https://www.example.com) | Link |
| * Listitem |
|
| > Quote | Quote |
I Like your post. But why did you start with Clojure?
Regarding new concepts from Lisp – did you give a try to play with Lisp's AST? The idea is to change the codebase at runtime:) So after you feed the Lisp code to Lisp machine it transforms code to AST, and then you could make changes to AST on-the-fly. There is a true story about the debugging Lisp application on space shuttle remotely:)
Another nice thing about the Common Lisp language – it has great macro-programming facilities. The benefits of it's macro-system are:
1) possibility to build DSL(Domain specific languages)
2) nice meta-programming experience
Also one of the best-know Lisp products is Emacs – best ever text editor:) It mostly implemented with ELisp and is very flexible – you could configure and modify almost every part of it with ELisp code.
Hi, thank you very much for such a great comment.
I started with Clojure as it is one of the languages in book "7 languages in 7 weeks", which I was reading. Also Uncle Bob says that Clojure is best language ever :)
I haven't tried AST in Closure, but I've tried similar idea in Erlang. (btw, I have post on that one as well). Indeed, great feature!
Meta-programming and DSL isn't something extremely unique, though I haven't tried it in Clojure.
Now you encouraged me to try out Emacs.
Thank you again.