ampc
ampc

Reputation: 312

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? 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

Upvotes: 1

Views: 370

Answers (1)

Apanatshka
Apanatshka

Reputation: 5958

Yes, I'm afraid both of those are old(er) material (than 0.15). Elm 0.15 uses core 2.0.1, in which (as the version suggests) there are breaking changes.
The one you're running into is that head and tail now return a Nothing instead of crashing on empty lists. When the list isn't empty, you get the head/tail, wrapped in a Just. These two constructors are of the Maybe type.

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:

Upvotes: 2

Related Questions