Thursday, June 11, 2009

APIIT haskell Assignment in 16 lines of code


1 >type Table = ( String, [String], [[String]])
2 >top10 = ("LargestCountries",[ "Rank","Country", "Area"],[ [ "1", "Russia", "17075400"] , [ "2", "Canada", "9976140"] , [ "3", "United States", "9629091"] , [ "4", "China", "9596960"] , [ "5", "Brazil", "8511965"] , [ "6", "Australia", "7686850"] , [ "7", "India", "3287590"] , [ "8", "Argentina", "2776890"] , [ "9", "Kazakhstan", "2717306"] , [ "10", "Sudan", "2505810"]] )
3 formatSingleRow [][] = "|"
4 formatSingleRow (x:xs) (y:ys) = "| " ++ addSpace y x ++ formatSingleRow xs ys where addSpace a b | length b > a = b | otherwise = addSpace (a-1) b ++ " "
5 formatValues [] _ = "\n"
6 >formatValues ((x:xs)) (y:ys) = formatSingleRow x (y:ys) ++ "\n" ++ formatValues xs (y:ys)
7 >writeTable (x,y,z) = x ++ "\n\n" ++ formatHeader y ([ a+10|(a)<-(map length y)]) ++ formatValues z ([ a+10|(a)<-(map length y)]) where formatHeader x y = formatSingleRow x y ++ "\n" ++ addLine ((length y)*18) "-" ++ "\n" where addLine a b | length b == a = b |otherwise = b ++ addLine (a - 1) b
--the prgram logic
8 >printTable = putStr.writeTable
9 >filterByIndex [] _ = []
10 >filterByIndex (x:xs) y = [[value]|(value,ind)<-(zip x [0..]),ind==y] ++ filterByIndex xs y
11 >addListofLists x [] = x
12 >addListofLists (x:xs) (y:ys) = [[ a |a<-x] ++ [ b |b<-y ]] ++ addListofLists xs ys
13 >filterListofLists [] _ = []
14 >filterListofLists (x:xs) (a,b,c) = addListofLists (filterByIndex c (idx x b)) (filterListofLists xs (a,b,c)) where idx x y = [ind|(value,ind)<-(zip y [0..]),value==x]!!0 --gets index of list
15 >project x (a,b,c) = (a, x,(filterListofLists x (a,b,c)))
16 >select feild condition (a,b,c) = (a,b,((filter (\x -> condition (x!!(idx feild b))))c)) where idx x y = [ind|(value,ind)<-(zip y [0..]),value==x]!!0 -- gets index of list

No comments:

Post a Comment