gpt4 book ai didi

从 Haskell 到 Clojure

转载 作者:行者123 更新时间:2023-12-04 07:46:30 25 4
gpt4 key购买 nike

我将在 count down game 上复习这个 haskell 讲座,我不知道任何haskell,但我对这个问题很感兴趣,我正在尝试将他的代码移植到clojure。

这是我被卡住的部分必须是我在haskell中没有得到的东西,


split :: [a] -> [([a],[a])]
split [] = [([],[])]
split (x:xs) = ([],x:xs) : [(x:ls,rs) | (ls,rs) [([a],[a])]
nesplit = filter ne . split

ne :: ([a],[b]) -> Bool
ne (xs,ys) = not (null xs || null ys)

exprs :: [Int] -> [Expr]
exprs [] = []
exprs [n] = [Val n]
exprs ns = [e | (ls,rs)


I have my own split given 1 2 3 4 it spits out,



(((1) (2 3 4)) ((1 2) (3 4)) ((1 2 3) (4)))

(defn split [v]
(if (= (count v) 1)
(list (first v))
(map #(list (take % v) (drop % v)) (range 1 (count v)))))

(defn exprs [v]
(if (= (count v) 1)
v
(map #(concat (exprs (first %)) (exprs (second %))) v)))

(exprs (split [1 2 3 4]))

that gives me,

java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Integer

谁能告诉我haskell代码中我缺少什么?

他的完整代码列表可用 here .

最佳答案

就我有限的 Haskell fu 允许我做的而言,这与 Haskell 实现密切相关......

(defn split
[s]
(map #(split-at % s) (range 1 (count s))))

(defn ne
[s]
(every? (complement empty?) s))

(defn nesplit
[s]
(filter ne (split s)))

(declare combine)

(defn exprs
[s]
(when-let [s (seq s)]
(if (next s)
(for [[ls rs] (nesplit s)
l (exprs ls)
r (exprs rs)
e (combine l r)]
e)
s)))

虽然没有测试过。

至于您的错误信息:我认为问题在于,您没有调用 splitexprs 中递归.然后你得到 1是一个序列是预期的......

其他随机备注: count对于序列而言,时间上是线性的。由于我们只需要知道,我们是否有多个元素,我们可以检查 (next s) 的值。反对 nil .

关于从 Haskell 到 Clojure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1905628/

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