2024年Clojure调查!分享您的想法。

欢迎!请参阅关于页面获取更多有关工作方式的详细信息。

0
语法和阅读器
(defn get-pins [observed]
  (let [num (count observed)
        keypad {\1 [1 2 4] \2 [2 1 5 3] \3 [3 2 6]
                \4 [4 1 5 7] \5 [5 2 6 8 4] \6 [6 3 5 9]
                \7 [7 4 8] \8 [8 5 9 0 7] \9 [9 6 8] \0 [0 8]}
        observed-map (zipmap [:a :b :c :d] (map keypad observed))]
    (case num
      1 (for [a (:a observed-map)]
          (str a))
      2 (for [a (:a observed-map)
              b (:b observed-map)]
         (str a b))
      3 (for [a (:a observed-map)
              b (:b observed-map)
              c (:c observed-map)]
         (str a b c))
      4 (for [a (:a observed-map)
              b (:b observed-map)
              c (:c observed-map)
              d (:d observed-map)]
         (str a b c d))
       "default")))

2 个答案

+1

以下操作将实现您所需的结果

(defn get-pins [observed]
  (let [num (count observed)
        keypad {\1 [1 2 4] \2 [2 1 5 3] \3 [3 2 6]
                \4 [4 1 5 7] \5 [5 2 6 8 4] \6 [6 3 5 9]
                \7 [7 4 8] \8 [8 5 9 0 7] \9 [9 6 8] \0 [0 8]}
        observed-map (zipmap [:a :b :c :d] (map keypad observed))]
    (for [a (:a observed-map) 
          b (or (:b observed-map) [nil]) 
          c (or (:c observed-map) [nil]) 
          d (or (:d observed-map) [nil])] 
      (str a b c d))))

[nil]的值将产生一个单个的“迭代”(带有的值),当调用str时,它会产生一个空字符串。

感谢帮助
0

在这里您提出了什么问题?能详细说明一下吗?

是否可以将情况语句更改为更灵活的解决方案,比如单个for语句,并且仍然能够处理各种长度的字符串。
不可以,for不适用于这种情况。
...