Created
March 4, 2013 02:21
-
-
Save lewisou/5079467 to your computer and use it in GitHub Desktop.
Haskel Function Pattern Match Syntax Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List -- sortBy | |
myLength :: [a] -> Int | |
myLength [] = 0 | |
myLength (x:xs) = 1 + (myLength xs) | |
meanOfList [] = 0 | |
meanOfList xs = (fromIntegral (listSum xs)) / (fromIntegral (myLength xs)) | |
where listSum [] = 0 | |
listSum (e:es) = e + (listSum es) | |
palindrome [] = [] | |
palindrome xs = xs ++ (toReverse xs) | |
where toReverse (e:es) = (toReverse es) ++ [e] | |
toReverse [] = [] | |
isPalindrome [] = True | |
isPalindrome [e] = False | |
isPalindrome (e1:[e2]) = (e1 == e2) | |
isPalindrome (e1:e2) = (getLast e2) == e1 && (isPalindrome (getPreList e2)) | |
where getLast [e] = e | |
getLast (x:xs) = getLast xs | |
getPreList [e] = [] | |
getPreList (e:es) = e:(getPreList es) | |
lend3 amount balance | |
| amount <= 0 = Nothing | |
| amount > reserve * 0.5 = Nothing | |
| otherwise = Just newBalance | |
where reserve = 100 | |
newBalance = balance - amount | |
sortBySubListLength xs = sortBy ording xs | |
where ording l1 l2 | |
| (myLength l1) > (myLength l2) = GT | |
| (myLength l1) == (myLength l2) = EQ | |
| otherwise = LT | |
intersperse1 sep [] = "" | |
intersperse1 sep ([x]) = x | |
intersperse1 sep (x:xs) = x ++ sep ++ (intersperse1 sep xs) | |
data Tree a = Node a (Tree a) (Tree a) | |
| Leaf | |
deriving (Show) | |
treeHight Leaf = 0 | |
treeHight (Node _ l r) = 1 + (maxNum (treeHight l) (treeHight r)) | |
where maxNum n1 n2 | |
| n1 > n2 = n1 | |
| otherwise = n2 | |
data Direction = Lef | Righ | Straight deriving (Show) | |
calculateDir (x1, y1) (x2, y2) (x3, y3) = | |
if angle1 > angle2 | |
then Righ | |
else if angle1 == angle2 | |
then Straight | |
else Lef | |
where angle1 = (y2 - y1) / (x2 - x1) | |
angle2 = (y3 - y2) / (x3 - x2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment