Searching for an Element search x xs returns True if x is an element of xs search :: Eq t => t -> [t] -> Bool search x [] = False search x (y : ys) = x==y || search x ys Quicksort qSort :: Ord t => [t] -> [t] qSort [] = [] qSort (x:xs) = qSort [ y | y<-xs , y<=x] ++ [x] ++ qSort [ y | y<-xs , y>x] search x xs = or [x==y | y <- xs] qSort :: Ord t => [t] -> [t] qSort [] = [] qSort (x:xs) = qSort leftList ++ [x] ++ qSort rightList where leftList = smaller x xs rightList = larger x xs smaller :: Ord t => t -> [t] -> [t] smaller _ [] = [] smaller v (y:ys) = if y<=v then y:smaller v ys else smaller v ys larger :: Ord t => t -> [t] -> [t] ......... MergeSort • Divide the list into two roughly equal halves. • Sort each half. • Merge the sorted halves together. MergeSort mergeSort :: Ord t => [t] -> [t] mergeSort [] = [] mergeSort [x] = [x] Taking and Dropping • take n xs • drop n xs the first n elements of xs all but the first n elements mergeSort xs | size > 0 = merge (mergeSort front) (mergeSort back) where size = length xs `div` 2 front = take size xs Examples: take 3 [2, 3, 4, 5, 6, 7, 8] [2, 3, 4] drop 3 [2, 3, 4, 5, 6, 7, 8] [5, 6, 7, 8] back = drop size xs Defining Merge merge :: Ord t => [t] -> [t] -> [t] merge (x : xs) (y : ys) | x <= y = x : merge xs (y : ys) |x>y = y : merge (x : xs) ys merge [] ys = ys merge xs [] = xs [38,16,27,39,12,27] [38,16,27] partitions [39,12,27] [38] [16,27] [39] [12,27] [38] [16] [27] [39] [12] [27] [38] [16,27] merges [39] [12,27] [16,27,38] [12,27,39] [12,16,27,27,38,39] elemNum :: Eq t =>t -> [t] -> Int unique :: Eq t => [t] -> [t] elemNum 4 [4,2,1,3,2,3] = 1 e.g. unique [4,2,1,3,2,3] = [4,1] elemNum 3 [4,2,1,3,2,3] = 2 unique [] = [] elemNum x [] = 0 unique (x : xs) = elemNum x (y : ys) = if eleNum x xs == 0 then x:unique xs if y = x then 1+elemNum x ys else elemNum x ys elemNum x xs = length [y | y<- xs, y == x] else unique xs unique xs = [x | x<- xs, length [y | y<- xs, y ==x]==1] Tentabetyg Prime Numbers primtal :: Int -> Int -> [Int] givet två positiva heltal a och b, ger som resultat ger listan med alla primtal från och med a till och med b primtal 1 12 = [1,2,3,5,7,11] primtal 20 30 = [23,29] prim :: Int - > Bool prim n = [i i < −[2..n], n `mod` i == 0] == [n] primtal :: Int - > Int - > [Int] primtal m n = [i i < −[m..n], prim i ] Tentabetyg 3 4 5 Tentapoäng Tentapoäng gjort labbarna ej gjort labbarna 50 ≤ p < 70 50 ≤ p < 75 70 ≤ p < 80 75 ≤ p < 98 80 ≤ p ≤ 100 98 ≤ p ≤ 100 [(" Eva" ,95,´G´), (" Ida" ,40,´G´), (" Per" ,75,´_´)] → [(" Eva" ,5), (" Per" ,4 )] tentabytyg :: [(String , Int , Char )] → [(String , Int )] tentabytyg | p < 50 | p < 75 ((namn , p ,´_´) : ls ) = tentabytyg ls = (namn , 3 ) : tentabytyg ls = (namn , 4 ) : tentabytyg ls | p < 98 | otherwise = (namn , 5 ) : tentabytyg ls tentabytyg ((namn , p ,´G´) : ls ) = tentabytyg ls | p < 50 = (namn , 3 ) : tentabytyg ls | p < 70 = (namn , 4 ) : tentabytyg ls | p < 80 | otherwise = (namn , 5 ) : tentabytyg ls tentabytyg _ _ = error "......" tentabytyg lista = [(namn , betyg p lab )(namn , p , lab ) < − lista , p >= 50 ] bytyg :: Int → Char → Int bytyg p ´_´ bytyg p ´G´ | p < 75 =3 | p < 98 =4 | otherwise = 5 betyg _ _ = error "......" | p < 70 =3 | p < 80 =4 | otherwise = 5