Skip to content

Instantly share code, notes, and snippets.

import Control.Applicative ((<*>))
import Control.Arrow (first,(***))
runlength :: (Eq a) => [a] -> [(a,Int)]
runlength = hylo ((:) . runlen) [] null (span' . (==) . head <*> id)
runlen :: ([a],Int) -> (a,Int)
runlen = first head
hylo f e p g x = if p x then e else f y (hylo f e p g x')
@nobsun
nobsun / file0.txt
Last active August 29, 2015 14:26
順列列挙関数(素朴な実装) ref: http://qiita.com/nobsun/items/babd28fe81ba3b9f304f
perms :: [a] -> [[a]]
perms :: [a] -> [[a]]
permNth :: [a] -> Int -> [a]
@nobsun
nobsun / file0.txt
Last active February 3, 2017 12:41
Haskellで「ひらけ!ポンキッキ」 (文字列の回転) ref: http://qiita.com/nobsun/items/0f453affdcdd45f2982a
module Main where
import Data.List (tails)
rots :: String -> [String]
rots s = take n $ map (take n) $ tails $ cycle s
where
n = length s
main :: IO ()
import Control.Arrow
import Data.Char
type Field = ([Int],Int)
ifield :: Field
ifield = (repeat 0,0)
data Piece = I | L | O | S | T deriving (Eq,Show,Read)
type Pos = Int
@nobsun
nobsun / F02.hs
Created March 6, 2017 10:30
2つの矩形に含まれるマス目の数 ref: http://qiita.com/nobsun/items/3b436fa0349e8bd5de29
module F02 where
import Control.Arrow
import Data.Ix
import Data.List
type Range = ((Int,Int),(Int,Int))
readRange :: String -> Range
readRange s = case break (','==) s of
{-# LANGUAGE FlexibleContexts #-}
module E11 where
import Data.List
import Data.Ord
import qualified Data.Set as S
import Data.Tree
import Math.NumberTheory.ArithmeticFunctions
type NodeID = Int
@nobsun
nobsun / F01.hs
Last active March 8, 2017 21:06
ふたマスの領域の数 ref: http://qiita.com/nobsun/items/c29e0e56de478303259a
module F01 where
import Control.Arrow
import Numeric
import Data.Char
import Data.Function
import Data.List
import Text.Printf
import Debug.Trace
@nobsun
nobsun / O24.hs
Last active March 20, 2017 01:41
関数プログラミングの気分(『多段階選抜』問題を題材にして) ref: http://qiita.com/nobsun/items/4562b728ecd560557bd2
module O24 where
import Data.Char (digitToInt)
import Data.List (intercalate)
type Problem = String
type Answer = String
o24 :: Problem -> Answer
o24 = intercalate "," -- 文字列のリストを "," を挟んで連結する