Wednesday, December 28, 2011

[IFP] Exercises 1.1

Introduction to Functional Programming by Richard Bird and Philip Wadler.
1.1.1 Using the function square, design a function quad which raises its argument to the fourth power
square :: Num a => a -> a
square x = x * x

quad :: Integer -> Integer
quad = square . square

1.1.2 Define a function max which returns the greater of its two arguments
max' :: Ord a => a -> a -> a
max' a b
  | a > b     = a
  | otherwise = b

1.1.3 Define a function for computing the area of a circle with given radius r (use 22/7 as an approximation to pi)
areaCircle :: Fractional a => a -> a
areaCircle r = pi * r ^ 2
  where pi = 22/7

No comments:

Post a Comment