-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 2
More file actions
50 lines (38 loc) · 1.31 KB
/
Week 2
File metadata and controls
50 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--Assignment 1
module SquareRoot where
eps :: Double
eps = 0.001
improve :: Double -> Double -> Double
improve x y_old = (y_old+(x/y_old))/2 --implement as defined
goodEnough :: Double -> Double -> Bool
goodEnough y_old y_new
| abs((y_new - y_old)/y_new)<eps = True -- if approximation is below<eps => Good enough
| otherwise = False -- else, not good enough
root :: Double -> Double
root x = aux x 1 --helper function defined below
aux :: Double -> Double -> Double
aux x y
| goodEnough y (improve x y) = improve x y --if approximation is good enough, done
| otherwise = aux x (improve x y) --else recurse
main :: IO ()
main = do
putStrLn "Compute the root of:"
input <- getLine -- <- to assign in do block from getLine
let x = read input -- let to assign in do block, read to convert to double
if x<0
then return() -- abort if negative
else do -- do not forget do block
putStrLn ("Square root: " ++ show(root x)) -- else compute root
main --loop back to main
--Assignment 2
module Coins where
ls = [500, 200, 100, 50, 20, 10, 5]
cntChange :: Int -> Int
cntChange x = aux x ls
aux :: Int -> [Int] -> Int
aux x (d:ds)
| x<0 = 0 -- BC1
| x==0 = 1 --BC2
| ds==[] && x `mod` d == 0 = 1 --BC3
| ds==[] && x `mod` d /= 0 = 0 --BC4
| otherwise = (aux x ds) + (aux (x-d) (d:ds)) --recursion