parent:
870f95074bbfd5de16a15e7e80b0a62cd4726fbc
Nick Mykins <nickmykins@transfix.io>
2018-01-25T16:02:17-05:00
add a couple of alternate implementations
diff --git a/99_problems.hs b/99_problems.hs index 3d1414edb8af318d943b2780802309baa61c2736..9e432bb122dcfa5141e7c4b9166d6146c581d7fc 100644 --- a/99_problems.hs +++ b/99_problems.hs @@ -50,6 +50,12 @@ -- 6. Write a function to determine whether a list is a palindrome isPalindrome :: Eq a => [a] -> Bool isPalindrome list = myReverse list == list +isPalindrome' :: Eq a => [a] -> Bool +isPalindrome' [] = True +isPalindrome' [x] = True +isPalindrome' list = ((head list) == (last list)) + && (isPalindrome' (init $ tail list)) + -- 7. Flatten an arbitrarily-nested list data NestedList a = Elem a | List [NestedList a] @@ -73,6 +79,11 @@ -- 9. Separate runs of identical elements into sublists pack :: Eq a => [a] -> [[a]] pack [] = [] pack (x:xs) = (x : (takeWhile (== x) xs)) : pack (dropWhile (== x) xs) + +pack' :: Eq a => [a] -> [[a]] +pack' [] = [] +pack' (x:xs) = (x : fst run) : (pack $ snd run) + where run = span (== x) xs -- 10. Encode runs of identical elements into tuples: