Created
March 27, 2014 14:53
-
-
Save tomwhoiscontrary/9809308 to your computer and use it in GitHub Desktop.
London Code Dojo - 7 languages in 7 weeks: Haskell day 2 - code by Tom A and a bunch of other people
This file contains 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
module Dollars where | |
import Test.HUnit | |
----------------------------------------------------- | |
-- parsing dollars | |
----------------------------------------------------- | |
parseDigit '0' = 0 | |
parseDigit '1' = 1 | |
parseDigit '2' = 2 | |
parseDigit '3' = 3 | |
parseDigit '4' = 4 | |
parseDigit '5' = 5 | |
parseDigit '6' = 6 | |
parseDigit '7' = 7 | |
parseDigit '8' = 8 | |
parseDigit '9' = 9 | |
-- we were in the process of rewriting this as a fold when the time ran out | |
accumulate a [] = a | |
accumulate a (head : rest) = accumulate ((a * 10) + head) rest | |
parseDigits digits = accumulate 0 $ map parseDigit digits | |
parseDollars ('$' : digits) = parseDigits digits | |
----------------------------------------------------- | |
-- tests | |
----------------------------------------------------- | |
tests = TestList [ | |
TestLabel "broke" $ TestCase (assertEqual "WTF" 0.0 $ parseDollars "$0"), | |
TestLabel "one dollar" $ TestCase (assertEqual "WTF" 1.0 $ parseDollars "$1"), | |
TestLabel "two dollar" $ TestCase (assertEqual "WTF" 2.0 $ parseDollars "$2"), | |
TestLabel "all the dollars" $ TestCase (assertEqual "WTF" 1234567890.0 $ parseDollars "$1234567890"), | |
TestLabel "this test goes at the bottom so i don't have to keep typing commas" $ TestCase (assertEqual "WTF" 0 0)] | |
main = do runTestTT tests |
This file contains 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
module Sorting where | |
import Test.HUnit | |
----------------------------------------------------- | |
-- basic sorting | |
----------------------------------------------------- | |
insertInRightPlace b [] = [b] | |
insertInRightPlace b (head : rest) | b <= head = b : head : rest | |
insertInRightPlace b (head : rest) | b > head = head : insertInRightPlace b rest | |
sort [] = [] | |
sort [a] = [a] | |
sort (head : rest) = insertInRightPlace head $ sort rest | |
----------------------------------------------------- | |
-- sorting with a comparator | |
----------------------------------------------------- | |
sortBy :: (String -> String -> Ordering) -> [String] -> [String] | |
sortBy comparator list = list | |
-- we didn't finish this! | |
----------------------------------------------------- | |
-- tests and test fixtures | |
----------------------------------------------------- | |
emptyList :: [Int] | |
emptyList = [] | |
compareLength :: String -> String -> Ordering | |
compareLength a b = compare (length a) (length b) | |
tests = TestList [ | |
TestLabel "sorting an empty list returns an empty list" $ TestCase (assertEqual "WTF" emptyList $ sort emptyList), | |
TestLabel "sorting an singleton list returns a singleton list" $ TestCase (assertEqual "WTF" [1] $ sort [1]), | |
TestLabel "sorting an unsorted list return a sorted list" $ TestCase (assertEqual "WTF" [1, 2] $ sort [2, 1]), | |
TestLabel "sorting an unsorted list return a sorted list" $ TestCase (assertEqual "WTF" [1, 2, 3] $ sort [2, 3, 1]), | |
-- tests for the compareLength fixture | |
TestLabel "imp is less than giant" $ TestCase (assertEqual "WTF" LT $ compareLength "imp" "giant"), | |
TestLabel "giant is greater than imp" $ TestCase (assertEqual "WTF" GT $ compareLength "giant" "imp"), | |
TestLabel "time is equal to cash" $ TestCase (assertEqual "WTF" EQ $ compareLength "time" "cash"), | |
TestLabel "sorting a list by length returns the list in length order" $ TestCase (assertEqual "WTF" ["imp", "cash", "time", "giant"] $ sortBy compareLength ["cash", "giant", "imp", "time"]), | |
TestLabel "this test goes at the bottom so i don't have to keep typing commas" $ TestCase (assertEqual "WTF" 0 0)] | |
main = do runTestTT tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment