[’a’,’b’,’c’]
(’a’,’b’,’c’)
[(False,’0’),(True,’1’)]
([False,True],[’0’,’1’])
[tail,init,reverse]
second xs = head (tail xs)
swap (x,y) = (y,x)
pair x y = (x,y)
double x = x*2
palindrome xs = reverse xs == xs
twice f x = f (f x)
What functions define the typeclasses Real and Integral,
respectively? Read the documentation (find them either
through Hayoo
or Hoogle) of these functions and try
invoking each of them in ghci. Show your trials and explain in English
what the functions do.
What are the example types that are instances of Real and Integral,
respectively. Try invoking the Real and Integral functions on
values of the example types that you give.
Define a function f as follows:
f x = toRational $ rem x 5
What is f's type?
f's type.
Redefine the following version of (&&) using conditionals rather than patterns:
True && True = True
_ && _ = FalseGive three possible definitions for the logical or operator (||)
using pattern matching.
What does the init function in Prelude1 do? Define init in
terms of functions length and take.
The scalar product of two lists of integers xs and ys of length n is
give by the sum of the products of the corresponding integers:
Using a list comprehension, define a function that returns the scalar product of two lists. (Hint: You could start from the following program stub.)
scalarProduct :: [Int] -> [Int] -> Int
scalarProduct xs ys = undefinedA positive integer is perfect if it equals the sum of all of its factors, excluding the number itself. Using a list comprehension, define a function
perfects :: Int -> [Int]
perfects n = undefined
that returns the list of all perfect numbers up to a given limit, such that, e.g.:
Prelude λ> perfects 500
[6,28,496]1.Prelude is a standard module loaded by default when you start ghci. Information about Prelude can be found on the Prelude Hackage page.
↩