榆树列表类型不匹配

时间:2015-05-10 01:29:34

标签: list functional-programming elm

我正在关注(旧?)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

1 个答案:

答案 0 :(得分:2)

是的,我担心这些都是旧的(呃)材料(比0.15)。 Elm 0.15使用核心2.0.1,其中(如版本所示)存在重大变化 您遇到的问题是headtail现在返回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 []

来源: