开发者

Haskell - declare/use data

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 [] =开发者_如何学Python ""
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?


This is homework, isn't it?

Issues with your code include:

  1. This line makes no sense:

    cheak [] = ""
    

    because cheak is supposed to return a Consonant, but here you return a String.

  2. This line also makes no sense:

    cheak = Cons (seq:seqs) "Is Consonant"
    

    beacause cheak is supposed to take a String parameter and return a Consonant, but here you just return a Consonant without taking any parameter.

  3. This line makes even less sense:

    else    cheak = Cons (seq:seqs) "Not Consonant"
    

    Haskell is not Pascal or Visual Basic. You return a value from a function by letting the RHS of the equation evaluate to that value.

  4. Indentation matters.

  5. There is a function called break in Haskell, but it is unrelated to the break keyword you may be familiar with from Java or C. The Java/C concept of breaking out of a loop doesn't exist in Haskell.

  6. You probably want to use a helper function.

  7. "Cheak" isn't a word.

  8. if-then-else is not a statement. The then-branch and the else-branch are not statements either. They're all expressions. It's like ?: from Java, C and some other languages.


I'm not entirely sure what you are asking for. Is the Word in the returned Cons supposed to be the Word passed to cheak or the remainder of the Word starting from the first vowel?

Does this do what you want?

module Consonant
    where

import Data.Char

type Word = String
type ConOrNot = String
data Consonant = Cons Word ConOrNot
                 deriving (Show,Eq)

cheak :: Word -> Consonant
cheak "" = Cons "" ""
cheak s = Cons s $ if any isVowel s then "Not Consonant" else "is Consonant"
          where isVowel c = (toUpper c) `elem` "AEIOU"


There are many ways to do what you want to do in Haskell. The obvious first choices are Maybe and Either. But in general, checkout the 8 ways to report errors in Haskell .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜