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.
test not run
(= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2))
test not run
(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))
test not run
(= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1))
test not run
(= (__ 1 '(:a :b :c)) '(:b :c :a))
test not run
(= (__ -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)))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值