Skip to content

Instantly share code, notes, and snippets.

View AndrasKovacs's full-sized avatar

András Kovács AndrasKovacs

View GitHub Profile
@AndrasKovacs
AndrasKovacs / SmallHindleyMilner.hs
Last active March 6, 2017 09:31
Minimal Hindley-Milner-Damas type inference implementation. Has recursive let.
{-# LANGUAGE LambdaCase, TupleSections, TemplateHaskell, DeriveFunctor, DeriveFoldable, DeriveTraversable #-}
import qualified Data.IntMap.Strict as IM
import qualified Data.IntSet as IS
import qualified Data.Foldable as F
import Control.Applicative
import Control.Lens
import Control.Monad.State.Strict
import Debug.Trace
@AndrasKovacs
AndrasKovacs / FastBoggle.hs
Last active January 1, 2016 22:38
Fast Boggle solver. Based on my previous Boggle gist, but now with more bitsets, multicore and hackish chunking. Currently it takes around 15 ms file reading included with the TWL06 word list, an i7 3770 and the -N4 flag.
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as UV
import qualified Data.ByteString.Char8 as B
import Data.Ix
import Text.Printf
import Data.List
import Data.Ord
import System.Environment
import Data.Bits
@AndrasKovacs
AndrasKovacs / FinalBoggle.hs
Last active January 2, 2016 00:29
The Boggle to end all Boggles has arrived! Runs in 60 usec on my machine. It uses a DAWG generated from the TWL06 Scrabble dictionary.
{-
Copyright (C) 2014 András Kovács
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
@AndrasKovacs
AndrasKovacs / DualKnapsack.hs
Last active January 4, 2016 22:09
Adaptively using the DP knapsack solution or the solution of its dual depending on the weight limit. It's more of an exercise in theory since both functions leak space rather severely.
{-# LANGUAGE LambdaCase #-}
import Data.List
import Control.Arrow
import Control.Lens
inv = [("map",9,150), ("compass",13,35), ("water",153,200), ("sandwich",50,160),
("glucose",15,60), ("tin",68,45), ("banana",27,60), ("apple",39,40),
("cheese",23,30), ("beer",52,10), ("cream",11,70), ("camera",32,30),
("tshirt",24,15), ("trousers",48,10), ("umbrella",73,40),
@AndrasKovacs
AndrasKovacs / Combinatorics.hs
Created January 29, 2014 11:25
Combinatorics.
module Combinatorics (powerset, combsOf, perms) where
powerset :: [a] -> [[a]]
powerset (x:xs) = let xs' = powerset xs in xs' ++ map (x:) xs'
powerset [] = [[]]
combsOf :: Int -> [a] -> [[a]]
combsOf n _ | n < 1 = [[]]
combsOf n (x:xs) = combsOf n xs ++ map (x:) (combsOf (n - 1) xs)
combsOf _ _ = []
@AndrasKovacs
AndrasKovacs / CoinChange.hs
Last active January 4, 2016 22:09
DP solution to SICP Chapter 1.2 coin change problem.
coinChange :: [Int] -> [Int]
coinChange = foldr go (1: repeat 0) where
go coin xs = xs' where
(a, b) = splitAt coin xs
xs' = a ++ zipWith (+) xs' b
main = print $ coinChange [1,5,10,25,50] !! 100 -- prints 292
@AndrasKovacs
AndrasKovacs / RightListT.hs
Last active August 29, 2015 13:55
My shot at ListT done right.
{-# LANGUAGE LambdaCase, DeriveFunctor #-}
import Prelude hiding (take, head)
import Control.Monad
import Control.Monad.Trans
data ListT' m a = Nil | Cons a (ListT m a) deriving (Functor)
newtype ListT m a = ListT {runListT :: m (ListT' m a)} deriving (Functor)
instance Monad m => Monad (ListT m) where
@AndrasKovacs
AndrasKovacs / Notes.hs
Last active August 29, 2015 13:56
Random notes.
{-# LANGUAGE
LambdaCase, DeriveFunctor, FlexibleContexts,
RankNTypes, TemplateHaskell, NoMonomorphismRestriction #-}
-- golfing the state monad with free
import Control.Monad
import Control.Monad.Free
import Control.Monad.Free.TH
@AndrasKovacs
AndrasKovacs / GildedRose.hs
Last active August 29, 2015 13:56
Gilded Rose kata with lens and ADTs (of course we don't observe the original "don't change the data structure" constraint here).
{-# LANGUAGE
TemplateHaskell, LambdaCase, MultiParamTypeClasses,
FlexibleInstances, FunctionalDependencies #-}
import Control.Applicative
import Control.Lens
import Control.Lens.Extras
data Item = Simple {_sellIn :: Int, _quality :: Int, _itemName :: String}
| Conjured {_sellIn :: Int, _quality :: Int, _itemName :: String}
@AndrasKovacs
AndrasKovacs / IAmNotAFiniteNumber.hs
Last active August 29, 2015 13:56
De Bruijn indexing as in "http://www.cs.ru.nl/~james/RESEARCH/haskell2004.pdf" but with statically checked finite indices.
{-# LANGUAGE
DataKinds, GADTs, TypeFamilies,
ScopedTypeVariables, LambdaCase,
TemplateHaskell, StandaloneDeriving,
DeriveFunctor, DeriveFoldable, DeriveTraversable, TypeOperators #-}
import Data.Singletons.TH
import Data.Foldable (Foldable)
import Data.Traversable (Traversable)