gpt4 book ai didi

linux - 参数声明列表。在 Clojure 中应该是一个向量

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:08 26 4
gpt4 key购买 nike

<分区>

我正在尝试为我的类(class)做我的 CS 实验室,但每次我尝试运行我的测试与我的代码时,我的代码永远不会加载。它显示以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Parameter declaration List. should be a vector
at clojure.core$assert_valid_fdecl.invoke(core.clj:6567)
at clojure.core$sigs.invoke(core.clj:220)
at clojure.core$defn.doInvoke(core.clj:294)
at clojure.lang.RestFn.invoke(RestFn.java:494)
at clojure.lang.Var.invoke(Var.java:431)
at clojure.lang.AFn.applyToHelper(AFn.java:178)
at clojure.lang.Var.applyTo(Var.java:532)
at clojure.lang.Compiler.macroexpand1(Compiler.java:6366)
at clojure.lang.Compiler.macroexpand(Compiler.java:6427)
at clojure.lang.Compiler.eval(Compiler.java:6495)
at clojure.lang.Compiler.load(Compiler.java:6952)
at clojure.lang.RT.loadResourceScript(RT.java:359)
at clojure.lang.RT.loadResourceScript(RT.java:350)
at clojure.lang.RT.load(RT.java:429)

抱歉,这是我的代码。我不知道哪里出了问题,我觉得一切正常。

(ns linked_list_lab.core)

;; We start with a defrecord. In traditional lisps, the
;; first element of a list is called the "car", standing for
;; "Contents of Address Register," the name of the register where
;; it was stored. The pointer to the rest of the list was called
;; the "cdr," for "Contents of Decrement Register", and is pronounced
;; "could-er".

(defrecord Cons [car cdr])

;; One problem with singly linked lists is that finding the length is
;; expensive $${\cal O}(n)$$. We can use a wrapper class to deal with
;; that. Here we have a `List` record that keeps a pointer to the list
;; along with the size.

(defrecord List [data size])

;; The `make-list` function just creates an empty list.

(defn make-list
"Create an empty list."
(List. nil 0))

;; The `insert-front` function shows a special syntax Clojure has.
;; If you know the argument to your function should be a record or a
;; hash-map, you can use this pattern-matching syntax as a shorthand.
;; Here we have `{:keys [data size]}`, which will create new variables
;; `data` and `size`. So if I pass in `{:data 10 :size 20}`, then
;; `data` will be given 10, and `size` will be given 20.
;; This is often called "destructuring".

;; Test broke-1 will not increment the size.
;; Test broke-2 will forget to do the cons.
;; Test broke-3 will replace the cons and not point to the next one.

(defn insert-front
"Insert an element at the beginning of the list."
[{:keys [data size]} new-elt]
(List. (Cons. new-elt data) (+ 1 size)))

;; Here are some utility functions that convert Clojure lists to
;; our Cons. record, and vice-versa. The broke versions will not
;; mess with these.

(defn list-to-cons
[xx]
(cond (empty? xx) nil
:else (Cons. (first xx) (list-to-cons (next xx)))))

(defn cons-to-list
[xx]
(cond (nil? xx) '()
:else (cons (:car xx) (cons-to-list (:cdr xx)))))

;; The `insert-sorted` function assumes that the elements are orderable
;; and puts the element in the spot that will preserve the ordering.

;; Test broke-4 will use `(Cons. elt (:cdr xx))` in the third case.
;; Test broke-5 will use `(Cons. (:car xx) nil)` in the second case.

(defn insert-ordered-cons
"Insert the element `elt` into an ordered `Cons.` chain.
This is used by `insert-ordered`."
[elt xx]
(cond (empty? xx) (Cons. elt nil)
(> elt (:car xx)) (Cons. (:car xx) (insert-ordered-cons elt (:cdr xx)))
:fine-be-that-way (Cons. elt xx)))

(defn insert-ordered
"Insert an element into an ordered list."
[{:keys [data size]} new-elt]
(List. (insert-ordered-cons new-elt data) (+ size 1)))

;; The `delete` function will delete one element from the list.

;; Test broke-6 will truncate the list past the deletion point.
;; Test broke-7 will forget to decrement the size.
;; Test broke-8 will always decrement the size, even if the element is not found.

(defn delete
"Delete `elt` from `xx`."
[elt xx]
(cond (empty? xx) nil
(= elt (:car xx)) (:cdr xx)
:else (Cons. (:car xx) (delete elt (:cdr xx))) )
)

;; The `delete-all` function will delete all copies of elt from xx.

;; Test broke-9 will delete only one copy.
;; Test broke-10 will decrement the count instead of properly subtracting the
;; number of deletions.

(defn delete-all
"Delete all occurrences of `elt` from `xx`."
[elt xx]
(cond (empty? xx) nil
(= elt (:car xx)) (delete-all elt (:cdr xx))
:else (Cons. (:car xx) (delete-all elt (:cdr xx)))
)
)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com