十进制到二进制 Clojure
Decimal to Binary Clojure
我按照这个伪代码递归地将十进制转换为二进制。
findBinary(decimal)
if (decimal == 0)
binary = 0
else
binary = decimal % 2 + 10 * (findBinary(decimal / 2)
这是我试过的:
(defn binary [n]
(loop [res 0]
(if (= n 0)
res
(recur (res (* (+ (mod n 2) 10) (binary (quot n 2)))) )
)
)
)
但是我得到这个错误:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/binary (form-init9002795692676588773.clj:6)
关于如何修复代码以完成任务有什么想法吗?
你的伪代码可以很直接地用 clojure 表达:
(defn find-binary [decimal]
(if (= decimal 0)
0
(+ (mod decimal 2) (* 10 (find-binary (quot decimal 2))))))
示例:
user=> (find-binary 1)
1
user=> (find-binary 2)
10
user=> (find-binary 25)
11001
user=> (find-binary 255)
11111111
你的版本错误在这里:
(recur (res (* (+ (mod n 2) 10) (binary (quot n 2))))
具体来说,投诉是您试图将 res
(其值为 0
)用作函数。
老实说,我不确定如何使用循环重复执行此操作。当我尝试时,它抱怨反复出现不在尾部位置。或许另一个答案可以启发我们!
这里有一个稍微不同的方法,允许使用 recur
:
(defn find-binary [d]
(loop [ decimal d
digits '() ]
(if (= decimal 0)
(Integer. (clojure.string/join (map str digits)))
(recur (quot decimal 2) (conj digits (mod decimal 2))))))
在循环中,我们建立了一个二进制数字的集合,在列表的开头添加每个新数字,以便我们最终得到列表中从左到右所需顺序的数字。当达到终止条件时,我们将数字集合转换为字符串集合,将字符串集合连接在一起成为单个字符串,然后将字符串转换回整数。
我意识到,这是关于旅程而不是结果。但是要
有没有提到: Long/toString
可以给你一个数字的字符串
各种基数。
(Long/toString 123 2)
; → "1111011"
使用recur
:
;; returns a binary number string of the given decimal number
(defn find-binary [decimal]
(loop [n decimal
res ""]
(if (= n 0)
res
(recur (quot n 2)
(str (mod n 2) res)))))
但没有loop
:
(defn find-binary [decimal & {:keys [acc] :or {acc ""}}]
(if (= decimal 0)
acc
(find-binary (quot decimal 2) :acc (str (mod decimal 2) acc))))
原来的尝试是这样的,但是在decimal
的大小上走不了多远:
(defn find-binary [decimal]
(loop [n decimal ;; number to be binarized
res '() ;; collector list
pos 0] ;; position of binary number
(if (= n 0)
(reduce #'+ res) ;; sum up collector list
(recur (quot n 2)
(cons (* (int (Math/pow 10 pos))
(mod n 2))
res)
(+ 1 pos)))))
对于大量使用:
我按照这个伪代码递归地将十进制转换为二进制。
findBinary(decimal)
if (decimal == 0)
binary = 0
else
binary = decimal % 2 + 10 * (findBinary(decimal / 2)
这是我试过的:
(defn binary [n]
(loop [res 0]
(if (= n 0)
res
(recur (res (* (+ (mod n 2) 10) (binary (quot n 2)))) )
)
)
)
但是我得到这个错误:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/binary (form-init9002795692676588773.clj:6)
关于如何修复代码以完成任务有什么想法吗?
你的伪代码可以很直接地用 clojure 表达:
(defn find-binary [decimal]
(if (= decimal 0)
0
(+ (mod decimal 2) (* 10 (find-binary (quot decimal 2))))))
示例:
user=> (find-binary 1)
1
user=> (find-binary 2)
10
user=> (find-binary 25)
11001
user=> (find-binary 255)
11111111
你的版本错误在这里:
(recur (res (* (+ (mod n 2) 10) (binary (quot n 2))))
具体来说,投诉是您试图将 res
(其值为 0
)用作函数。
老实说,我不确定如何使用循环重复执行此操作。当我尝试时,它抱怨反复出现不在尾部位置。或许另一个答案可以启发我们!
这里有一个稍微不同的方法,允许使用 recur
:
(defn find-binary [d]
(loop [ decimal d
digits '() ]
(if (= decimal 0)
(Integer. (clojure.string/join (map str digits)))
(recur (quot decimal 2) (conj digits (mod decimal 2))))))
在循环中,我们建立了一个二进制数字的集合,在列表的开头添加每个新数字,以便我们最终得到列表中从左到右所需顺序的数字。当达到终止条件时,我们将数字集合转换为字符串集合,将字符串集合连接在一起成为单个字符串,然后将字符串转换回整数。
我意识到,这是关于旅程而不是结果。但是要
有没有提到: Long/toString
可以给你一个数字的字符串
各种基数。
(Long/toString 123 2)
; → "1111011"
使用recur
:
;; returns a binary number string of the given decimal number
(defn find-binary [decimal]
(loop [n decimal
res ""]
(if (= n 0)
res
(recur (quot n 2)
(str (mod n 2) res)))))
但没有loop
:
(defn find-binary [decimal & {:keys [acc] :or {acc ""}}]
(if (= decimal 0)
acc
(find-binary (quot decimal 2) :acc (str (mod decimal 2) acc))))
原来的尝试是这样的,但是在decimal
的大小上走不了多远:
(defn find-binary [decimal]
(loop [n decimal ;; number to be binarized
res '() ;; collector list
pos 0] ;; position of binary number
(if (= n 0)
(reduce #'+ res) ;; sum up collector list
(recur (quot n 2)
(cons (* (int (Math/pow 10 pos))
(mod n 2))
res)
(+ 1 pos)))))
对于大量使用: