Jump to content

Concurrent Clean/AnswersCH1

From Wikibooks, open books for an open world

2. Write a function that takes two arguments, say n and x, and computes their power, xn. Use this to construct a function that squares its argument. Write a program that computes the square of 128.

Answers:

module exercicio02
import StdEnv

quadrado n x = n ^ x

Start = quadrado 2 7

3. Define the function isum :: Int -> Int which adds the digits of its argument. So, isum 1234 = 10 isum 0 = 0 isum 1001 = 2 You may assume that isum is applied to an argument which is not negative.

Answers:

module exercicio04
import StdEnv

isum :: Int -> int
isum 1234 = 10
isum 0 = 0
isum 1001 = 2

Start = isum 0

4. Use the function isum to check whether a number can be divided by 9.

5. Define a function Max with two arguments that delivers the maximum of the two.

6. Define a function Min that has two arguments that delivers the minimum of the two.

7. Define a function MaxOfList that calculates the largest element of a list.

8. Define a function MinOfList that calculates the smallest element of a list.

9. Define a function Last that returns the last element of a list.

10. Define a function LastTwo that returns the last two elements of a list.

11. Define a function Reverse that reverses the elements in a list.

12. Define a function Palindrome which checks whether a list of characters is a palindrome, i.e. when you reverse the characters you should get the same list as the original.