4Clojure 21: Nth Element

Nth Element

Write a function which returns the Nth element from a sequence.

#(loop [l %1 n %2] (if (< n 1) (first l) (recur  (rest l) (- n 1))))


Infix Calculator
Your friend Joe is always whining about Lisps using the prefix notation for math. Show him how you could easily write a function that does math using the infix notation. Is your favorite language that flexible, Joe?Write a function that accepts a variable length mathematical expression consisting of numbers and the operations +, -, *, and /. Assume a simple calculator that does not do precedence and instead just calculates left to right.

(fn [a & args] 
  (loop [ops args final a] 
    ( if (empty? ops) 
      final 
      (recur (rest(rest ops)) 
             ((first ops) final (second ops))))))

Rotate Sequence
Difficulty: Medium
Topics: seqs

Write a function which can rotate a sequence in either direction.
(= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2))
(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))
(= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1))
(= (__ 1 '(:a :b :c)) '(:b :c :a))
(= (__ -4 '(:a :b :c)) '(:c :a :b))

(fn [n l] 
  (let [d (mod n (count l)) [x y] (split-at d l)] 
        (concat y x)))


你可能感兴趣的:(4Clojure 21: Nth Element)