Haskell/Solutions/Lists III

Define the following functions recursively (like the definitions for sum, product and concat above), then turn them into a fold:

and :: [Bool] -> Bool, which returns True if a list of Bools are all True, and False otherwise.

or :: [Bool] -> Bool, which returns True if any of a list of Bools are True, and False otherwise.

Define the following functions using foldl1 or foldr1:

maximum :: Ord a => [a] -> a, which returns the maximum element of a list (hint: max :: Ord a => a -> a -> a returns the maximum of two values).

minimum :: Ord a => [a] -> a, which returns the minimum element of a list (hint: min :: Ord a => a -> a -> a returns the minimum of two values).

Use a fold (which one?) to define reverse :: [a] -> [a], which returns a list with the elements in reverse order.

Note that all of these are Prelude functions, so they will be always close at hand when you need them. (Also, that means you will want to use slightly different names for testing your answers in GHCi.)

and[]=Trueand(x:xs)=x&&andxs--As a foldand=foldr(&&)Trueor[]=Falseor(x:xs)=x||orxs--As a foldor=foldr(||)Falsemaximum::Orda=>[a]->a-- omitting type declaration results in "Ambiguous type variable" errormaximum=foldr1max--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::Orda=>[a]->aminimum=foldr1min--Same as above.--Using foldl'; you will want to import Data.List in order to do the same.reverse::[a]->[a]reverse=foldl'flippedCons[]whereflippedConsxsx=x:xs

Write your own definition of scanr, first using recursion, and then using foldr. Do the same for scanl first using recursion then foldl.

-- with recursionscanr2stepzero[]=[zero]scanr2stepzero(x:xs)=(stepx(headprev)):prevwhereprev=scanr2stepzeroxsscanl2stepzero[]=[zero]scanl2stepzero(x:xs)=zero:scanl2step(stepzerox)xs-- An alternative scanl with poorer performance. The problem is that-- last and init, unlike head and tail, must run through the entire-- list, and (++) does the same with its first argument.scanl2Slowstepzero[]=[zero]scanl2Slowstepzeroxs=prev++(step(lastprev)(lastxs)):[]whereprev=scanl2Slowstepzero(initxs)--with foldscanr3stepzeroxs=foldrstep'[zero]xswherestep'xxs=(stepx(headxs)):xs-- This implementation has poor performance due to (++), as explained-- for scanl2Slow. In general, using (++) to append to a list-- repeatdely is not a good idea. scanl3Slowstepzeroxs=foldlstep'[zero]xswherestep'xsx=xs++(step(lastxs)x):[]

In the efficient scanr implementations above, we use head and tail as a way to avoid having to break down the list argument with pattern matching in the where clause only to assemble it back in the right-hand side. In the Pattern matching chapter, we will see how as-patterns allow us to reach the same effect without needing head.

Exercises

Define the following functions:

factList :: Integer -> [Integer], which returns a list of factorials from 1 up to its argument. For example, facList 4 = [1,2,6,24].

Write a returnDivisible :: Int -> [Int] -> [Int] function which filters a list of integers retaining only the numbers divisible by the integer passed as first argument. For integers x and n, x is divisible by n if (mod x n) == 0 (note that the test for evenness is a specific case of that).

returnDivisible::Int->[Int]->[Int]returnDivisiblenxs=[x|x<-xs,(modxn)==0]--or, if you prefer to use filter...returnDivisible::Int->[Int]->[Int]returnDivisiblen=filter(\x->(modxn)==0)

Exercises

Write, using list comprehension syntax, a function definition with no case analysis (that is, without multiple equations, if, case, or similar constructs) a [[Int]] -> [[Int]] function which, takes a list of lists of Int and returns a list of the tails of those lists using, as filtering condition, that the head of each [Int] must be larger than 5. Also, your function must not trigger an error when it meets an empty [Int], so you'll need to add an additional test to detect emptiness.

Does order matter when listing the conditions for list comprehension? (You can find it out by playing with the function you wrote for the first part of the exercise.)

The artificial constraints of the exercise suggest a solution which uses head and tail instead of pattern matching.

The boolean conditions in a list comprehension are evaluated in order, so if you swapped the conditions like this:

choosingTailsls=[taill|l<-ls,headl>5,not(nulll)]

if one of the l turned out to be an empty list the program would try to apply head on it, which would cause an error. Putting not (null l) first causes the program to, when it meets an empty list, to short-circuit the conditional - that is, ignore the second condition since the first one being false is enough for rejecting the list - thus avoiding doing head [] and the error that follows.

Exercises

Over this section we've seen how list comprehensions are essentially syntactic sugar for filter and map. Now work in the opposite direction and define alternative versions of the filter and map using the list comprehension syntax.

alternativeFilter::(a->Bool)->[a]->[a]alternativeFiltercondxs=[x|x<-xs,condx]alternativeMap::(a->b)->[a]->[b]alternativeMapfxs=[fx|x<-xs]--no need for any condition, as all x will be accepted

Exercises

Rewrite doubleOfFirstForEvenSeconds using filter and map instead of list comprehension.

doubleOfFirstForEvenSeconds'::[(Int,Int)]->[Int]doubleOfFirstForEvenSecondsps=letfpair=2*(fstpair)gpair=isEven(sndpair)inmapf(filtergps)--isEven, naturally, remains the same.isEven::Int->BoolisEvenn=(modn2==0)-- a small teaser - using the (.) function composition operator which will be formally introduced on "More on functions".doubleOfFirstForEvenSeconds'::[(Int,Int)]->[Int]doubleOfFirstForEvenSeconds'ps=map((2*).fst)(filter(isEven.snd)ps)