Answers

Answer
The to be evaluated expression: 4*(8-5)+3.

First 8-5 is evaluated to 3, then 4*3=12 and finally 12+3=15.


Prelude> pound 10
ERROR: Undefined variable "pount"
Prelude> pund kr
ERROR: Undefined variable "kr"
Prelude>
In both cases GHCi cannot find definitions of the variables.

Prelude> price 9.5
33.25
Prelude> price 10.5

*** Exception: …: Non-exhaustive patterns in function price


Prelude> price 11.5
39.5
Prelude> 


For arguments 9.5 and 11.5 is the result the same. For 10.5 no guard evaluates to True, and ghci gives an (unfortunately quite cryptic) error message (maybe similar to the one shown)

For the argument 10.5 both guards become True. But because ghci stops when it finds the first guard that evaluates to True, it uses the first right-hand side and gives the (wrong) result 36.75
Prelude> price 4+8
22.0
Prelude> 


Explanation: the expression is interpreted as
(price 4)+8
So, first price 4 is calculated which is 14, and then 8 is added, so the result is 22.

 
price :: Double -> Double
price v
   |v <= 10    = 3.5*v
   |v <= 20    = 5+3*v
   |otherwise  = 15+2.5*v
average3 :: Double -> Double -> Double -> Double
average3 x y z = (x+y+z)/3
We call the function biggest instead of max to avoid a name-clash with the function max in the Prelude.
biggest :: Int -> Int -> Int
biggest x y
  |x >= y = x
  |True   = y

Many variants are possible, for example one can use > instead of >= on the first line. (Why?)
circleArea r = pi*r*r
((4*8)-5)+3 will be calculated.
Prelude> 4*8-5+3
30
Prelude>


The concept of a remainder in the case of division by zero is not meaningful. Therefore, we get an error message.
> 5 `mod` 0

*** Exception: divide by zero

Prelude> 107139224 `mod` 11731
1
Prelude> 
So, the answer is no.
days :: Int -> Int
days y
   |y `mod` 4 == 0 = 366
   |otherwise      = 365
We assume that the parameter n is greater than 1.
next :: Int -> Int
next n
  |n `mod` 2 == 0 = n `div` 2
  |otherwise      = 3*n+1
 
We use the function next several times:
Prelude> next 6
3
Prelude> next 3
10
Prelude> next 10
5
Prelude> next 5
16
Prelude> next 16
8
Prelude> next 8
4
Prelude> next 4
2
Prelude> next 2
1
The answer is 9 numbers.
20 is even, so the next number is 10. But we already know than the sequence of 10 contains 7 numbers.  So, for  20  we have  8 numbers.
So,  steps 20 = 8.

3 is odd, so the next number is 10. So just as above we get steps 3 = 8.


It seems hard to come up with an argument showing that one always reaches the number 1. The sequence seems to behave quite erratic. For instance, for 27 we get the following sequence.

27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1.

Actually, it is a famous open (meaning unsolved) problem to prove that for every n, eventually the number 1 is reached. This problem is known as: Collatz' problem. Therefore, it could be the case that for some n the sequence can go on for ever and ever without ever reaching 1.

So, we cannot be sure that the evaluation of steps n terminates for every n.

When defining recursive functions it is important to make sure that they do terminate.
In this course we show how one can make sure only to define terminating recursive functions.




Den som redan börjar vara förvirrad kan hoppa över den här frågan och dess ofullständiga svar. Om man prövar med ett (ganska) stort n så blir det problem:
Prelude> steps 1000000000000

Program error: {primIntegerToInt 1000000000000}


Prelude> 
Vi kan inte förklara allt nu, men det finns flera typer av heltal också i Haskell. En typ heter Int; för dessa heltal går beräkningar fort, men i gengäld kan de inte bli större än 2147483647. En annan typ heter Integer; för dessa tar beräkningar längre tid men här finns inga storleksbegränsningar. Vill vi ha de senare får vi säga till:
Prelude> steps(1000000000000 :: Integer)
147
Prelude> steps(100000000000000000000000000000000000000000000000000000000 :: Integer)
1198
Prelude> 
The expression 3.5/(3+4)*4.5 will be evaluated as follows:

First 3+4 is evaluated, which is 7, then 3.5/7=0.5 and finally 0.5*4.5=2.25.

Prelude> sqrt 1234567
1111.11
The answer is yes!
Last modified: Tue Aug 13 16:12:43 CEST 2019
Note that the welcome message contains:
:? for help
Try this!
No.
Let ghci calculate:
Prelude>  1000/12.7775
78.2626
Prelude>  
Then we should interpret the result. Perhaps rounded to the nearest penny? At Forex we'd get maybe 75 pounds in notes (there are no one pound notes any more) plus some Swedish change. We'll leave this version as an exercise. Think about what type your function would have and what arithmetic operations you would need.
Prelude>  1000 - 78*12.7775
3.35498
Prelude>  
Man får 3.35 kr tillbaka.
Prelude>  32+28/5*9
82.4
Prelude>   
The answer is 82.4° F.

Frågan är alltså för vilket c man har att fahrenheit c = 100. Ett sätt att ta reda på detta är att pröva:
Prelude> fahrenheit(37)
98.6
Prelude> fahrenheit(38)
100.4
Prelude> fahrenheit(37.5)
99.5
Prelude> fahrenheit(37.8)
100.04
Prelude> fahrenheit(37.78)
100.004
Prelude> fahrenheit(37.77)
99.986
Prelude> fahrenheit(37.775)
99.995
Prelude> fahrenheit(37.777777)
100.0
Ett bättre sätt är att använda definitionen av fahrenheit. Man får då
32+c/5*9=100, dvs
c/5*9 = 100-32 = 68. dvs
c = 68/9*5 = 340/9 = 37 7/9.