Skip to content

Instantly share code, notes, and snippets.

View BillyBadBoy's full-sized avatar

William Morgan BillyBadBoy

  • London UK
View GitHub Profile
module Lambda where
-----------------------------------------------------------
----------------------- API -------------------------------
-----------------------------------------------------------
num2fun :: Int -> Fun
num2fun n = Fun (show n) [] $ go n
where
go n | n < 0 = E[F neg, go (-n)]
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
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
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)
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
-----------------------------------------------------------------------
-- [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:
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
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
---------------------------------------------------------------------------
@BillyBadBoy
BillyBadBoy / week1.hs
Last active October 10, 2016 12:14
Week 1 exercises.
-- [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