Skip to content

Instantly share code, notes, and snippets.

@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]]
@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: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)
-- file: ch15/Supply.hs
next = S $ do st <- get
case st of
[] -> return Nothing
(x:xs) -> do put xs
return (Just x)
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: ch03/GrahamScan.hs
import Data.List
import Data.Ord
type Point = (Double, Double)
data Direction = LeftTurn
| RightTurn
| Straight
{-# 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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
convert Microsoft Word 2007 docx files to wiki markup files
'''
from lxml import etree
import zipfile
import string
import sys
@qzchenwl
qzchenwl / unzip.hs
Created February 14, 2012 10:29
unzip file
{-# LANGUAGE OverloadedStrings #-}
import Codec.Archive.Zip
import Data.ByteString.Lazy (getContents, putStrLn)
import Data.ByteString.Lazy.Char8 (ByteString)
import Prelude hiding (getContents, putStrLn)
import Data.Maybe (maybe)
import System.Environment (getArgs)
import System.Exit (exitFailure)
main = do filePath <- getArgs >>= parse
@qzchenwl
qzchenwl / 2011[2].hs
Created February 23, 2012 11:00
puzzle
module Main where
import Data.Ratio
import Data.Tree
data Label = Label
{ f1 :: (Rational -> Rational)
, accept :: (Rational -> Bool)
, f2 :: (Rational -> Rational)