错误的争论数量

时间:2015-03-04 23:39:42

标签: clojure first-class-functions seesaw

我正在构建一个程序,它允许用户计算字符串中的字母数或单词数,但是当通过cmd运行程序时,我得到一个clojure.lang.ArityException,错误的数字args(1)传递给:core / -main / counter - 5333

我的代码是

;;Create a GUI which allows user to input a string and to select "word count" or "letter count". When "Start" is clicked pass both the string and either (wordCount [string x]) or (letterCount [string x]) to 
;;declare functions as variables
;;show function that takes functions as parameters
;;show function that returns another function
(ns firstclass.core
  (:gen-class)
  (:use seesaw.core))

(defn -main
  [& args]

(def strInput (input "Please enter a string to be evaluated"))

(def groups (button-group))
(def s (selection groups))

(def letterRadio (radio :text "Letter" :group groups))
(def wordRadio (radio :text "Word" :group groups))

(defn letterCount
  [string]
  (loop [characters string
         a-count 0]
    (if (= (first characters) \a)
      (recur (rest characters) (inc a-count))
      a-count)))

(defn wordCount
  [string]
  (loop [characters string
         a-count 0]
    (if (= (first characters) \a)
      (recur (rest characters) (inc a-count))
      a-count)))

(def counter (fn [fn x, string strInput] (x [strInput])))

(defn handler [event]
    (if-let [s letterRadio]
        (counter [letterCount, strInput]))
    (if-let [s wordRadio]
        (counter [wordCount, strInput])))

(def start (button :text "Start Count" :listen [:action handler] ))

(def panel
  (flow-panel :items [strInput, letterRadio, wordRadio, start]))

(invoke-later
  (-> (frame :content panel :on-close :dispose) 
      pack! show!)))

1 个答案:

答案 0 :(得分:2)

您对counter

的定义
(def counter (fn [fn x, string strInput] (x [strInput])))

你有四个参数的功能

handler函数中,用一个参数调用它:

(counter [letterCount strInput])

从上下文我假设你打算将counter定义为有两个参数,你打算用两个参数调用它,而不是两个项的单个向量。

(def counter (fn [x strInput] (x strInput)))

...

(counter letterCount strInput)

此外,最好使用defn来定义函数,而不是单独使用deffn

(defn counter [x strInput] (x strInput))