榆树列表类型不匹配
Elm List type mismatch
我在关注一个(旧的?)tutorial and I got a type mismatch. Has the List library changed from 0.14.1 to 0.15? elmpage。
代码:
module Fibonacci where
import List exposing (..)
fibonacci : Int -> List Int
fibonacci n =
let fibonacci1 n acc =
if n <= 2
then acc
else fibonacci1 (n-1) ((head acc + (tail >> head) acc) :: acc)
in
fibonacci1 n [1,1] |> reverse
类型不匹配:
Type mismatch between the following types on line 11, column 40 to 69:
number
Maybe.Maybe a
It is related to the following expression:
(head acc) + ((tail >> head) acc)
Type mismatch between the following types on line 11, column 52 to 64:
Maybe.Maybe
List
It is related to the following expression:
tail >> head
是的,恐怕这两个都老了(呃)material(比 0.15)。 Elm 0.15 使用核心 2.0.1,其中(如版本所示)有重大变化。
您 运行 喜欢的是 head
和 tail
现在 return 和 Nothing
而不是在空列表上崩溃。当列表不为空时,您会得到 head/tail,包裹在 Just
中。这两个构造函数都是Maybe
类型的。
这是一些更新的代码(不需要 head/tail):
fibonacci : Int -> List Int
fibonacci goal =
let fibonacciHelp n a b fibs =
if n >= goal
then List.reverse fibs
else fibonacciHelp (n+1) (a + b) a (a :: fibs)
in
fibonacciHelp 0 1 0 []
来源:
- Similar question on the mailing list
- Source of the above code snippet 在那次讨论中,由 Evan Czaplicki 撰写
我在关注一个(旧的?)tutorial and I got a type mismatch. Has the List library changed from 0.14.1 to 0.15? elmpage。
代码:
module Fibonacci where
import List exposing (..)
fibonacci : Int -> List Int
fibonacci n =
let fibonacci1 n acc =
if n <= 2
then acc
else fibonacci1 (n-1) ((head acc + (tail >> head) acc) :: acc)
in
fibonacci1 n [1,1] |> reverse
类型不匹配:
Type mismatch between the following types on line 11, column 40 to 69:
number
Maybe.Maybe a
It is related to the following expression:
(head acc) + ((tail >> head) acc)
Type mismatch between the following types on line 11, column 52 to 64:
Maybe.Maybe
List
It is related to the following expression:
tail >> head
是的,恐怕这两个都老了(呃)material(比 0.15)。 Elm 0.15 使用核心 2.0.1,其中(如版本所示)有重大变化。
您 运行 喜欢的是 head
和 tail
现在 return 和 Nothing
而不是在空列表上崩溃。当列表不为空时,您会得到 head/tail,包裹在 Just
中。这两个构造函数都是Maybe
类型的。
这是一些更新的代码(不需要 head/tail):
fibonacci : Int -> List Int
fibonacci goal =
let fibonacciHelp n a b fibs =
if n >= goal
then List.reverse fibs
else fibonacciHelp (n+1) (a + b) a (a :: fibs)
in
fibonacciHelp 0 1 0 []
来源:
- Similar question on the mailing list
- Source of the above code snippet 在那次讨论中,由 Evan Czaplicki 撰写