Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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 / 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 / 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
{-# LANGUAGE FlexibleInstances #-}
instances Num (a -> Maybe b) where
fromInteger 1 = const Nothing
a = Nothing
ex = a >>= 1
テキストファイルを指定するとそのファイル名と,行番号をふった内容を印字するプログラムを作成せよ.
ただし,ファイルは複数指定できるものとし,行番号は(そのファイルでの)残りの行数を表す数である.
(対象ファイルがn行からなる場合,n-1から0までの番号が降順にふられる.)
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module AdventCalendar2012 where
module Main where
import System.Environment
main :: IO ()
main = mapM_ numbering =<< getArgs
numbering :: FilePath -> IO ()
numbering f = putStr . unlines . zipWith number [1..] . lines =<< readFile f
number :: Int -> String -> String
@nobsun
nobsun / ifphex010204.hs
Created November 23, 2012 08:12
IFPH 練習問題 1.2.4 簡約系列の列挙 ref: http://qiita.com/items/ffd0dab664422ed2b079
-- | 式の定義
data Expr = Zero
| Pred Expr
| Succ Expr
| Add (Expr, Expr)
deriving (Show)
data AExpr' = AZero
| APred AExpr