Skip to content

Instantly share code, notes, and snippets.

View shangaslammi's full-sized avatar

Sami Hangaslammi shangaslammi

  • Infer Oy
  • Tampere, Finland
  • 10:14 (UTC +02:00)
View GitHub Profile
@shangaslammi
shangaslammi / functional.py
Created February 2, 2011 18:18
Small prototype for playing around with functional concepts in Python (WORK IN PROGRESS)
__all__ = [
"functional",
"later",
"X", "Y", "Z", "IF"
]
import functools
import operator
@shangaslammi
shangaslammi / katapotter.hs
Created February 3, 2011 09:15
Sample implementation of KataPotter (http://codingdojo.org/cgi-bin/wiki.pl?KataPotter) in Haskell (FYI, it's my first program in Haskell, so it might not be all that idiomatic)
import List
import Array
import System (getArgs)
discounts = listArray (1,5) ([1.0, 0.95, 0.9, 0.8, 0.75]::[Float])
bookPrice = 8.0::Float
grouped :: Ord a => [a] -> [Int]
grouped = sort . (map length) . group . sort
@shangaslammi
shangaslammi / ocr.hs
Created June 20, 2011 16:52
KataBankOCR
import Data.List
import Data.Maybe
import Control.Monad
import Control.Arrow
import System.Environment
newtype Account = Account { toList :: [Maybe Int] } deriving (Eq)
data Status = OK | Illegible | Error | Amb [Account] deriving (Eq)
instance Show Status where
@shangaslammi
shangaslammi / poker.hs
Created June 21, 2011 17:20
Defining poker hands in Haskell using custom combinators
match High = 1 `with` same value
match Pair = 2 `with` same value
match TwoPair = 2 `with` same value `plus` 2 `with` same value
match ThreeOfKind = 3 `with` same value
match Straight = 5 `with` sequential value
match Flush = 5 `with` same suit
match FullHouse = 3 `with` same value `plus` 2 `with` same value
match FourOfKind = 4 `with` same value
match StraightFlush = 5 `with` same suit `and` sequential value
@shangaslammi
shangaslammi / regex.hs
Created July 3, 2011 18:47
Simple regexp matcher in Haskell
module Regex (match) where
import Data.List
data Token = C Char | Start | End deriving (Eq)
match :: String -> String -> Bool
match re = not . null . filter (not.null) . fmap match' . tails . tokenize
where match' = flip (foldl' (>>=)) (compile re) . return
tokenize = (Start:) . (++[End]) . map C
@shangaslammi
shangaslammi / Roman.hs
Created October 3, 2011 19:52
Roman numerals kata in Haskell
module Roman where
import Data.Tuple
import Data.List
values = [
(1000, 'M'),
(500, 'D'),
(100, 'C'),
@shangaslammi
shangaslammi / webspider.hs
Created October 4, 2011 08:25
Haskell Web Spider example
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class
import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Lazy.UTF8 (toString)
import Data.Function
import Data.Enumerator
import Data.List
import Data.Maybe
@shangaslammi
shangaslammi / nfa.hs
Created November 18, 2011 15:43
A simple NFA in Haskell
-- See: http://swizec.com/blog/strangest-line-of-python-you-have-ever-seen/swizec/3012
import Control.Monad
import Data.Maybe
import System.Environment
data NFA s i = NFA s [s] [((s,i),[s])]
runNFA (NFA i f t) = any (`elem` f) . foldM step i where
step s c = fromMaybe [] $ lookup (s,c) t
@shangaslammi
shangaslammi / Algebra.hs
Created December 21, 2011 16:04
Statically Type-Checked Vector and Matrix Algebra
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances, EmptyDataDecls, OverlappingInstances, FlexibleContexts #-}
module Data.Algebra where
import Prelude hiding (Num(..), (/))
import qualified Prelude as P
import Control.Applicative
import Control.Arrow (first, second)
import Data.Ratio (Ratio, (%))
@shangaslammi
shangaslammi / CustomShow.hs
Created December 27, 2011 19:59
Template Haskell Example
{-# LANGUAGE TemplateHaskell, FlexibleInstances #-}
module CustomShow where
import Language.Haskell.TH
import Data.List (intercalate)
emptyShow :: Name -> Q [Dec]
emptyShow name = [d|instance Show $(conT name) where show _ = ""|]