Haskell/Solutions/List processing
From Wikibooks, the open-content textbooks collection
[edit] Map
| Exercises |
|---|
|
Use
factors p = [ f | f <- [1..p], p `mod` f == 0 ]
|
In one block of code. In addition to the variations here, you can also replace Int by Integer:
negateList, negateList2 :: [Int] -> [Int] negateList = map negate negateList2 list = map negate list factorList, factorList2 :: [Int] -> [[Int]] factorList = map factors factorList2 list = map factors list -- There are a few possible variations for this one. If yours is different, check with a Haskell interpreter to see if it's correct. negateFactorList, negateFactorList2, negateFactorList3, negateFactorList4 :: [Int] -> [[Int]] negateFactorList = map (negateList . factors) negateFactorList2 = map negateList . factorList negateFactorList3 list = map (negateList . factors) list negateFactorList4 list = map (map negate) (map factors list)
| Exercises |
|---|
|
Implement a Run Length Encoding (RLE) encoder and decoder.
|
Here's my Run Length Encoding, although I don't see how I'm supposed to use map. (Maybe if the text explained group?)
encode :: Eq a => [a] -> [(Int,a)]
encode [] = []
encode [x] = [(1,x)]
encode (x:y:xs) | x == y = succHead (encode (y:xs))
| otherwise = (1,x) : encode (y:xs)
succHead :: [(Int,a)] -> [(Int,a)]
succHead [] = [] --this should never be called by encode on any input
succHead ((n,x):ps) = (n+1,x):ps
decode :: [(Int,a)] -> [a]
decode [] = []
decode ((n,x):ps) = (repetition n x) ++ decode ps
repetition :: Int -> a -> [a]
repetition 0 _ = [] --this should never be called by decode on a properly formatted input
repetition n x = x:(repetition (n-1) x)
And here is mine using map. To understand what the group command did, I visited hayoo. If you aren't familiar with hayoo yet, take a couple of minutes to try it now. It's very useful.
import Data.List lenwhat :: [Char] -> (Int, Char) lenwhat x = (length x, head x) encode :: [Char] -> [(Int, Char)] encode "" = [] encode x = map lenwhat (group x) decode :: [(Int, Char)] -> [Char] decode [] = "" decode ((l, w):xs) = (replicate l w) ++ decode xs toRleString :: [(Int, Char)] -> [Char] toRleString [] = "" toRleString ((l, w):xs) = show l ++ w : rlestring xs
N.B., the RLE example is inspired from a blog post by Don Stewart on the same subject. See Don's post for a more elegant solution (which might not be immediately understandable; see Haskell/Understanding arrows for a discussion on &&&)
[edit] Folds
| Exercises |
|---|
|
Define the following functions recursively (like the definitions for
Define the following functions using
|
and [] = True and (x:xs) = x && and xs --As a fold and = foldr (&&) True or [] = False or (x:xs) = x || or xs --As a fold or = foldr (||) False maximum = foldr1 max --foldl1 also works, but not if you want infinite lists. Not that you can ever be sure of the maximum of an infinite list. minimum = foldr1 min --Same as above.
[edit] Scans
| Exercises |
|---|
|
Define the following functions:
|
factList n = scanl1 (*) [1..n] factList' n = scanl (*) 1 [2..n]