First 8-5 is evaluated to 3, then 4*3=12 and finally 12+3=15.
circleArea :: Double -> Double circleArea r = 3.1416*r*r
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>
Prelude> price 4+8 22.0 Prelude>
(price 4)+8So, 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
biggest :: Int -> Int -> Int biggest x y |x >= y = x |True = y
circleArea r = pi*r*r
Prelude> 4*8-5+3 30 Prelude>
> 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
next :: Int -> Int next n |n `mod` 2 == 0 = n `div` 2 |otherwise = 3*n+1
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 1The answer is 9 numbers.
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.
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>
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.11The answer is yes! Last modified: Tue Aug 13 16:12:43 CEST 2019
:? for helpTry this!
Prelude> sin 1.57 - 1 -2.98023e-07 Prelude>
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.
farenheit :: Double -> Double fahrenheit c = 32+c/5*9
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.0Ett bättre sätt är att använda definitionen av fahrenheit. Man får då