-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwh3.hs
44 lines (35 loc) · 1.12 KB
/
rwh3.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Data.List
import Data.Ord
import Data.Function
data Tree a = Node a (Tree a) (Tree a)
| Empty
deriving (Show)
-- Real World Haskell Chapter 3
-- Write a function that computes the number of elements in a list.
-- To test it, ensure that it gives the same answers as the standard length function.
length' :: [a] -> Int
length' [] = 0
length' (x:xs) = 1 + (length' xs)
mean' :: Fractional a => [a] -> a
mean' [] = 0
mean' xs = sum' xs / fromIntegral (length' xs)
where sum' [] = 0
sum' (y:ys) = y + (sum' ys)
palindrome :: [a] -> [a]
palindrome [] = []
palindrome (x:xs) = x : palindrome xs ++ [x]
isPalindrome :: Eq a => [a] -> Bool
isPalindrome [] = True
isPalindrome x = x == reverse x
sortByLength :: [[a]] -> [[a]]
sortByLength x = sortBy (compare `on` length) x
intersperse' :: a -> [[a]] -> [a]
intersperse' x (y:[]) = y
intersperse' x (y:ys) = y ++ [x] ++ (intersperse' x ys)
treeDepth :: Tree a -> Int
treeDepth Empty = 0
treeDepth (Node _ b c) = 1 + treeDepth b + treeDepth c
data Direction = Left
| Right
| Straight
deriving (Show)