sách gpt4 ai đã đi

clojure - 99 Clojure 中的第 9 题(将连续重复的列表元素打包到子列表中)

In lại 作者:太空宇宙 更新时间:2023-11-03 19:04:04 29 4
mua khóa gpt4 Nike

这是处理具有重复元素的单个列表的 nieve 案例,我在处理一些嵌套列表时遇到了麻烦,所以我想先写简单的案例。 所以我有:

    (defn packDuplicatesIntoLists [listOfElements l e]
(if(= e 'nil)
'true
(if(= () listOfElements)
(if
(= e '())
l
(list l e)
)
(if
(= (first listOfElements) (first e) )
(packDuplicatesIntoLists (rest listOfElements) l (cons (first listOfElements) e))
(packDuplicatesIntoLists (rest listOfElements) (list l e) (first listOfElements))
)
)

))

    (packDuplicatesIntoLists '(2) '(1 1) '(2 2)) (packDuplicatesIntoLists '() '(1 1) '(2 2)) (packDuplicatesIntoLists '() '() '()) (packDuplicatesIntoLists '(1 1 1 2 2 2 3 3 3 4 4 4 4) '() '())

但是 (packDuplicatesIntoLists (rest listOfElements) (list l e) (first listOfElements))给我惹麻烦了,

    #'NintyNineProblems.LearnSpace/packDuplicatesIntoLists
((1 1) (2 2 2))
((1 1) (2 2))
()
IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:505

那一行有什么问题?

1 Câu trả lời

所以,它仍然反转 lsit,但尾递归

     (defn packDuplicatesIntoLists [listOfElements l e]
(if(= '() listOfElements)
(cons e l)
(if
(= (first listOfElements) (first e) )
(recur (rest listOfElements) l (cons (first listOfElements) e))
(if (= '() e)
(recur (rest listOfElements) l (list (first listOfElements)))
(recur (rest listOfElements) (cons e l) (list (first listOfElements)))
)
)
)
)

(packDuplicatesIntoLists '(2 2 2 4 4 4 5 5 5 8 8 8 6 9 9 9 9 9) '() '())(packDuplicatesIntoLists '() '() '()) (packDuplicatesIntoLists 'nil '() '())

关于clojure - 99 Clojure 中的第 9 题(将连续重复的列表元素打包到子列表中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16778146/

29 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com