如何在Elixir中返回列表?

时间:2016-09-19 06:53:22

标签: elixir

我正在编写Elixir编程书并练习练习。我有一个sum函数,我希望在列表[]中返回总和值,而不仅仅是一个数字,但我得到bad argument in arithmetic expression。可以做些方向吗?

defmodule Mymod do
  def sum([]), do: []
  def sum([head | tail]) do
    [head  + sum(tail)]
  end
end

2 个答案:

答案 0 :(得分:3)

问题是Mymod.sum/1会返回一个列表,而您正尝试将其添加到该号码中。

要使其正常工作,您应该将sum返回的列表“揭开”到该号码中,然后添加它:

defmodule Mymod do
  def sum([]), do: []
  def sum([head | tail]) do
    case sum(tail) do
      []  -> [head]
      [s] -> [head + s]
    end
  end
end

IO.inspect Mymod.sum([5, 6, 7])
#⇒ [18]

尾部递归变体看起来像(受@ theanh-le的回答启发):

defmodule Mymod do
  def sum(list, acc \\ 0)

  def sum([], 0), do: []
  def sum([], acc), do: [acc]
  def sum([head | tail], acc) do
    sum(tail, head + acc)
  end
end

IO.inspect Mymod.sum([5, 6, 7])
#⇒ [18]

答案 1 :(得分:1)

有一种方法可以使用 private void DataTypeMappingSet() { List<int> indexes = getPackageAndElementsIndex("DATA-TYPE-MAPPING-SET"); if (indexes != null) { try { ElementTag elementTag = null; int elementId = 0; string elementName = null; String elementNameAndId = null; List<int> elementChildElements = null; ElementTag ParentDiagramtag = null; string parentDiagramName = null; ElementTag parentPackagetag = null; string parentPackageName = null; EA.Package parentPackage; EA.Diagram parentDiagram = null; ; EA.Element DataTypeMappingSetElement; foreach (int index in indexes) { elementTag = parsedFile.store_element[index]; elementId = elementTag.Element_id; elementName = parsedFile.duplicate_element_array [elementTag.Tag_array_ref].Key; elementChildElements = elementTag.Child_index; ParentDiagramtag = parsedFile.store_element[elementTag.Parent_index]; parentDiagramName = parsedFile.duplicate_element_array[ParentDiagramtag.Tag_array_ref].Key; parentDiagramName = parentDiagramName + ParentDiagramtag.Element_id; parentPackagetag = parsedFile.store_element[ParentDiagramtag.Parent_index]; parentPackageName = parsedFile.duplicate_element_array[parentPackagetag.Tag_array_ref].Key; parentPackageName = parentPackageName + parentPackagetag.Element_id; parentPackage = getPackageObjByName[parentPackageName]; parentDiagram = getDiagramObjByName[parentDiagramName]; DataTypeMappingSetElement = (EA.Element)parentPackage.Elements.AddNew("", "Data-Type-Mapping-Set"); DataTypeMappingSetElement.Update(); elementNameAndId = elementName + elementId; getElementObjByName.TryAdd(elementNameAndId, DataTypeMappingSetElement); AddTagValues(DataTypeMappingSetElement, elementChildElements); //EA.DiagramObject DiagOb = (EA.DiagramObject)parentDiagram.DiagramObjects.AddNew("", ""); //DiagOb.ElementID = DataTypeMappingSetElement.ElementID; //DiagOb.Update(); createDataTypeMap(elementTag.Child_index, DataTypeMappingSetElement, parentDiagramName, parentPackage); }); } catch (Exception) { // throw; } } } 来做到这一点,让您的代码更漂亮:

tail recursive

IEX:

defmodule MyMod do
  def sum([]), do: []
  def sum(list) do
    do_sum(0, list)
  end

  defp do_sum(current_sum, []), do:[current_sum] 
  defp do_sum(current_sum, [head | tail]) do
    new_sum = current_sum + head
    do_sum(new_sum, tail)
  end
end