我应该写一个2个参数的函数,它将第一个数字从base转换10为base b。返回的值应该是一个集合。
我尝试使用格式,但如果b是2,9,0等,我不知道如何使用。
(defn f[x y]
(cond
(= y 8)(format "octal %o" x)
(= y 16)(format "hex %x" x)
(= y 10)(format "decimal %d" x)
:else 0))
Run Code Online (Sandbox Code Playgroud)
鉴于您无需手动进行转换,因此可以使用Java的 Integer.toString
例如
user> (Integer/toString 123456 13) ;; 123456 in base 13
"44268"
Run Code Online (Sandbox Code Playgroud)
至于序列部分,您可以执行以下操作:(seq (Integer/toString numb base)
但是,这将返回字符(0-9和az)。您可能需要一个查找函数来获取数字。
另请查看toString文档以了解允许的半径。
另一方面,如果要求手动转换,则此 MO文章可能是一个好的开始。
| 归档时间: |
|
| 查看次数: |
2656 次 |
| 最近记录: |