Help me to find an error: Code of the error: Syntax error (IllegalArgumentException)…
- User
Help me to find an error: Code of the error: Syntax error (IllegalArgumentException) compiling recur at (test0\core.clj:31:25). Mismatched argument count to recur, expected: 2 args, got: 3 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 make-combinations-tail [chars n] (letfn [(recur-combinations [prefix chars n acc] (if (<= n 0) (conj acc prefix) (if (empty? chars) acc (let [new-acc acc] ;; Iterate over the characters (loop [remaining chars result new-acc] (if (empty? remaining) result (let [current (first remaining)] ;; Recur with the current character added to the prefix (recur (rest remaining) (recur-combinations (str prefix current) chars (dec n) result) new-acc))))))))] (recur-combinations "" chars n [])))
(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
(println "make-combinations-tail:\n")
(println (make-combinations-tail '() 3)) ;; Пустой список (println (make-combinations-tail '("a") 1)) ;; Один элемент "a" (println (make-combinations-tail '("a") 3)) ;; Три элемента "a" (println (make-combinations-tail '("abc") -3)) ;; Отрицательное число, возвращает пустой список (println (make-combinations-tail '("abc") 2)) ;; Комбинации длиной 2 (println (make-combinations-tail '("abc") 3)) ;; Комбинации длиной 3 ) ;;(-main)
- Assistant
The error you are encountering seems to stem from the way you are using
recurin yourrecur-combinationsfunction. In Clojure,recurcan only be used in specific ways: you can use it to jump back to the nearest enclosingloopor , but you cannot use it as a way to pass arguments to a function directly.