Monday, January 2, 2012

[IFP] Exercises 1.5

Unsure about these two... they seemed simple, which quite often means they weren't....
1.5.1 Using any suitable notation, write a specification of a function isSquare that determines whether or not its argument is an integer which is the square of another integer.
isSquare x == (squareRoot x)^(.5), for all x >= 0
1.5.2 Write down a precise specification of the function insqrt mentioned in the previous question.
intsqrt is the largest possible integer < sqrt(x)

[IFP] Exercises 1.4

I feel like I understand currying and function application, but I'm still really, really, having a difficult time with this type declaration business.

1.4.1 Define one appropriate type for the definite integral function of mathematical analysis, as in used in the phrase 'the integral of f from a to b'.
integral :: (num -> num) -> num -> num -> num

1.4.2 Give examples of functions with these types....

multiply :: num -> (num -> num)
multiply a b = a * b

applyFToThree :: (num -> num) -> num
applyFToThree f = f 3

theSame :: (num -> num) -> (num -> num)
theSame f = g
    where g x = f x 

1.4.3 Give a definition of a function sign :: num -> num which returns 1 if its argument is positive, -1 if its argument is negative, and 0 otherwise.
sign :: num -> num
sign x =  1, if x > 0
       = -1, if x < 0
       =  0, otherwise

1.4.4 Suggest possible types for these functions: one x = 1, apply f x = f x, compose f g x = f (g x)
one :: α -> num
apply :: (num -> num) -> num -> num
compose :: (num -> num) -> (num -> num) -> num -> num