From a8bf474c88c23078f2f789a7d24bbd25a89e63b2 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sat, 10 Jul 2010 18:08:46 +0530 Subject: [PATCH] solution to 2.28. Really interesting.. --- src/sicp/ex2_28.clj | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/sicp/ex2_28.clj 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))) -- 2.45.2