Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload homework #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions exercises/secret-lang/Secret-lang.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
import Data.Char(toLower)

vowels = "aeiou"

isChar :: Char -> Bool
isChar = undefined
isChar char
| toLower char >= 'a' && toLower char <= 'z' = True
isChar _ = False

isInString :: Char -> String -> Bool
isInString char [] = False
isInString char (first:rest)
| char == first = True
| char /= first = isInString char rest


isVowel :: Char -> Bool
isVowel = undefined
isVowel char
| isInString (toLower char) vowels == True = True
Copy link
Contributor

@gcnew gcnew Nov 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Няма нужда от guard, тъй като резултатният тип е Bool, т.е.

isVowel char = isInString (toLower char) vowels

е еквивалентно :)

isVowel _ = False


isConsonant :: Char -> Bool
isConsonant = undefined
isConsonant char
| isVowel char == False && isChar char == True = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Няма нужда да сравняваш върнатия булев резултат с True, тъй като:
True == True е същото като True, само че с повече тавтология. Ако искаме да обърнем резултата, обикновено ползваме не (not).

| otherwise = False


encode :: String -> String
encode = undefined
encode [] = []
encode (first:rest)
| isConsonant first == True = first : 'o' : first : (encode rest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant == True

| otherwise = first:(encode rest)


-- Bonus #1
encode' :: String -> String
encode' = undefined
encode' [] = []
encode' (first:rest)
| isConsonant first == True = first : 'o' : toLower first : (encode' rest)
| otherwise = first:(encode' rest)


-- Bonus #2
dropN :: Int -> String -> String
dropN = undefined
dropN number [] = []
dropN 0 string = string
dropN number (first:rest) = dropN (number-1) rest



--Assume we'll be decoding only valid words
decode :: String -> String
decode = undefined
decode [] = []
decode (x : 'o' : y : rest)
| x == y = x : decode (dropN 2 ('o':y:rest))
| otherwise = x : 'o' : y : (decode (rest))
decode (first : rest) = first : decode (rest)