Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:17:14+00:00 2026-06-03T04:17:14+00:00

Possible Duplicate: How to make a type with restrictions Is it possible in Haskell

  • 0

Possible Duplicate:
How to make a type with restrictions

Is it possible in Haskell to create a type for example “Name” which is a String but containing no more then 10 letters?

If not how can I forbid to create a Person with to long name (where Person is defined like that: data Person = Person Name).

Maybe it is not important at all, maybe that kind of problems should be solved in Haskell in a different way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:17:15+00:00Added an answer on June 3, 2026 at 4:17 am

    Don’t export the constructor from the module where you define the type, and instead export a “smart constructor”:

    module Name (Name(), -- exports the type Name, but not the data constructor Name
                 nameFromString,
                 stringFromName)
    where
    
    data Name = Name String
    
    -- this is the only way to create a Name
    nameFromString :: String -> Maybe Name
    nameFromString s | 10 < length s = Nothing
                     | otherwise     = Just (Name s)
    
    -- this is the only way to access the contents of a Name
    stringFromName :: Name -> String
    stringFromName (Name s) = s
    

    So you’re concerned that if you previously had code that didn’t require names to be limited to ten characters, you can’t just drop in nameFromString as it has type String -> Maybe Name instead of String -> Name.

    First, if you really want to throw an exception, you can define

    import Data.Maybe (fromMaybe)
    
    nameFromString' :: String -> Name
    nameFromString' = fromMaybe (error "attempted to construct an invalid Name") . nameFromString
    

    and use that instead.

    Second, throwing an exception is sometimes the wrong thing to do. Consider

    askUserForName :: IO Name
    askUserForName
       = do putStr "What's your name? (10 chars max)  "
            s <- getLine
            case nameFromString s of
                Just n  -> return n
                Nothing -> askUserForName
    

    Rewriting this to use exceptions would result in more complex code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Do the parentheses after the type name make a difference with new?
Possible Duplicate: Platform independent paths in Java I make a program but it save
Possible Duplicate: Replace individual list elements in Haskell? I have managed to make some
Possible Duplicate: format xml string I'm generating an XML page like so: header('Content-Type: text/html');
Possible Duplicate: Make iPhone app paid version replace free version on install from app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.