exercises: recommend not to override nth permanently
authorNicolas Boulenguez <nicolas.boulenguez@free.fr>
Tue, 14 May 2019 22:56:50 +0000 (00:56 +0200)
committerNicolas Boulenguez <nicolas.boulenguez@free.fr>
Thu, 30 May 2019 14:46:04 +0000 (16:46 +0200)
docs/exercises.md
examples/exercises.mal

index dddad9c..aac6eb6 100644 (file)
@@ -52,6 +52,10 @@ make REGRESS=1 TEST_OPTS='--hard --pre-eval=\(load-file\ \"../answer.mal\"\)' te
 
   Try to replace explicit recursions with calls to `reduce` and `foldr`.
 
+  Once you have tested your solution, you should comment at least
+  `nth`.  Many implementations, for example `foldr` in `core.mal`,
+  rely on an efficient `nth` built-in function.
+
 - Implement the `do` special as a non-recursive function. The special
   form will hide your implementation, so in order to test it, you will
   need to give it another name and adapt the test accordingly.
index be51eb6..247e050 100644 (file)
 (def! count
   (let* [inc_left (fn* [acc _] (inc acc))]
     (fn* [xs] (if (nil? xs) 0 (reduce inc_left 0 xs)))))
-(def! nth
-  (fn* [xs index]
-    (if (or (empty? xs) (< index 0))
-      (throw "nth: index out of range")
-      (if (zero? index)
-        (first xs)
-        (nth (rest xs) (dec index))))))
+;; (def! nth
+;;   (fn* [xs index]
+;;     (if (or (empty? xs) (< index 0))
+;;       (throw "nth: index out of range")
+;;       (if (zero? index)
+;;         (first xs)
+;;         (nth (rest xs) (dec index))))))
 (def! map
   (fn* [f xs]
     (let* [iter (fn* [x acc] (cons (f x) acc))]