Reputation: 141
I'm having some troubles with lists.
I have the following lists of lists:
sentence = [['t','h','e'], ['b','o','y'],['i','s'],['h','a','p','p','y']]
shortVowel = [['a'], ['e'], ['y']]
I need to make an if statement that checks 2 things:
(1) if an element x is a member of the last "sublist" (whose, of course, is the last element of the main list),
and (2) if an element (a sublist) of the list shortVowel
is the last member of the last sublist.
The (1) I was able to do with elem 'а' (last sentence))
(checking if 'a'is a member of ['h','a','p','p','y']
, which it is.
The (2) is the one I don't know how to get.
For exemple, the last element of the last sublist in sentence
is y
(from happY). And y
is also a sublist in the shortVowel
list.
I need to check exactly this.
I try some things, like elem shortVowel (last sentence)
, but it didn't work.
This is my current code:
import Data.List
sentence = [['t','h','e'], ['b','o','y'],['i','s'],['h','a','p','p','y']]
shortVowel = [['a'], ['e'], ['y']]
main = if ((elem 'а' (last sentence)) || (elem 'о' (last sentence)) || (elem 'у' (last sentence)) && (elem shortVowel (last sentence)))
then putStrLn "A"
else if (elem 'р' (last sentence))
then putStrLn "B"
else putStrLn "C"
Upvotes: 2
Views: 100
Reputation: 16125
Here are some things you could do:
Deal with handling errors. Both the list of words and the last word itself could be empty.
last
is a partial function and can crash at run-time.
Move your logic into a pure function separate from main :: IO
.
Make the example string a parameter of the function.
And deal with extracting the last word from the sentence separately.
Here's an example of that:
module Main where
-- | `lastMay [x,y,z] == Just z` and `lastMay [] == Nothing`.
lastMay :: [a] -> Maybe a
lastMay [] = Nothing
lastMay [x] = Just x
lastMay (_:xs) = lastMay xs
-- | `endsWithShortVowel "yay" == True` and `endsWithShortVowel "moo" == False`.
wordEndsWithShortVowel :: String -> Bool
wordEndsWithShortVowel s = case lastMay s of
Nothing -> False -- No letters in word!
Just c -> c `elem` "aey"
sentenceEndsWithShortVowel :: [String] -> Bool
sentenceEndsWithShortVowel s = case lastMay s of
Nothing -> False -- No words in sentence!
Just w -> wordEndsWithShortVowel w
main :: IO ()
main =
if sentenceEndsWithShortVowel exampleSentence
then ...
else ...
exampleSentence :: [String]
exampleSentence = words "the boy is happy"
Then, for example, making the program receptible to strings from standard input becomes easier:
main :: IO ()
main = do
sentence <- words <$> getLine
if sentenceEndsWithShortVowel sentence
then ...
else ...
Note: Be careful of indentation levels when you combine do
and if-then-else
.
Upvotes: 2
Reputation: 3504
Instead of writing shortVowel = [['a'], ['e'], ['y']]
I would write shortVowel' = ['a', 'e', 'y']
. Which is the same as shortVowel' = "aey"
. Now do
or [x == last $ last sentence | x <- shortVowel']
The above checks if any of the elements in shortVowel
is the last element of the last word in your sentence. If you want to check it for all short vowels then do
[(x, x == last $ last sentence) | x <- shortVowel']
Upvotes: 2