Programming

4Clojure 166

steloflute 2012. 12. 8. 19:37

http://www.4clojure.com/problem/166


Comparisons
 

Difficulty:Elementary
Topics:


For any orderable data type it's possible to derive all of the basic comparison operations (<, ≤, =, ≠, ≥, and >) from a single operation (any operator but = or ≠ will work). Write a function that takes three arguments, a less than operator for the data and two items to compare. The function should return a keyword describing the relationship between the two items. The keywords for the relationship between x and y are as follows:
  • x = y → :eq
  • x > y → :gt
  • x < y → :lt

test not run
(= :gt (__ < 5 1))
test not run
(= :eq (__ (fn [x y] (< (count x) (count y))) "pear" "plum"))
test not run
(= :lt (__ (fn [x y] (< (mod x 5) (mod y 5))) 21 3))
test not run
(= :gt (__ > 0 2))





(fn [lt a b] 
    (cond (lt a b) :lt
          (lt b a) :gt
          :else :eq))


'Programming' 카테고리의 다른 글

Getting Started With Clojure  (0) 2012.12.10
(Java) Robot Class  (0) 2012.12.08
solutions for the first 50 problems on 4clojure.com  (0) 2012.12.04
4Clojure - Clojure 문제 연습 사이트  (0) 2012.12.03
(Racket) gui-draw-test.rkt  (0) 2012.11.12