Skip to content

Instantly share code, notes, and snippets.

@petermarks
petermarks / gist:510622
Created August 6, 2010 00:19
Notes for Johan Lindberg
In http://github.com/johanlindberg/langgeeks/commit/3e6249fcd36be6685cce2a6936da8dd2471cbd51
you have:
> process :: String -> (Char, [String])
> process filename = do contents <- readFile filename
> return (getPlayer contents, getBoard contents)
It looks like you want to read the file and extract the player and board to pass this on to
another function. All that is missing here is the IO in the type signature.
@petermarks
petermarks / GuessGame-abstracted.hs
Created August 22, 2010 22:21
Number guessing game
module Main where
import System.Random
import Data.Function
processGuess :: Int -> IO () -> IO ()
processGuess answer cont = do
n <- getLine
case compare (read n) answer of
EQ -> putStrLn "Awesome!!!!"
@petermarks
petermarks / Rope Intranet benchmark
Created September 5, 2010 21:02
Benchmark of Rope Intranet counting function
-- | This program benchmarks different versions of the counting function for
-- the Rope Intranet problem for Google Code Jam
-- http://code.google.com/codejam/contest/dashboard?c=619102#.
--
-- To compile:
-- >>> ghc -O2 --make bench.hs
--
-- To run (Windows):
-- >>> Bench
@petermarks
petermarks / guessMaker.hs
Created September 20, 2010 20:46
Number guesser
module Main where
makeAGuess :: Int -> Int -> Int
makeAGuess i j = (i + j) `div` 2
guess :: Int -> Int -> IO ()
guess i j = do
let x = makeAGuess i j
putStrLn (show x)
@petermarks
petermarks / guesser.hs
Created September 20, 2010 20:59
Number guesser iterations
module Main where
import Data.Char
import Data.List
data Guess
= Guess Int Guess Guess
| GiveUp
| Done
@petermarks
petermarks / expressions.hs
Created September 20, 2010 21:05
Simple arithmetic expression language
module Expressions where
import Data.Maybe
import Text.Printf
import Control.Applicative
data Expr = Term Int | Op Operator Expr Expr | Var String
deriving (Show, Eq)
data Operator = Sum | Mult | Sub | Div
@petermarks
petermarks / gist:702671
Created November 16, 2010 22:45
Expression parser
import Data.Maybe
import Text.Printf
import Data.Char
import Control.Monad
data Expr = Term Int | Op Operator Expr Expr | Var String
deriving (Show, Eq)
data Operator = Sum | Mult | Sub | Div
deriving (Show, Eq)
@petermarks
petermarks / gist:736615
Created December 10, 2010 18:57
Chess boards
module Main where
import Control.Arrow
import Data.Array
import Data.Char
import Data.Bits
import Data.Function
import Data.List
import Data.Maybe
type Bark = Array (Int, Int) Bool
@petermarks
petermarks / gist:782199
Created January 16, 2011 22:13
Theme Park (Naive)
module Main where
import Text.Printf
profit :: Integer -> Integer -> [Integer] -> Integer
profit cap rides groups
| sum groups <= cap = rides * sum groups
| otherwise = profit' cap rides (cycle groups)
where
@petermarks
petermarks / gist:791274
Created January 22, 2011 17:27
Theme Park (Optimized)
-- This program solves the Theme Park Google CodeJam problem at
-- http://code.google.com/codejam/contest/dashboard?c=433101#s=p2
--
-- I've tried to make the code clear whilst employing a selection of Haskell library
-- functions and idioms. I've also gone for efficiency, probably over optimizing in
-- places in order to demonstrate some techniques.
module Main where
import Data.List