Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString.Char8 as B
import qualified Data.Text as T
import Data.Maybe
import Snap
import Snap.Snaplet.Heist
-- file: ch03/GrahamScan.hs
import Data.List
import Data.Ord
type Point = (Double, Double)
data Direction = LeftTurn
| RightTurn
| Straight
import Control.Monad
import Data.Maybe
maybeToMonad :: (MonadPlus m) => Maybe a -> m a
maybeToMonad Nothing = mzero
maybeToMonad (Just x) = return x
type Sheep = Int
father :: Sheep -> Maybe Sheep
-- file: ch15/Supply.hs
next = S $ do st <- get
case st of
[] -> return Nothing
(x:xs) -> do put xs
return (Just x)
@qzchenwl
qzchenwl / gist:1136647
Created August 10, 2011 11:57
最大质因子
getD n = getD' n 2 where
getD' n factor | n == factor = factor
| n `mod` factor == 0 = getD' (n `div` factor) factor
| otherwise = getD' n (succ factor)
@qzchenwl
qzchenwl / gist:1129088
Created August 6, 2011 06:41
Eight queens puzzle
import Data.List
isUnique :: (Ord a) => [a] -> Bool
isUnique = all (null . drop 1) . group . sort
cols = [0..7]
solutions = [vec | vec <- permutations cols
, isUnique [vec!!i + i | i <- cols]
, isUnique [vec!!i - i | i <- cols]]
@qzchenwl
qzchenwl / gist:1129057
Created August 6, 2011 05:58
eight queens in haskell
import Data.List
isUnique :: (Ord a) => [a] -> Bool
isUnique = all (null . drop 1) . group . sort
cols = [0..7]
solutions = [vec | vec <- permutations cols
, isUnique [vec!!i + i | i <- cols]
, isUnique [vec!!i - i | i <- cols]]