Skip to content

Instantly share code, notes, and snippets.

swap i j xs = reverse $ fst $ foldl f ([],0) xs
where
f (acc, idx) x | idx == i = (xs!!j:acc, idx+1)
| idx == j = (xs!!i:acc, idx+1)
| otherwise = (x:acc, idx+1)
swap i j xs0 = swap' 0 xs0
where
swap' idx [] = []
swap' idx (x1:xs1) | idx == i = xs0 !! j : swap''
| idx == j = xs0 !! i : swap''
| otherwise = x1 : swap''
where
swap'' = swap' (idx+1) xs1
swap i j xs0 = swap' [] withIdx
where
withIdx = zip [0..] xs0
swap' acc [] = reverse acc
swap' acc ((idx,x1):xs1) | idx == i = swap' ((xs0!!j):acc) xs1
| idx == j = swap' ((xs0!!i):acc) xs1
| otherwise = swap' (x1:acc) xs1
swap i j xs = let a = slice' 0 (i-1)
b = slice' (i+1) (j-1)
c = slice' (j+1) (length xs - 1)
in a ++ (xs!!j):b ++ (xs!!i):c
where
slice' i j = slice i j xs
slice i j = snd . splitAt i . fst . splitAt (j+1)
swap 0 j xs'@(x:xs) = xs' !! j : take (j-1) xs
++ x : drop j xs
swap i j (x:xs) = x : swap (i-1) (j-1) xs
^+Q:: ; Ctrl + Shift + Q
Send ^c ; 対象のソースコードをコピー
Send !{Tab} ; Alt + Tab でウィンドウを切り替える
; Windows Live Writer がアクティブになるのを待つ
SetTitleMatchMode, 2
WinWaitActive, Windows Live Writer, , 2
; クリップボードの内容を変更
clipboard = <pre class="prettyprint">%clipboard%</pre>
import Control.Monad.State
type Word = String -- 最長の文字列
type Idx = Int -- 最長の文字列のインデックス
type Index = Int -- ループで使うインデックス
longestWord' :: [String] -> State Index (Word, Idx)
longestWord' (x:xs) = foldl f (return (x, 0)) xs
where
f :: State Index (Word, Idx) -> String -> State Index (Word, Idx)
import Text.Regex
import Control.Monad.State
longestWord' :: [String] -> State (String, Int) ()
longestWord' [] = return ()
longestWord' (x:xs) = do x1@(word, idx) <- get
put (x, idx+1)
longestWord' xs
x2@(word', idx') <- get
if length word < length word' then put x2 else put x1
import Text.Regex
type State s a = s -> (a, s)
comb :: State s a -> (a -> State s b) -> State s b
comb m k = \s -> let (x1, s') = m s in k x1 s'
comb_ :: State s a -> State s b -> State s b
comb_ m n = m `comb` \_ -> n
import Text.Regex
import Control.Monad.State
longestWord' :: [String] -> State Int String
longestWord' [x] = return x
longestWord' (x:xs) = do x' <- longestWord' xs
if length x < length x'
then do inc
return x'
else return x