1

我正在构建一个程序,该程序旨在允许用户计算字符串中的字母数或单词数,但是当通过 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!)))
4

1 回答 1

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定义函数,而不是def单独fn定义

(defn counter [x strInput] (x strInput))
于 2015-03-04T23:56:38.523 回答