mirror of
https://github.com/iritmaximus/haskell.git
synced 2026-10-12 19:13:39 +03:00
part1 excercises
This commit is contained in:
+27
-12
@@ -20,14 +20,16 @@ import Mooc.Todo
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 1: define variables one and two. They should have type Int and
|
||||
-- values 1 and 2, respectively.
|
||||
|
||||
one, two :: Int
|
||||
one = 1
|
||||
two = 2
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 2: define the function double of type Integer->Integer. Double
|
||||
-- should take one argument and return it multiplied by two.
|
||||
|
||||
double :: Integer -> Integer
|
||||
double x = todo
|
||||
double x = x * 2
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 3: define the function quadruple that uses the function double
|
||||
@@ -35,7 +37,7 @@ double x = todo
|
||||
-- four.
|
||||
|
||||
quadruple :: Integer -> Integer
|
||||
quadruple x = todo
|
||||
quadruple x = double (double x)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 4: define the function distance. It should take four arguments of
|
||||
@@ -50,8 +52,8 @@ quadruple x = todo
|
||||
-- Examples:
|
||||
-- distance 0 0 1 1 ==> 1.4142135...
|
||||
-- distance 1 1 4 5 ==> 5.0
|
||||
|
||||
distance = todo
|
||||
distance :: Double -> Double -> Double -> Double -> Double
|
||||
distance x1 y1 x2 y2 = sqrt ((x2-x1)^2 + (y2-y1)^2)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 5: define the function eeny that returns "eeny" for even inputs
|
||||
@@ -60,7 +62,9 @@ distance = todo
|
||||
-- Ps. have a look at the built in function "even"
|
||||
|
||||
eeny :: Integer -> String
|
||||
eeny = todo
|
||||
eeny num = if num `mod` 2 == 0
|
||||
then "eeny"
|
||||
else "meeny"
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 6: here's the function checkPassword from the course material.
|
||||
@@ -68,7 +72,7 @@ eeny = todo
|
||||
-- "mellon".
|
||||
|
||||
checkPassword :: String -> String
|
||||
checkPassword password = if password == "swordfish"
|
||||
checkPassword password = if password == "swordfish" || password == "mellon"
|
||||
then "You're in."
|
||||
else "ACCESS DENIED!"
|
||||
|
||||
@@ -82,7 +86,11 @@ checkPassword password = if password == "swordfish"
|
||||
-- in grams, and returns the cost in credits.
|
||||
|
||||
postagePrice :: Int -> Int
|
||||
postagePrice = todo
|
||||
postagePrice grams = if grams <= 500
|
||||
then 250
|
||||
else if grams > 500 && grams <= 5000
|
||||
then 300 + grams
|
||||
else 6000
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 8: define a function isZero that returns True if it is given an
|
||||
@@ -92,7 +100,9 @@ postagePrice = todo
|
||||
--
|
||||
-- Ps. remember, the type of booleans in haskell is Bool
|
||||
|
||||
isZero = todo
|
||||
isZero :: Integer -> Bool
|
||||
isZero 0 = True
|
||||
isZero _ = False
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 9: implement using recursion a function sumTo such that
|
||||
@@ -100,14 +110,16 @@ isZero = todo
|
||||
-- computes the sum 1+2+...+n
|
||||
|
||||
sumTo :: Integer -> Integer
|
||||
sumTo = todo
|
||||
sumTo 1 = 1
|
||||
sumTo n = n + sumTo (n-1)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 10: power n k should compute n to the power k (i.e. n^k)
|
||||
-- Use recursion.
|
||||
|
||||
power :: Integer -> Integer -> Integer
|
||||
power = todo
|
||||
power n 1 = n
|
||||
power n k = n * power n (k-1)
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Ex 11: ilog3 n should be the number of times you can divide given
|
||||
@@ -126,4 +138,7 @@ power = todo
|
||||
-- ilog3 7 ==> 2
|
||||
|
||||
ilog3 :: Integer -> Integer
|
||||
ilog3 = todo
|
||||
ilog3 0 = 1
|
||||
ilog3 1 = 1
|
||||
ilog3 2 = 1
|
||||
ilog3 n = ilog3 (n `div` 3) + 1
|
||||
|
||||
Reference in New Issue
Block a user