Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 长生不老药字符被视为整数,而不是拆分为头和尾时的字符_Elixir - Fatal编程技术网

Elixir 长生不老药字符被视为整数,而不是拆分为头和尾时的字符

Elixir 长生不老药字符被视为整数,而不是拆分为头和尾时的字符,elixir,Elixir,我正在研究长生不老药RNA转录的一个基本问题。但是,我的解决方案遇到了一些意想不到的行为: defmodule RnaTranscription do @doc """ Transcribes a character list representing DNA nucleotides to RNA ## Examples iex> RnaTranscription.to_rna('ACTG') 'UGAC' """ @spec to_rna([char])

我正在研究长生不老药RNA转录的一个基本问题。但是,我的解决方案遇到了一些意想不到的行为:

defmodule RnaTranscription do
  @doc """
  Transcribes a character list representing DNA nucleotides to RNA

  ## Examples

  iex> RnaTranscription.to_rna('ACTG')
  'UGAC'
  """
  @spec to_rna([char]) :: [char]
  def to_rna(dna) do
    _to_rna(dna)
  end

  def _to_rna([]), do: ''
  def _to_rna([head | tail]), do: [_rna(head) | _to_rna(tail)]

  def _rna(x) when x == 'A', do: 'U' 
  def _rna(x) when x == 'C', do: 'G'
  def _rna(x) when x == 'T', do: 'A'
  def _rna(x) when x == 'G', do: 'C'
end
当解决方案运行时,当使用与guard子句(而不是字符)不匹配的整数调用_rna函数时,会出现错误

The following arguments were given to RnaTranscription._rna/1:

        # 1
        65

    lib/rna_transcription.ex:18: RnaTranscription._rna/1
    lib/rna_transcription.ex:16: RnaTranscription._to_rna/1

有没有一种方法可以强制elixir在拆分为头和尾时将值保留为字符?

您可以使用
代码点操作符:

  def _rna(x) when x == ?A, do: 'U'
  def _rna(x) when x == ?C, do: 'G'
  def _rna(x) when x == ?T, do: 'A'
  def _rna(x) when x == ?G, do: 'C'

严格地说,长生不老药已经保留了角色的价值!字符是一个代码点,它是一个整数。当您在
'A'
上进行匹配时,您在一个字符表上进行匹配,该字符表是一个整数列表。也就是说,您正在尝试将
65
匹配到
[65]

您可以使用
代码点操作符:

  def _rna(x) when x == ?A, do: 'U'
  def _rna(x) when x == ?C, do: 'G'
  def _rna(x) when x == ?T, do: 'A'
  def _rna(x) when x == ?G, do: 'C'

严格地说,长生不老药已经保留了角色的价值!字符是一个代码点,它是一个整数。当您在
'A'
上进行匹配时,您在一个字符表上进行匹配,该字符表是一个整数列表。也就是说,您正在尝试将
65
匹配到
[65]

除了列表
'a'
和字符
之间的差异之外,Michael完美地回答了a
,该代码还有一个隐藏但重要的故障

您使用的递归不是尾部优化的,应该不惜任何代价避免。这通常可能导致堆栈溢出。下面是TCO代码

defmodule
定义到rna(dna),do:do到rna(dna)
defp do_to_rna(acc\\[],[]),do:Enum.reverse(acc)
反转录核糖核酸(acc,[char | tail]),
do:do_to_rna([do_char(char)| acc],tail)
解除do_字符(?A),do:?U
defp do_char(?C),do:?G
解除do_字符(?T),do:?A
分解do_char(?G),do:?C
结束
rna转录到核糖核酸(“ACTG”)
#⇒ 'UGAC'
或者,更好的是,有了理解力

converter=fn
?A->?U
?C->?G
T->A
?G->?C
结束

对于c来说,除了列表
'a'
和字符
之间的区别之外,迈克尔完美地回答了a
,这个代码还有一个隐藏但重要的故障

您使用的递归不是尾部优化的,应该不惜任何代价避免。这通常可能导致堆栈溢出。下面是TCO代码

defmodule
定义到rna(dna),do:do到rna(dna)
defp do_to_rna(acc\\[],[]),do:Enum.reverse(acc)
反转录核糖核酸(acc,[char | tail]),
do:do_to_rna([do_char(char)| acc],tail)
解除do_字符(?A),do:?U
defp do_char(?C),do:?G
解除do_字符(?T),do:?A
分解do_char(?G),do:?C
结束
rna转录到核糖核酸(“ACTG”)
#⇒ 'UGAC'
或者,更好的是,有了理解力

converter=fn
?A->?U
?C->?G
T->A
?G->?C
结束
对于c Sidenote:以下划线开头命名公共函数是反习惯用法。对私有函数使用
defp
,并将它们称为
dou-rna
not
\u-rna
,这是常规用法。旁注:以下划线开头命名公共函数是反习惯用法。对私有函数使用
defp
,并将它们称为
do\u-rna
not
\u-rna
,这是常规的。