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

  • Home
  • SEARCH
  • 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 6639079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:30:44+00:00 2026-05-25T23:30:44+00:00

I have written the following code to remove vowels from a sentence: main =

  • 0

I have written the following code to remove vowels from a sentence:

   main = print $ unixname "The House"

   vowel x = elem x "aeiouAEIOU"

   unixname :: [Char] -> [Char]
   unixname [] = []
   unixname (x:xs) | vowel x = unixname xs
            | otherwise = x : unixname xs

Just wondering if it is possible to create a data type for vowel? The compiler won’t let me use characters in a data type.

  • 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-05-25T23:30:44+00:00Added an answer on May 25, 2026 at 11:30 pm

    Not directly. The problem is that characters are a built-in type with no facility for polymorphism. This is different from numeric literals, which are designed to be polymorphic via the Num type class.

    That said, there are two basic approaches you can take: a newtype wrapper with a smart constructor, or a totally new type.

    The newtype wrapper is easier to use:

    module Vowel (Vowel, vowel, fromVowel) where
    
    newtype Vowel = Vowel Char
    
    vowel :: Char -> Maybe (Vowel)
    vowel x | x `elem` "aeiouAEIOU" = Just (Vowel x)
            | otherwise = Nothing
    
    fromVowel :: Vowel -> Char
    fromVowel (Vowel x) = x
    

    Since the Vowel constructor isn’t exported, new Vowels can only be created by the vowel function, which only admits the characters you want.

    You could also make a new type like this:

    data Vowel = A | E | I | O | U | Aa | Ee | Ii | Oo | Uu
    
    fromChar :: Char -> Maybe Vowel
    fromChar 'a' = Just Aa
    fromChar 'A' = Just A
    -- etc.
    
    toChar :: Vowel -> Char
    toChar Aa = 'a'
    toChar A = 'A'
    

    This second way is pretty heavyweight, and therefore is much more awkward to use.

    So that’s how to do it. I’m not quite certain that you want to though. The usual idiom is to make types that represent your data, and you specifically don’t represent vowels. A common pattern would be something like this:

    newtype CleanString = Cleaned { raw :: String }
    
    -- user input needs to be sanitized
    cleanString :: String -> CleanString
    

    Here the newtype differentiates between unsanitized and sanitized input. If the only way to make a CleanString is by cleanString, then you know statically that every CleanString is properly sanitized (provided that cleanString is correct). In your case, it seems you actually need a type for consonants, not vowels.

    Newtypes in Haskell are very lightweight*, but the programmer does have to write and use code to do the wrapping and unwrapping. In many instances the benefits outweigh the extra work. However, I really can’t think of any application where it’s important to know that your String is vowel-free, so I’d probably just work with a plain String.

    *newtypes only exist at compile-time, so in theory there’s no runtime performance cost to using them. However, their existence can change the produced code (e.g. inhibiting RULEs), so sometimes there is a measurable performance impact.

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

Sidebar

Related Questions

I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I have written following PHP code: $input=menu=1&type=0&; print $input.<hr>.ereg_replace('/&/', ':::', $input); After running above
I have written following code to get JSON result from webservice. function SaveUploadedDataInDB(fileName) {
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
i have written the following code: As you can see there is a for
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
In physics library written in C# I have the following code: (in ContactManager.cs) public
I have written the following code in my Rails app to generate XML. I

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.