Snippets

lion 137 clojurePersistentLists

Created by lion 137
;;Basic implementation of operations on persistent lists: concatenation and update element.

(defn my-concat [xs ys] (if (empty? xs) ys 
		(cons (first xs) (my-concat (rest xs) ys))))
        

(defn my-update [xs n ys] (cond 
				(empty? xs) (throw (Exception. "Can't change empty list!"))
				(= n 0) (cons ys (rest xs))
				:else
					(cons (first xs) (my-update (rest xs) (dec n) ys))))

(def lst '(A B C))
(def lst1 '(D E F))

(my-concat lst lst1);;=>(A B C D E F)
(my-update lst1 2 'Z);;=>(D E Z)
lst ;;=> (A B C)
lst1 ;;=>(D E F)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.