Help me to add a new function make-combinations-tail which will be make the same result…
- User
Help me to add a new function make-combinations-tail which will be make the same result as make-combinations, but the new function must be implemented using tail recursion. And it mustn't use such functions as "map", "reduce", "filter" and "atom" My code: (ns test0.core (:gen-class) ;;(:require [clojure.string :as s]) )
;; Генерация комбинаций символов длины n (defn make-combinations [chars n] ;; Базовый случай для – если n <= 0 (if (<= n 0) (list "") ;; Если chars пуст (if (empty? chars) '() (for [c chars rest (make-combinations chars (dec n))] ;; рекурсивно создаем комбинации длины n-1 (str c rest))))) ;; Соединяем символы
(defn -main [& args]
(println "make-combinations:\n")
(println (make-combinations '() 3)) ;; Пустой список (println (make-combinations '("a") 1)) ;; Один элемент "a" (println (make-combinations '("a") 3)) ;; Три элемента "a" (println (make-combinations '("a" "b" "c") -3)) ;; Отрицательное число, возвращает пустой список (println (make-combinations '("a" "b" "c") 2)) ;; Комбинации длиной 2 (println (make-combinations '("a" "b" "c") 3)) ;; Комбинации длиной 3 ) ;;(-main)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.