Víctor R. Escobar
Víctor R. Escobar

Reputation: 425

Case split on a maybe type on PureScript

I am learning PureScript by following PureScript by example, and by chapter 3 (rest of definitions there) there is an exercise to delete duplicate entries from a bog, but I cannot get to work the case split for the Maybe (Nothing/Just e). I checked also the syntax guide but I cannot catch the pitfall and keep getting Unknown data constructor Nothing.

import Prelude    
import Data.AddressBook
import Control.Plus (empty)
import Data.List (List(..), (:), filter, head, foldl, tail)
import Data.Maybe (Maybe, isJust)
import Data.Newtype (overF)

skipIfDup :: AddressBook -> AddressBook -> AddressBook
skipIfDup newBook (Nil : book) = newBook
skipIfDup newBook (entry : book) =
    skipIfDup newerBook book
    where
        newerBook :: AddressBook
        newerBook = 
            case findEntry entry.firstName entry.lastName newerBook of
                Nothing -> newBook
                Just e -> insertEntry e newBook

Upvotes: 1

Views: 159

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80744

"Unknown data constructor Nothing" means just what it says: the compiler doesn't know what Nothing is, where it's defined.

And how do you let it know where it's defined? Same way as with all the other stuff you're using in your program - with an import!

You already have import Data.Maybe (Maybe), but that's not enough: this is importing only the type Maybe, but not its constructors.

To import the Nothing constructor, add it after the type in parens like this:

import Data.Maybe (Maybe(Nothing), isJust)

Or you can import all constructors that exist by using two dots instead:

import Data.Maybe (Maybe(..), isJust)

The latter is the more accepted pattern.

Upvotes: 4

Related Questions