Skip to content

Instantly share code, notes, and snippets.

@takeisa
Created February 19, 2013 14:47
Show Gist options
  • Save takeisa/4986529 to your computer and use it in GitHub Desktop.
Save takeisa/4986529 to your computer and use it in GitHub Desktop.
import Data.List(sortBy)
myLength :: [a] -> Int
myLength [] = 0
-- myLength (x:xs) = 1 + myLength xs
myLength (_:xs) = 1 + myLength xs
-- sumList :: [Double] -> Double
-- sumList [] = 0
-- sumList (x:xs) = x + sumList xs
-- average :: [Double] -> Double
-- average xs = sumList xs / fromIntegral (length xs)
mean :: [Double] -> Double
mean xs = (total xs) / fromIntegral (length xs)
where total :: [Double] -> Double
total [] = 0
total (x:xs) = x + total xs
-- palindrome
pali :: [a] -> [a]
pali [] = []
pali (x:xs) = [x] ++ pali xs ++ [x]
isKaibun :: Eq a => [a] -> Bool
isKaibun [] = True
isKaibun [x] = True
-- isKaibun xs = (head xs == last xs) && isKaibun (init (tail xs))
isKaibun (x:xs) = x == last xs && isKaibun (init xs)
sortByListLength :: [[a]] -> [[a]]
sortByListLength xs = sortBy compareListLength xs
where compareListLength xs1 xs2 = compare (length xs1) (length xs2)
-- intersperse
myIntersperse :: a -> [[a]] -> [a]
myIntersperse s [] = []
myIntersperse s [x] = x
myIntersperse s (x:xs) = x ++ (s:(myIntersperse s xs))
-- Tree
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
-- treeHeight
treeHeight :: Tree a -> Int
treeHeight Empty = 0
treeHeight (Node _ left right) = 1 + max (treeHeight left) (treeHeight right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment