Skip to content

Instantly share code, notes, and snippets.

import Data.List
newline [a] = []
newline (a:b:xs) = abs (a-b) : (newline (b:xs))
check prev [] = True
check prev line = all (`notElem` prev) line && nub line == line && check (line ++ prev) (newline line)
main = print $ filter (check []) $ concatMap permutations $ filter ((==5) . length) $ subsequences [1..15]
@sharkdp
sharkdp / coins.hs
Last active August 29, 2015 13:59
min/max algorithm for coins game
-- Simple min/max algorithm playing a game with coins
--
-- Rules:
-- > Initially, there is a stack of N coins
-- > Players take 1, 2 or 3 coins
-- > The player who takes the last coin loses
import Data.List (maximumBy, minimumBy)
import Data.Ord (comparing)
import System.IO
@sharkdp
sharkdp / coinset.hs
Created June 4, 2015 12:49
Find the smallest number of coins to represent all numbers between 1 and 99
import Data.List
coins :: [Int]
coins = [1, 2, 5, 10, 20, 50]
type CoinSet = [Int]
-- | All coinsets of a given length
draw :: Int -> [CoinSet]
draw 0 = [[]]
@sharkdp
sharkdp / paniclab.purs
Created July 26, 2015 09:39
Simulation of the game Panic Lab
module Main where
import Prelude
import Control.Monad (when)
import Control.Monad.Eff
import Control.Monad.Eff.Console
import Data.Int
import Data.Int.Bits
import Data.List
module Main where
import Prelude
import Data.Array (reverse)
import Data.Foldable (traverse_)
import Data.Monoid
import Data.Traversable (traverse)
import Control.Applicative.Free
module Main where
import Prelude
import Control.Apply ((*>))
import Signal (Signal(), merge)
import Signal.Time (every, second)
import Flare (UI(), liftSF, button, foldp, runFlareShow)
tick :: Signal Int
tick = every second *> pure 1
@sharkdp
sharkdp / Main.purs
Created February 8, 2016 22:14
Celsius to Fahrenheit (small)
module Main where
import Prelude
import Flare
celsius = number "Celsius" 0.0
convert c = "Fahrenheit: " <> show (c * 9.0 / 5.0 + 32.0)
main = runFlare "controls" "output" (map convert celsius)
@sharkdp
sharkdp / Main.purs
Created March 22, 2016 18:47
Rendering with additional effects
module Main where
import Prelude
import Data.Int
import Data.Array
import Data.Maybe
import Data.Foldable
import Control.Monad.Eff
{-# LANGUAGE DeriveFunctor #-}
import Data.Monoid
import Control.Monad
import Control.Applicative
-- | An applicative functor `f` over a base type `b` that is "annotated" by a
-- | monoidal type `a`.
data Ann a f b = Ann a (f b)
deriving (Eq, Ord, Show, Functor)
module Main where
import Prelude
import Control.Monad.Eff.Console
import Data.Int as I
import Data.Complex as C
import Math as M
class Ring r <= Absolute r where