mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
did longest, cheated a little bit and imported extra packages
This commit is contained in:
+14
-5
@@ -21,6 +21,7 @@ import Data.List
|
|||||||
import Data.Ord
|
import Data.Ord
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Array
|
import Data.Array
|
||||||
|
import Data.Foldable as Foldable
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
-- Ex 1: implement the function allEqual which returns True if all
|
-- Ex 1: implement the function allEqual which returns True if all
|
||||||
@@ -104,12 +105,20 @@ rangeOf xs = last sorted_xs - head sorted_xs
|
|||||||
-- longest [[1,2,3],[4,5],[6]] ==> [1,2,3]
|
-- longest [[1,2,3],[4,5],[6]] ==> [1,2,3]
|
||||||
-- longest ["bcd","def","ab"] ==> "bcd"
|
-- longest ["bcd","def","ab"] ==> "bcd"
|
||||||
|
|
||||||
longest = todo
|
-- longest = todo
|
||||||
-- longest :: (Foldable t) => [t a] -> t a
|
-- longest :: (Foldable t) => [t a] -> t a
|
||||||
-- longest (x:xs) = if length x == longest_len then return x else longest xs
|
longest (x:xs) = foldr compare x xs
|
||||||
-- where
|
where
|
||||||
-- sorted = sortBy (comparing length) xs
|
-- compare :: (Foldable t) => t a -> Int -> Int
|
||||||
-- longest_len = length (last sorted)
|
-- compare x len = if (length x > len) then (length x) else len
|
||||||
|
-- compare :: (Foldable t) => t a -> t a -> t a
|
||||||
|
compare x y = if length x == length y then head_comp x y else len_comp x y
|
||||||
|
|
||||||
|
-- head_comp :: (Foldable t) => t a -> t a -> t a
|
||||||
|
head_comp x y = if (head (Foldable.toList x)) < (head (Foldable.toList y)) then x else y
|
||||||
|
|
||||||
|
-- len_comp :: (Foldable t) => t a -> t a -> t a
|
||||||
|
len_comp x y = if length x > length y then x else y
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
-- Ex 6: Implement the function incrementKey, that takes a list of
|
-- Ex 6: Implement the function incrementKey, that takes a list of
|
||||||
|
|||||||
Reference in New Issue
Block a user