mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
implemented exercises upto capitalize
This commit is contained in:
+18
-8
@@ -28,8 +28,10 @@ import Data.List
|
||||
-- maxBy head [1,2,3] [4,5] ==> [4,5]
|
||||
|
||||
maxBy :: (a -> Int) -> a -> a -> a
|
||||
maxBy measure a b = todo
|
||||
|
||||
maxBy measure a b = if a_val > b_val then a else b
|
||||
where
|
||||
a_val = measure a
|
||||
b_val = measure b
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 2: implement the function mapMaybe that takes a function and a
|
||||
-- Maybe value. If the value is Nothing, it returns Nothing. If it is
|
||||
@@ -40,7 +42,8 @@ maxBy measure a b = todo
|
||||
-- mapMaybe length (Just "abc") ==> Just 3
|
||||
|
||||
mapMaybe :: (a -> b) -> Maybe a -> Maybe b
|
||||
mapMaybe f x = todo
|
||||
mapMaybe f Nothing = Nothing
|
||||
mapMaybe f (Just x) = Just (f x)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 3: implement the function mapMaybe2 that works like mapMaybe
|
||||
@@ -54,7 +57,8 @@ mapMaybe f x = todo
|
||||
-- mapMaybe2 div (Just 6) Nothing ==> Nothing
|
||||
|
||||
mapMaybe2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
|
||||
mapMaybe2 f x y = todo
|
||||
mapMaybe2 f (Just x) (Just y) = Just (f x y)
|
||||
mapMaybe2 _ _ _ = Nothing
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 4: define the functions firstHalf and palindrome so that
|
||||
@@ -76,9 +80,13 @@ mapMaybe2 f x y = todo
|
||||
palindromeHalfs :: [String] -> [String]
|
||||
palindromeHalfs xs = map firstHalf (filter palindrome xs)
|
||||
|
||||
firstHalf = todo
|
||||
|
||||
palindrome = todo
|
||||
firstHalf :: String -> String
|
||||
firstHalf x = take (halfLength x) x
|
||||
where
|
||||
halfLength x = (length x) `div` 2 + ((length x) `mod` 2)
|
||||
|
||||
palindrome :: String -> Bool
|
||||
palindrome x = (reverse x) == x
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 5: Implement a function capitalize that takes in a string and
|
||||
@@ -96,7 +104,9 @@ palindrome = todo
|
||||
-- capitalize "goodbye cruel world" ==> "Goodbye Cruel World"
|
||||
|
||||
capitalize :: String -> String
|
||||
capitalize = todo
|
||||
capitalize xs = unwords (map capitalizeWord (words xs))
|
||||
where
|
||||
capitalizeWord (x:xs) = toUpper x : xs
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 6: powers k max should return all the powers of k that are less
|
||||
|
||||
Reference in New Issue
Block a user