Use an online Haskell interpreter such as.
- https://play.haskell.org/
- https://www.jdoodle.com/execute-haskell-online/
- https://www.onlinegdb.com/online_haskell_compiler
Solve the following exercises.
- Write A signum function which returns 1 if the number is positive, -1 if the number is negative and 0 if the number is 0.
- Write a function with guards that returns the signum of a number.
- Based on the Factorial example, write a recursive function which calculates the Fibonacci number for a given number n. Fibonacci sequence is a sequence of numbers where the next number is the sum of the previous two numbers. The first two numbers in the sequence are 0 and 1. The first 10 numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
- Implement a recursive sum function which takes a list of numbers and returns the sum of all numbers in the list.
main = print $ sumList [1,2,3] -- returns 6
- Implement a string reverser
main = print $ Main.reverse "Hello" -- returns "olleH"
Remember, that strings are just lists of characters
- Implement a function which counts the number of odd numbers in a list of integers. Use pattern matching and recursion.
Type definition:
countOdds :: [Int] -> Int
Example main routine:
main = print $ countOdds [1,2,3,4,5] -- returns 3
- Implement your own filter function which takes a list and a predicate function and returns a list of all elements that satisfy the predicate. For example, the expression
main = print $ filter even [1,2,3,4,5]
returns [2,4]
This game generates a random number between 1 and 100, and the player has to guess it. After each guess, the game will tell the player if their guess is too high, too low, or correct. The game continues until the player guesses the correct number.
Implement this game in Haskell.
Check that the function getRandomFromTime indeed returns a random number.
import Data.Time.Clock
import Data.Time.Clock.POSIX
-- returns a random number between 1 and 100
getRandomFromTime :: IO Int
getRandomFromTime = do
now <- getCurrentTime
let millis = floor $ utcTimeToPOSIXSeconds now * 1000
return $ (millis `mod` 100) + 1
main :: IO ()
main = do secret <- getRandomFromTime
print secret
Given the main function
main :: IO ()
main = do secret <- getRandomFromTime
putStrLn "Try to guess a secret number 1-100."
input <- getLine
let guess = read input :: Int
isWin <- Main.status guess secret
print isWin
putStrLn "Done"
Implement the status function, which returns a boolean if the guess is correct. It should also print a message if the guess is too high or too low or it you have won The function should have the following signature:
status :: Int -> Int -> IO Bool
Finally implement a loop until the user has guessed the correct number.