This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- [1] Implement sign which returns -1, 0 or +1 depending on the sign of the argument | |
-- use only ifThenElse and < > | |
sign :: Int -> Int | |
sign x = ??? | |
--expected behaviour | |
sign 5 == 1 | |
sign 0 == 0 | |
sign (-3) == -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animal :: IO () | |
animal = do | |
putStrLn "Animal guessing game..." | |
newGame dbInit | |
--------------------------------------------------------------------------- | |
newGame :: [(String, Int)] -> IO () | |
newGame db = do | |
putStrLn "\nThink of an animal and I'll guess it." | |
play db 0 | |
--------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Random | |
import Data.List | |
--------------------------------------------------------------------- | |
-- run this function to play | |
mastermind :: IO () | |
mastermind = do | |
putStrLn "" | |
p1 <- randPeg; p2 <- randPeg; p3 <- randPeg; p4 <- randPeg | |
let code = p1 ++ p2 ++ p3 ++ p4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------------- | |
-- [1] | |
-- Implement 'vecLen', which finds the length of an n-dimensional vector | |
-- length (a1, a2, ..., an) = sqrt (a1^2 + a2^2 + ... + an^2) | |
-- use: zipWith, sqrt, sum, * | |
vecLen :: [Double] -> Double | |
vecLen v = ??? | |
-- expected behaviour: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import System.Random | |
import Data.List | |
--------------------------------------------------------------------- | |
mastermind :: IO () | |
mastermind = do | |
putStrLn "\nThink of a 4 digit code using 1,2,3,4,5,6" | |
turn [0..6^4 - 1] 5 | |
--------------------------------------------------------------------- | |
type Code = Int | |
type Score = Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
balanceTree :: Tree -> Tree | |
balanceTree t = go $ (sort . tree2list) t | |
where go [] = Leaf | |
go ns = let m = length ns `div` 2 | |
in Node (ns !! m) (go $ take m ns) (go $ drop (m + 1) ns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Entry = Qu String | Ans String deriving Eq | |
data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Eq | |
data YesNo = Yes | No | |
insert :: Eq a => Tree a -> Tree a -> Tree a -> Tree a | |
insert o n e | o == e = n | |
insert _ _ Leaf = Leaf | |
insert o n (Node t1 m t2) = Node (f t1) m (f t2) | |
where f = insert o n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kmeans where | |
import System.Random | |
type Point = (Int, Int) | |
type Mean = (Float, Float) | |
type Cluster = [Point] | |
i2f :: (Integral a, Num b) => a -> b | |
i2f = fromIntegral |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lambda where | |
----------------------------------------------------------- | |
----------------------- API ------------------------------- | |
----------------------------------------------------------- | |
num2fun :: Int -> Fun | |
num2fun n = Fun (show n) [] $ go n | |
where | |
go n | n < 0 = E[F neg, go (-n)] |