-- Examples of using list comprehension import Data.Char -- sumSquares n gives the sum of the squares of the numbers from 1 to n sumSquares :: Int -> Int sumSquares n = sum [x*x | x <- [1..n]] -- Change each character to upper case allCAPS :: String -> String allCAPS s = [toUpper c | c <- s] -- takeWords n s returns the first n words of s takeWords :: Int -> String -> String takeWords n s = unwords (take n (words s)) -- translate a sentence to Norwegian toNorwegian :: String -> String toNorwegian s = unwords [trans w | w <- words s] where trans "inte" = "ikke" trans "jag" = "jeg" trans w = w -- keepPositives as delete all negative numbers from as keepPositives :: [Int] -> [Int] keepPositives as = [a | a <- as, a >= 0] -- divisors n lists the (positive) divisors of n divisors :: Int -> [Int] divisors n = [m | m <- [1..n], mod n m == 0] -- isPrime n checks is n is a prime number isPrime :: Int -> Bool isPrime n = divisors n == [1,n] -- dices n returns the ways that a pair of dices can result in the sum n dices :: Int -> [(Int,Int)] dices n = [ (a,b) | a <- [1..6], b <- [1..6], a+b==n] -- check_max n checks that the max function is correct for all arguments from 0 -- to n check_max :: Int -> Bool check_max n = and [ if a>b then max a b == a else max a b == b | a <- [0..n], b <- [0..n] ] -- closestPoints ps gives the distance between the closest (non-equal) points in -- the list ps closestPoints :: [(Double,Double)] -> Double closestPoints ps = minimum [ sqrt ((x1-x2)^2 + (y1-y2)^2) | p1@(x1,y1) <- ps , p2@(x2,y2) <- ps , p1 /= p2 ] -- @ is used to give a name to the expression matched by a pattern -- censor s deletes all lines in s which contain the word "money" censor :: String -> String censor s = unlines [ l | l <- lines s, "money" `notElem` words l] main1 = interact allCAPS main2 = interact toNorwegian main = main1 -- This allows us to run the file as a script in the terminal. If you run e.g. -- this (in the terminal; not in GHCi), -- -- > runhaskell ListComp.hs -- -- it will wait for input from the keyboard and print the result from allCAPS on -- the terminal. -- -- You can also do this (on a Unix system): -- -- > cat File1.txt | runhaskell ListComp.hs > File2.txt -- -- This will read the content of File1.txt, apply the allCAPS transformation, -- and write the result to File2.txt.