Skip to content

Instantly share code, notes, and snippets.

@nobsun
nobsun / coin.hs
Created May 22, 2013 22:27
複数の関数のメモ化はできるか ref: http://qiita.com/items/93e2110ddb80e1e6f2c1
{-# LANGUAGE TupleSections #-}
module Main where
import Data.Function.YaMemo (Memo, memo)
import Data.Map (Map)
main :: IO ()
main = undefined
@nobsun
nobsun / pascal.hs
Created June 11, 2013 23:25
Data.List.mapAccumR のつかいどころ ref: http://qiita.com/items/389da409cabceebd8ab7
import Data.List (mapAccumR)
pascal :: [[Integer]]
pascal = [1] : [f cs | cs <- pascal ]
where
f xs = snd $ mapAccumR g xs' xs'
where xs' = 0:xs
g (y:ys) z = (ys, y+z)
comb :: Int -> Int -> Integer
@nobsun
nobsun / delayList.hs
Last active January 31, 2019 00:00
遅延リストの間欠生成 ref: https://qiita.com/nobsun/items/850f48c8c8fbc2277816
module DelayList where
import Control.Concurrent (threadDelay)
import System.IO.Unsafe (unsafeInterleaveIO)
delayList :: Int -> [a] -> IO [a]
delayList _ [] = unsafeInterleaveIO $ return []
delayList i (x:xs)
= unsafeInterleaveIO $ threadDelay i >> delayList i xs >>= return . (x:)
@nobsun
nobsun / file0.txt
Created November 12, 2013 12:57
Lenovo ThinkPad X240s に Ubuntu 13.10 amd64 ja をインストール直後の設定 ref: http://qiita.com/nobsun/items/5b59358ec75c9b503ddd
$ sudo apt-get update
$ sudo apt-get upgrade
@nobsun
nobsun / deleteMin.hs
Created November 16, 2013 16:07
空ではないリストから最小の要素を1つだけ削除する ref: http://qiita.com/nobsun/items/49440368b85c36ec4d42
deleteMin :: Ord a => [a] -> [a]
deleteMin xs@(x:_) = snd $ para f (x,[]) xs
where
f x (xs,(y,_))
| x < y = (x,xs)
| otherwise = (y,x:xs)
para :: (a -> ([a],b) -> b) -> b -> [a] -> b
para _ z [] = z
para f z (x:xs) = f x (xs, para f z xs)
module Main where
import Data.List
main :: IO ()
main = putStr
$ unlines
$ map (intercalate ".")
$ map (map reverse) -- 部分文字列反転
$ unravels
@nobsun
nobsun / file0.txt
Created December 24, 2013 07:12
Haskell コード片鑑賞:「再帰」編 ref: http://qiita.com/nobsun/items/6c2fa63f294dbd98d5cb
factorial :: Integer -> Integer
factorial (n+1) = (n+1) * factorial n
factorial 0 = 1
@nobsun
nobsun / file1.txt
Created February 13, 2014 11:35
ジェムストリング問題 ref: http://qiita.com/nobsun/items/efe6a8965f5a5bfe5d41
ghci> :set +s
ghci> patternIndex "abbbbcddddeefggg" "eagcdfbe"
5578864439
(0.05 secs, 28983792 bytes)
@nobsun
nobsun / today.hs
Created February 20, 2014 01:05
今日の日付をISO 8601形式で表示する ref: http://qiita.com/nobsun/items/a01b96cbf5775f7959b7
module Main where
import Control.Applicative
import Data.Time
import Data.Time.Calendar.OrdinalDate
import Data.Time.Calendar.WeekDate
import System.Environment
main :: IO ()
main = dispatch =<< getProgName
@nobsun
nobsun / strictList.hs
Last active August 29, 2015 14:02
リストを(非)正格に構成する
data I a = I {unI :: a}
instance Functor I where
fmap f (I a) = I (f a)
instance Monad I where
return = I
I x >>= f = f x
foo :: [I a] -> I [a]