【发布时间】:2019-09-20 12:55:12
【问题描述】:
我正在研究长生不老药的一个基本问题 - 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
运行解决方案时,我收到错误,因为调用 _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
有没有办法强制长生不老药在分裂成头尾时将值保留为字符?
【问题讨论】:
-
旁注:以下划线开头的公共函数命名是反惯用的。将
defp用于私有函数并将它们称为do_rna而不是_rna,这是常规的。
标签: elixir