Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created February 10, 2014 08:30
Show Gist options
  • Save tiensonqin/8912301 to your computer and use it in GitHub Desktop.
Save tiensonqin/8912301 to your computer and use it in GitHub Desktop.
;; Clojure vs Erlang on counter recursion
;; Clojure version
(ns count)
(defn down
[x]
(if (zero? x) (prn "blastoff!")
(do
(prn x)
(recur (dec x)))))
(defn up
[x]
(loop [c 1 x x]
(if (> c x) (prn "Finished!")
(do
(prn c)
(recur (inc c) x)))))
------------------------------------------------------------
;; Erlang version
-module(count).
-export([countdown/1, countup/1]).
countdown(From) when From > 0 ->
io:format("~w!~n", [From]),
countdown(From-1);
countdown(From) ->
io:format("blastoff!~n").
countup(Limit) ->
countup(1, Limit).
countup(Count,Limit) when Count =< Limit ->
io:format("~w!~n", [Count]),
countup(Count+1, Limit);
countup(Count, Limit) ->
io:format("Finally!~n").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment