module Week03 where import Char -- need this if you want to -- use Char.ord or Char.chr factorial :: Integer -> Integer factorial n | n==0 = 1 | otherwise = n * (factorial (n - 1)) wordLength :: String -> Int wordLength word | word == "" = 0 | otherwise = 1 + (wordLength (tail word)) -- tested by test 3 isIn :: String -> [String] -> Bool isIn key list | list == [] = False | key == firstElement = True | otherwise = isIn key rest where firstElement = head list rest = tail list -- only works with pos numbers or 0 -- use test case 4 integer2digits :: Integer -> [Integer] integer2digits number | number < 10 = [number] | otherwise = leadingDigits++[lastDigit] where lastDigit = number `mod` 10 leadingDigits = integer2digits (number `div` 10) test :: Int -> Bool test 3 | (isIn "at" ["Strange","er","than","fiction"]) /= False = False | (isIn "at" ["at","er","than","fiction"]) /= True = False | (isIn "at" []) /= False = False | (isIn "at" ["Strange","at","than","fiction"]) /= True = False | (isIn "" ["Strange","at","than","fiction"]) /= False = False | otherwise = True test 4 | (integer2digits 42) /= [4,2] = False | (integer2digits 0) /= [0] = False | otherwise = True {- Imperial to Metric You wish to convert heights in feet and inches to metres. Unfortunately you remember that there are exactly 25.4 millimetres in an inch and 12 inches in a foot. Write a function imperial2m to do this conversion. -} imperial2m :: Float -> Float -> Float imperial2m a b = (a*12+b)*0.0254 {- -- function to convert feet and inches -- to metres imperial2m :: Float -> Float -> Float imperial2m feet inches = metres where metres = totalInches * metersPerInch totalInches = inches + (feet*inchesPerFoot) metersPerInch = mmPerInch / mmPerMetre inchesPerFoot = 12 mmPerMetre = 1000 mmPerInch = 25.4 -}