Advertisement
Altair200333

Untitled

Sep 9th, 2022
2,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns task_1)
  2.  
  3. ;; p - prefix to add
  4. ;; a - list of elements
  5. ;; n - onumber of elements (count a)
  6. ;; ex - exclude
  7. (defn permute_add [p, a, n]
  8.   (if (= n 0)
  9.     nil
  10.     (if (= (nth a (- n 1)) (last p))
  11.       (permute_add p a (- n 1))
  12.       (concat
  13.        (conj (permute_add p a (- n 1))
  14.              (concat p (list (nth a (- n 1)))))))))
  15.  
  16. ;; apply permute_add to every item of list
  17. (defn permute_add_list [list, a, n, counter]
  18.   (if (= counter (count list))
  19.     (if (nil? list)
  20.       (permute_add `() a n)
  21.       nil)
  22.     (concat
  23.      (permute_add (nth list counter) a n)
  24.      (permute_add_list list a n (+ counter 1)))))
  25.  
  26. (defn perm_of_len [a, n]
  27.   (if (= n 0)
  28.     nil
  29.     (permute_add_list (perm_of_len a (- n 1)) a (count a) 0)))
  30.  
  31. (perm_of_len `(0 1 2 3) 2)
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement