Advertisement
Guest User

Clojure var18

a guest
Dec 16th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Params:
  2. ;   a, b, c, d - input sets
  3. ;   state - variable for state saving
  4. ;   elm - single element used in contains subfunction
  5. (defn megaTask [a b c d  &{:keys [state, elm] :or {state 0 elm 0}}]
  6.     (cond
  7.         ;Subfunction contains
  8.         ;Checks if elm is in list a
  9.         ;Non empty list mean true, empty list - false
  10.         (= state 1) (cond
  11.                             (empty? a) nil
  12.                             (= elm (first a)) '(0)
  13.                             true (megaTask (rest a) b c d :state 1 :elm elm)
  14.                         )
  15.         ;Subfunction union
  16.         ;Creates union of two sets - a and b
  17.         (= state 2) (cond
  18.                         (empty? b) a
  19.                         (megaTask a () () () :state 1 :elm (first b)) (megaTask a (rest b) () () :state 2)
  20.                         true (cons (first b) (megaTask a (rest b) '() '() :state 2))
  21.                       )
  22.         ;Subfunction difference
  23.         ;Computes difference of two sets - a and b
  24.         (= state 3) (cond
  25.                           (empty? a) ()
  26.                           (megaTask b () () () :state 1 :elm (first a)) (megaTask (rest a) b () () :state 3)
  27.                           true (cons (first a) (megaTask (rest a) b '() '() :state 3))
  28.                       )
  29.         ;Subfunction intersection
  30.         ;Computes intersection of two sets - a and b
  31.         (= state 4) (cond
  32.                           (empty? a) ()
  33.                           (megaTask b () () () :state 1 :elm (first a)) (cons (first a) (megaTask (rest a) b () () :state 4))
  34.                           true (megaTask (rest a) b () () :state 4)
  35.                     )  
  36.         ;Computes target result - A union (B difference (C union D))
  37.         true (megaTask (megaTask (megaTask a b () () :state 3) c () () :state 2) d () () :state 3)
  38.     )
  39. )
  40.  
  41. (print (megaTask '(1 2 3 4 5) '(4 5 6 7 8 9) '(7 8 9 10 11 12) '(9 10 11 12)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement