(ns untitled1.core
(:require [clojure.string :as str]))
(defn nottrue ;helper function for below function
[inp]
(not (true? inp))
)
(defn and-simplify
"function that, for example, takes: (and-simplify '(and true false)) and evaluates it -> false.
This function works perfectly if called directly from REPL."
[last]
(cond
(some false? last) false
(every? true? (rest last)) true
(= 1 (count (filter nottrue (rest last)))) (let [xyz (filter nottrue (rest last))] xyz)
:else (let [xxx (filter nottrue last)] xxx)
) )
(defn concact_function
"When the user types: (concact_function '(and false true) '(and true true true false)). It should
return -> Concacted Version: (and true true true false)"
[my_expression original]
(println "Concacted Version: " (concat (drop-last original) (and-simplify my_expression)))
)
当我输入时:(concact_function'(and false true)'(and true true true false))
返回此值:在untitled1.core / concact-function(core.clj:26)处执行错误(IllegalArgumentException)。 不知道如何从以下位置创建ISeq:java.lang.Boolean 精简版:(和true true
我做了一些调试,发现问题出在我试图简洁化(并简化我的表达)时。和-简化完全可以直接调用时实现预期的效果,但由于某种原因不喜欢简洁。
(我实际上不需要打印它,我只需要将其返回以进行进一步的操作即可。但是出于可视化的考虑,我将其打印出来)
答案 0 :(得分:0)
concat
函数可用于集合,函数and-simplify
仅返回布尔值。要解决此问题,您可以将整个cond子句放入向量或列表中,或者将每个cond子句的返回放入向量中。
使用以下修复程序:
(defn and-simplify
"function that, for example, takes: (and-simplify '(and true false)) and evaluates it -> false.
This function works perfectly if called directly from REPL."
[last]
[(cond
(some false? last) false
(every? true? (rest last)) true
(= 1 (count (filter nottrue (rest last)))) (let [xyz (filter nottrue (rest last))] xyz)
:else (let [xxx (filter nottrue last)] xxx)
)])
将返回预期结果:
Concacted Version: (and true true true false)