9 ms·
What, a Haskell program that launches 3 threads and coordinates them with inter-thread communication is harder to read than an Ada Hello World? Who would have t
by marcosdumay 12y ago
What, a Haskell program that launches 3 threads and coordinates them with inter-thread communication is harder to read than an Ada Hello World? Who would have tought!
The equivalent in Haskell to your Ada program is:
main = putStrLn "Hello, World!"
I don't even think about taking the time to write the equivalent in Ada to the Haskell program you posted.
- tormeh 12y agoI'm no good at Ada, but I like the way it's syntax tries to guide you through reading the program. Now for something of comparative complexity in Clojure: (def x (ref 1)) (defn increment [i] (if (> i 0) ( (dosync (alter x inc) ) (Thread/sleep 1) (increment (- i 1)) ) ) ) (defn decrement [i] (if (> i 0) ( (dosync (alter x dec) ) (Thread/sleep 1) (decrement (- i 1)) ) ) ) (defn printref [i] (if (> i 0) ( (dosync (println (format "in printref %d" @x)) ) (Thread/sleep 1) (printref (- i 1)) ) ) ) (future (increment 10) ) (future (printref 15) ) (future (decrement 10) ) Isn't this much nicer? It's not immediately obvious that x is an atomic variable, but aside from that it's a lot better than the Haskell example. It took me along the lines of 2-3 hours from never having touched a Lisp to writing this.
- BasDirks 12y agoNo this is not much nicer.
- sgk284 12y agoHere is the Haskell equivalent of your Clojure code: import Data.IORef import Control.Concurrent increment _ 0 = return () increment x i = do alter x succ threadDelay 1000 increment x (i - 1) decrement _ 0 = return () decrement x i = do alter x pred threadDelay 1000 decrement x (i - 1) printref _ 0 = return () printref x i = do val <- readIORef x putStrLn ("in printref " ++ (show val)) threadDelay 1000 printref x (i - 1) main = do x <- newIORef 1 forkIO (increment x 10) forkIO (printref x 15) forkIO (decrement x 10) threadDelay 100000 -- This is just a helper to more closely match the clojure alter x fn = atomicModifyIORef' x (\y -> (fn y, ())) I'd argue the Haskell is even nicer.