问号是什么意思?

时间:2016-03-16 21:07:03

标签: elixir

考虑以下模块

defmodule Parse do

  def number([ ?- | tail ]), do: _number_digits(tail, 0) * -1
  def number([ ?+ | tail ]), do: _number_digits(tail, 0)
  def number(str),           do: _number_digits(str, 0)

  defp _number_digits([], value), do: value
  defp _number_digits([ digit | tail ], value) when digit in '0123456789' do
    _number_digits(tail, value*10 + digit - ?0)
  end
  defp _number_digits([ non_digit | _ ], _) do
    raise "Invalid digit '#{[non_digit]}'"
  end

end

当我将文件导入shell并执行以下语句时:

iex(3)> Parse.number('123')

作为输出我已经

123

我真正不明白的是这一行:

value*10 + digit - ?0

是什么意思?0真的是什么意思?我知道,当我在shell上编写?x时,它会向我显示代码点

iex(13)> ?5
53
iex(14)> ?t
116

?0的代码点为-48。上面的示例使用123,第一次迭代编号1的计算看起来像(值* 10 +数字 - ?0):

0 * 10 + 1 - 48 

当我自己计算时,我会得到结果-47但我得到1,为什么?

1 个答案:

答案 0 :(得分:12)

?后跟一个字符可以为您提供该字符的Unicode代码点值。

- ?0函数的第二个子句中完成的_number_digits减去字符的代码点' 0'从数字的代码点值。这样可以方便地为数字' 0'提供整数0,为数字' 1'等提供1。

因此,整个value*10 + digit - ?0表达式首先将运行总计(value)乘以10(因为我们正在处理基数为10的输入字符串),然后添加当前值正在考虑的数字。这是递归发生的,直到输入字符串的所有数字都被消耗,给定输入字符串表示的整数。