Advertisement
bs9o

task1

Apr 1st, 2022
2,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (use 'clojure.string)
  2.  
  3. ;tasks 1.1 & 1.2
  4.  
  5. (defn n-str-No-adj-rec
  6.   ([lang n]
  7.    (if (= n 1)
  8.      lang
  9.      (n-str-no-adj-rec lang n 0 lang)))
  10.   ([lang n i res]
  11.    (cond
  12.      (= (count (last res)) n) res
  13.      (= i (count lang)) (recur lang n 0 (take (- (count res) 1) res))
  14.      true (if
  15.             (ends-with? (last res) (nth lang i))
  16.             (recur lang n (inc i) res)
  17.             (recur lang n (inc i) (cons (.concat (last res) (nth lang i)) res))))))
  18.  
  19. ;task 1.3
  20.  
  21. (defn my-map
  22.   [f coll]
  23.   (reduce (fn [l x] (cons (f x) l)) '() (reverse coll)))
  24.  
  25. (defn my-filter [pred coll]
  26.   (reduce (fn [l x] (if (pred x)
  27.                       (cons x l)))
  28.           '() (reverse coll)))
  29.  
  30. ;task 1.4
  31.  
  32. (defn my-concat1 [lang s]
  33.   (map (fn [x] (.concat s x)) (filter (fn [x] (not (ends-with? s x))) lang)))
  34.  
  35. (defn my-concat2 [lang coll]
  36.   (reduce concat (map (fn [x] (my-concat1 lang x)) coll)))
  37.  
  38. (defn n-str-no-adj
  39.   ([lang n]
  40.    (if
  41.      (= n 1)
  42.      lang
  43.      (n-str-no-adj lang n lang)))
  44.   ([lang n res]
  45.    (reduce (fn [x _] (my-concat2 lang x)) res (range (- n 1)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement