1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-16 17:20:23 +03:00

exercises: recommend not to override nth permanently

This commit is contained in:
Nicolas Boulenguez 2019-05-15 00:56:50 +02:00
parent 9678065e17
commit 42d31a20ba
2 changed files with 11 additions and 7 deletions

View 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.

View File

@ -23,13 +23,13 @@
(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))]