Clojure中的空映射
当我在clojure中将数据传递到两个哈希映射时,我一直得到空映射,我知道数据是通过函数发送的,因为我使用了打印语句来显示正确的数据,只是当我将数据关联到映射时,它似乎没有做任何事,留给我{} 有人能看出我做错了什么吗Clojure中的空映射,clojure,hashmap,Clojure,Hashmap,当我在clojure中将数据传递到两个哈希映射时,我一直得到空映射,我知道数据是通过函数发送的,因为我使用了打印语句来显示正确的数据,只是当我将数据关联到映射时,它似乎没有做任何事,留给我{} 有人能看出我做错了什么吗 (defn sort-string [y] (apply str (sort y))) (defn get-unique [y] (let [x (sort-string (str/lower-case y)) hashmap1 (hash-map)
(defn sort-string [y]
(apply str (sort y)))
(defn get-unique [y]
(let [x (sort-string (str/lower-case y))
hashmap1 (hash-map)
hashmap2 (hash-map)]
(if-not (contains? hashmap1 x)
(assoc hashmap1 x, y)
(assoc hashmap2 y, y))
(if-not (get hashmap1 x) y)
(dissoc hashmap2 (get hashmap1 x))))
(for [strings '("door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#")]
(get-unique strings))
Clojure映射是不可变的。因此,返回修改的映射的
assoc
s无效,因为未使用返回的值
我不知道你想做什么,但以下几点可能会让你走上正确的道路:
(defn get-unique [y]
(let [x (sort-string (str/lower-case y))
hashmap1 (hash-map)
hashmap2 (hash-map)
[hashmap1 hashmap2] (if-not (contains? hashmap1 x)
[(assoc hashmap1 x, y) (assoc hashmap2 y, y)]
[hashmap1 hashmap2])]
(if-not (= (get hashmap1 x) y)
(dissoc hashmap2 (get hashmap1 x))
hashmap2)))
比如说,
(for [strings '("door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#")]
(get-unique strings))
;({"door" "door"} {" rood" " rood"} {"pen" "pen"} {"open" "open"} {"high" "high"} {"low" "low"} {"wall" "wall"} {"lawl" "lawl"} {"#" "#"})
(let [strings '("door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#")]
(group-by sort-string strings))
现在,您的评论告诉我您正在尝试将字谜组合在一起,使用
排序字符串
测试等效性
您可以使用分组方式
执行此操作。比如说,
(for [strings '("door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#")]
(get-unique strings))
;({"door" "door"} {" rood" " rood"} {"pen" "pen"} {"open" "open"} {"high" "high"} {"low" "low"} {"wall" "wall"} {"lawl" "lawl"} {"#" "#"})
(let [strings '("door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#")]
(group-by sort-string strings))
。。。生产
{"door" ["door" "rood"], "enp" ["pen"], "enop" ["open"], "ghhi" ["high"], "low" ["low"], "allw" ["wall" "lawl"], "#" ["#"]}
我认为您犯的错误是假定
hashmap1
和hashmap2
是可变的assoc
返回一个新的hashmap,其中包含关联的键和值,但原始值保持不变
您可以在这样的repl中看到这一点:
user=> (def my-map {:a 1 :b 2})
#'user/my-map
user=> my-map
{:a 1, :b 2}
user=> (assoc my-map :c 3)
{:a 1, :b 2, :c 3}
user=> my-map
{:a 1, :b 2}
这意味着您的区块:
(if-not (contains? hashmap1 x)
(assoc hashmap1 x, y)
(assoc hashmap2 y, y))
只是创建了一个新的hashmap,其中x作为键,y作为值,并没有做任何处理
也许您可以编写另一个函数来创建新地图并按如下方式返回:
(defn add-if-not-contains [m1 m2 x y]
(if-not (contains? m1 x)
[(assoc m1 x y) m2]
[m1 (assoc m2 y y)]))
但我认为你的解决方案仍然过于复杂。我猜您需要一个在列表中没有任何其他排列的单个排序元素
user=> (def my-strings ["door" " rood" "pen" "open" "high" "low" "wall" "lawl" "#"])
#'user/my-string
user=> (distinct (map sort-string my-strings))
("door" " door" "enp" "enop" "ghhi" "low" "allw" "#")
如果你只想得到没有排列的单词,你可以试试这样的
(frequencies (map sort-string ["door" "rood" "odor" "pen"]))
{"door" 3, "enp" 1}
过滤该结果以查找值为1的条目,并从排列中查找原始单词作为练习。是否有方法将X作为键,Y作为值添加到我的地图中???@Elliottsoughiott不要使用来自
assoc
的返回值,如上所述,在@Munk的回答中。这是我需要的,但我只想返回唯一的字符串,所以“门外”rood“气味”pen“only pen”返回,因为其他三个词可以组成其他词rood=door+odor,但pen是唯一的,因为没有其他词匹配pen。您希望从中得到什么输出?小问题:(散列图)
是一种非常规的书写方式{},该包含?
检查将永远不会返回true,因为它正在检查的哈希映射在所有情况下都是100%保证为空的。根据您的评论,您正在尝试将anagram分组在一起,使用排序字符串
生成对所有anagram都相同的内容,而不包含其他内容。请合并t有了这个新函数,我是否可以在get unique函数中运行它,然后我就可以使用类似于tihs(defn print map[m](prn“print map”,m)(for[[x,y](seq m)](println(str x:“y)))的东西打印地图了
我不确定你到底在想什么,但我怀疑你还没有理解不变性的要点。在repl中尝试一下,看看它能做什么。我试图做的是输入“door”“rood”“odor”“pen”。只返回“pen”,就像其他人被排序时是同一个词。“pen”“是唯一一根不能用任何其他弦组成的弦