我正在关注(旧?)tutorial并且我遇到类型不匹配。列表库是否从0.14.1更改为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
答案 0 :(得分:2)
是的,我担心这些都是旧的(呃)材料(比0.15)。 Elm 0.15使用核心2.0.1,其中(如版本所示)存在重大变化
您遇到的问题是head
和tail
现在返回Nothing
而不是在空列表上崩溃。当列表不为空时,你得到头部/尾部,包裹在Just
中。这两个构造函数属于Maybe
类型。
这是一些更新的代码(不需要头/尾):
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 []