Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
List 如何列出。对列表求和(可能是浮动)?_List_Elm - Fatal编程技术网

List 如何列出。对列表求和(可能是浮动)?

List 如何列出。对列表求和(可能是浮动)?,list,elm,List,Elm,我正在尝试将字符串转换为浮点数并找到它们的和: >String.split“,“0.2,0.3,3.1” >List.map String.toFloat[“0.2”、“0.3”、“3.1”] >List.sum[只有0.2,只有0.3,只有3.1] 但我得到了这些编译器消息: This argument is a list of type: List (Maybe Float) But `sum` needs the 1st argument to be: List n

我正在尝试将字符串转换为浮点数并找到它们的和:

>String.split“,“0.2,0.3,3.1”
>List.map String.toFloat[“0.2”、“0.3”、“3.1”]
>List.sum[只有0.2,只有0.3,只有3.1]
但我得到了这些编译器消息:

This argument is a list of type:

    List (Maybe Float)

But `sum` needs the 1st argument to be:

    List number

Hint: Use Maybe.withDefault to handle possible errors. Longer term, it is
usually better to write out the full `case` though!

如何获得这些值的总和?

正如错误消息所指出的,您需要处理
Nothing
的情况。假设您只是想丢弃这些值,您可以通过
list.filterMap
运行列表

>l=[只有0.2,只有0.3,只有3.1,什么都没有]
[只有0.2,只有0.3,只有3.1]
:列表(可能是浮动)
>List.filterMap标识l |>List.sum
3.6:浮动
您可以编写一个函数以重用逻辑:

> justSum = List.filterMap identity >> List.sum
<function> : List (Maybe number) -> number
> justSum l
3.6 : Float
>justSum=List.filterMap identity>>List.sum
:列表(可能是编号)->编号
>贾斯特苏姆l
3.6:浮动

您已经创建了一个
列表(可能是浮动的)
,但是您需要一个
列表浮动的
number
是一种特殊类型,可以是
Int
Float
)。这意味着您需要处理列表中的值为
Nothing
的情况。错误消息建议使用
Maybe.withDefault
,如下所示:

"0.2,0.3,3.1"
|> String.split ","
|> List.map String.toFloat
|> List.map (Maybe.withDefault 0)
|> List.sum
"0.2,0.3,3.1,h"
import Maybe.Extra

"0.2,0.3,3.1"
  |> String.split ","
  |> List.map String.toFloat
  |> Maybe.Extra.combine
  |> Maybe.map List.sum
或者,被设计为使用一个函数,该函数返回一个
可能
并删除
Nothing
值。在本例中,
String.toFloat
就是这种函数,因此您可以改为:

"0.2,0.3,3.1"
|> String.split ","
|> List.filterMap String.toFloat
|> List.sum

String.toFloat
在字符串不是有效浮点时返回
Nothing
。您需要决定当您有这样一个字符串时会发生什么:

"0.2,0.3,3.1"
|> String.split ","
|> List.map String.toFloat
|> List.map (Maybe.withDefault 0)
|> List.sum
"0.2,0.3,3.1,h"
import Maybe.Extra

"0.2,0.3,3.1"
  |> String.split ","
  |> List.map String.toFloat
  |> Maybe.Extra.combine
  |> Maybe.map List.sum
因为它包含不是数字的
h
,所以运行
String.split“,”
然后
List.map String.toFloat
将产生以下结果:

[Just 0.2, Just 0.3, Just 3.1, Nothing]
其他答案建议您使用
List.filterMap
忽略无效字符串。但是,如果您希望总和为
Nothing
,如果任何字符串无效,您可以使用如下方法:

"0.2,0.3,3.1"
|> String.split ","
|> List.map String.toFloat
|> List.map (Maybe.withDefault 0)
|> List.sum
"0.2,0.3,3.1,h"
import Maybe.Extra

"0.2,0.3,3.1"
  |> String.split ","
  |> List.map String.toFloat
  |> Maybe.Extra.combine
  |> Maybe.map List.sum

您可以使用以下功能:

sumMaybes : List (Maybe number) -> Maybe number
sumMaybes = List.foldl (Maybe.map2 (+)) (Just 0)

-- examples

sumMaybes [Just 1, Just 2] == Just 3
sumMaybes [Nothing, Just 2] == Nothing
sumMaybes [] = Just 0