It’s my first time to use data types in Haskell.
Got a problem and I don’t know how to improve the code.
Here is the problem:
Declear a data type called “Consonant” which include some letters(string), and an textstring which got the information about if all the letters in that word are consonant or not (string), then write a function “Cheak” which got indata (some letters to cheak) and outdata (the data type “Consonant”).
Here is my code:
module Consonant where
import Char
type Name = String
type ConOrNot = String
data Consonant = Cons Name ConOrNot
deriving (Show,Eq)
isVowel = "AEIOU"
cheak :: String -> Consonant
cheak [] = ""
cheak (char:chars) =
if elem (toUpper char) isVowel == false
then cheak chars
else cheak = Cons (char:chars) "Not Consonant"
-- here I want to use "break", but I don't know how to use it in Haskell...
cheak = Cons (char:chars) "Is Consonant"
It doesn’t work… How to change the code? Pls help! Thank you!
Update:
module Consonant where
import Char
type Word = String
type ConOrNot = String
data Consonant = Cons Word ConOrNot
deriving (Show,Eq)
isConsonant = "BCDFGHJKLMNPQRSTVWXYZ"
cheak :: String -> Consonant
cheak [] = Cons "" ""
cheak (char:chars)
|elem (toUpper char) isCosonant = cheak chars --if all the letters are cosonant, I want it return (Cons (char:chars) "is Consonant").. still working on it
|otherwise = Cons (char:chars) "Not Consonant"
It works now if the string got both vowels and consonant or only vowels, how to improve the code so it also works with only consonants?
I’m not entirely sure what you are asking for. Is the
Wordin the returnedConssupposed to be theWordpassed tocheakor the remainder of theWordstarting from the first vowel?Does this do what you want?