From: Ramakrishnan Muthukrishnan Date: Sat, 10 Jul 2010 12:38:46 +0000 (+0530) Subject: solution to 2.28. Really interesting.. X-Git-Url: https://git.rkrishnan.org/pf/content/en/service/index.php?a=commitdiff_plain;h=a8bf474c88c23078f2f789a7d24bbd25a89e63b2;p=sicp.git solution to 2.28. Really interesting.. --- diff --git a/src/sicp/ex2_28.clj b/src/sicp/ex2_28.clj new file mode 100644 index 0000000..027b285 --- /dev/null +++ b/src/sicp/ex2_28.clj @@ -0,0 +1,19 @@ +(ns sicp.ex2_28 + (:use [clojure.test])) + +;; take a list of nested lists and return a flat list with elements +;; in the same left-to-right order +(defn fringe [lst] + (cond (not (seq? lst)) (list lst) + (empty? lst) nil + :else (concat (fringe (first lst)) + (fringe (rest lst))))) + +(deftest test-fringe-with-simple-list + (is (= (fringe '(1 2 3 4)) '(1 2 3 4)))) + +(deftest test-fringe-with-nested-list + (are [x y] [= x y] + (fringe '((1 2) 3 4)) '(1 2 3 4) + (fringe '(1 2 (3 4))) '(1 2 3 4) + (fringe '((1 2) (3 4))) '(1 2 3 4)))