简体   繁体   English

榆木列表类型不匹配

[英]Elm List type mismatch

I was following an (old?) tutorial and I got a type mismatch. 我正在遵循(旧的?) 教程 ,但类型不匹配。 Has the List library changed from 0.14.1 to 0.15? 列表库是否已从0.14.1更改为0.15? elmpage . elmpage

Code: 码:

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: 类型不匹配:

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

Yes, I'm afraid both of those are old(er) material (than 0.15). 是的,我恐怕这两个都是旧材料(大于0.15)。 Elm 0.15 uses core 2.0.1, in which (as the version suggests) there are breaking changes. Elm 0.15使用核心2.0.1,其中(如版本所建议)存在重大变化。
The one you're running into is that head and tail now return a Nothing instead of crashing on empty lists. 您遇到的一个问题是, headtail现在返回Nothing而不是崩溃在空列表上。 When the list isn't empty, you get the head/tail, wrapped in a Just . 当列表不为空时,头/尾将包裹在Just These two constructors are of the Maybe type. 这两个构造函数属于Maybe类型。

Here's some updated code (which doesn't need 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 []

Sources: 资料来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM