【问题标题】:How to get a single digit from an integer in common lisp [duplicate]如何从普通lisp中的整数中获取单个数字[重复]
【发布时间】:2013-01-19 19:21:19
【问题描述】:

可能重复:
Coercing numbers to lists in common lisp

我是普通 lisp 的初学者。

如果我有数字 123456890 并且我想分别获得数字 1 2 3 5 9 我该如何在 common lisp 中做到这一点?

【问题讨论】:

  • 你会如何用任何语言来做?
  • 我还没有真正尝试过任何常见的 lisp。我记得很久以前我曾尝试在 C 中使用 for 循环和 "% 10"s,但无论我如何更改它都无法正常工作,而且当其中一个数字为 0 时也会出现问题。

标签: common-lisp


【解决方案1】:

首先,很明显,这个问题之前已经解决了很多次了,你必须在你问之前寻找解决方案,否则你就很难让别人找到一个好的解决方案。所以,这是一个较旧的答案:Coercing numbers to lists in common lisp

(defun int-to-list (int)
  (assert (>= int 0) nil "Argument must be non-negative")
  (if (= int 0) (list 0)
      (loop with result = nil
         while (> int 0) do
           (multiple-value-bind (divisor remainder)
               (floor int 10)
             (push remainder result)
             (setf int divisor))
         finally (return result))))

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 2020-02-26
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多