Advertisement
Altair200333

Untitled

Sep 9th, 2022
2,461
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns test)
  2.  
  3. "Welcome to the Getting Started REPL! 💜"
  4.  
  5. (comment
  6.   (+ (* 2 2)
  7.      2)
  8.   (Math/abs -1)
  9.   (hello "Calva REPL")
  10.   (defn hello [s]
  11.     (str "Hello " s))
  12.   (range 10)
  13.   "I ♥️ Clojure")
  14.  
  15. (defn dada [x, y]
  16.   (* x y))
  17.  
  18. (dada 2 3)
  19.  
  20. (dada `5 5)
  21.  
  22. (defn teq [x]
  23.   x)
  24.  
  25. (teq 2)
  26.  
  27. ;; peek - get first
  28. ;; rest - all except first
  29.  
  30. (defn mix [a, n]
  31.   (conj (rest a) (peek a)))
  32.  
  33. (mix (list 1 2 3) 2)
  34.  
  35. (defn nth-2 [a, n]
  36.   (nth a n))
  37.  
  38. (defn mix-2 [a, n]
  39.   (repeat n a))
  40.  
  41. (mix-2 "x" 5)
  42.  
  43. (repeat (count (list 1 2 3 4)) "x")
  44.  
  45. (for [x [0 1 2 3 4 5]]
  46.   (* 2 x))
  47.  
  48. (defn mix-3 [a, n]
  49.   (repeat n a))
  50.  
  51. (defn shuffle [a]
  52.   (if (= (count a) 2)
  53.     (list a (cons (peek a) (rest a)))
  54.     "net"))
  55.  
  56. (shuffle (list 1 2))
  57.  
  58. (shuffle (list 1 2 3))
  59.  
  60. (use '[clojure.string :only (join)])
  61.  
  62. (defn ins-str [string substring index]
  63.   (join (list (subs string 0 index) substring (subs string (inc index)))))
  64.  
  65. (ins-str "abcd" "ab" 0)
  66.  
  67. (defn permutations [s]
  68.   (if (= (count s) 1)
  69.     (list s)
  70.     (for [head s rest (permutations (rest s))]
  71.       (cons head rest))))
  72.  
  73. (permutations (list 1 2 3))
  74.  
  75.  
  76. (concat (list 1 2 3) (list 5 4))
  77.  
  78. (defn mix-3 [s]
  79.   (for [head s rest (remove #{head} s)]
  80.     rest))
  81.  
  82. (mix-3 "abc")
  83.  
  84. (cons 4 (list 1 2 3))
  85. (concat (list 1 2 3) (list 4))
  86. (reduce str '(1 2 3))
  87.  
  88. (defn shuffle-2 [a]
  89.   (if (= (count a) 2)
  90.     (list (concat a (cons (peek a) (rest a))))
  91.     (concat
  92.      (cons (first a) (shuffle-2 (rest a)))
  93.      (conj (shuffle-2 (rest a)) (first a)))))
  94.  
  95. (shuffle-2 (list 1 2 3))
  96.  
  97. (defn sum [x]
  98.   (if (= (count x) 2)
  99.     (+ (nth x 0) (nth x 1))
  100.     (+ (first x) (sum (rest x)))))
  101.  
  102. (sum (list 1 2))
  103.  
  104. (sum `(1 2 3))
  105.  
  106. (defn sample-c [a]
  107.   (if (= (count a) 1)
  108.     (list (list (+ (first a) 1) 2 3) (list 1 2 0))
  109.     (concat
  110.      (sample-c (list (first a)))
  111.      (sample-c (rest a)))))
  112.  
  113. (sample-c `(1))
  114. (sample-c `(1 2 3))
  115.  
  116. (apply str (list "The" " " "Brown" " " "asdasd"))
  117.  
  118. (if (= "aa" "bb") "da" "net")
  119.  
  120. (get "abc" 1)
  121.  
  122. (defn join-2 [& args]
  123.   (apply str args))
  124.  
  125. (apply str (apply str (list 'a 'b 'c)) "ab" "assa")
  126.  
  127. ;; ------- ;;
  128.  
  129. (join-2 "a" "b" "AA")
  130. (join-2 (get "aqa" 1) "b" "AA")
  131.  
  132. (defn tqd [a, n]
  133.   a)
  134.  
  135. (tqd "abcd" 3)
  136.  
  137. (defn permutations-2 [s]
  138.   (if (= (count s) 1)
  139.     (list s)
  140.     (for [head s rest (permutations (rest s))]
  141.       (cons head rest))))
  142.  
  143. (permutations-2 (list 1 2 3))
  144.  
  145. ;; p - prefix to add
  146. ;; a - list of elements
  147. ;; n - number of elements (count a)
  148. ;; ex - exclude
  149. (defn per_add [p, a, n]
  150.   (if (= n 0)
  151.     nil
  152.     (if (= (nth a (- n 1)) (last p))
  153.       (per_add p a (- n 1) )
  154.       (concat
  155.        (conj (per_add p a (- n 1) )
  156.              (concat p (list (nth a (- n 1)))))))))
  157.  
  158. (per_add `(0 1 2) (list 1 2 3) 3)
  159.  
  160.  
  161. (defn perm_of_len [a, n]
  162.   (if (= n 0)
  163.     nil
  164.     (per_add (first (perm_of_len a (- n 1))) a n)))
  165.  
  166. (perm_of_len `(0 1 2 3) 4)
  167.  
  168.  
  169. (defn per_list [a, n]
  170.   (if (= n 0)
  171.     nil
  172.     (concat
  173.      (conj (per_list a (- n 1))
  174.            a))))
  175.  
  176. (per_list (list 1 2 3) 3)
  177.  
  178.  
  179. (subs "aa" 0 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement