简体   繁体   中英

Case split on a maybe type on PureScript

I am learning PureScript by followingPureScript 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

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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