PasteRack.org
Paste # 13278
2015-04-25 00:11:47

Fork as a new paste.

Paste viewed 94 times.


Embed:

  1. #lang racket
  2.  
  3. (define (cmp op v_1 v_2)
  4.   (define sp (match (quote ,op)
  5.                [`< string<?]
  6.                [`<= string<=?]
  7.                [`> string>?]
  8.                [`>= string>=?]
  9.                [else string=?]))
  10.   (define tp (if (equal? op eq?) `== op))
  11.   (cond
  12.     [(and (or (null? v_1) (string? v_1)) (string? v_2))
  13.      (if (and (is-numerical v_1) (is-numerical v_2))
  14.          `(,tp (to-number ,(if (null? v_1) `(to-string ,v_1) v_1))
  15.                (to-number ,v_2))
  16.          (if (and (string? v_1) (string? v_2))
  17.              (sp v_1 v_2)
  18.              `(,tp ,(if (null? v_1) `(to-string ,v_1) v_1) v_2)))]
  19.     [(and (string? v_1) (or (null? v_2) (string? v_2)))
  20.      (if (and (is-numerical v_1) (is-numerical v_2))
  21.          `(,tp (to-number ,v_1)
  22.                (to-number ,(if (null? v_2) `(to-string ,v_2) v_2)))
  23.          (if (and (string? v_1) (string? v_2))
  24.              (sp v_1 v_2)
  25.              `(,tp ,v_1 ,(if (null? v_2) `(to-string ,v_2) v_2))))]
  26.     [(and (boolean? v_1) (null? v_1)) `(,tp ,v_1 (to-bool ,v_2))]
  27.     [(and (boolean? v_2) (null? v_2)) `(,tp (to-bool ,v_1) ,v_2)]
  28.     [else (op (to-number v_1) (to-number v_2))]))

=>