Skip to content

Instantly share code, notes, and snippets.

@pb10005
Last active July 20, 2018 13:31
Show Gist options
  • Save pb10005/c89dd27afcc7d1235375c2090fb8da5c to your computer and use it in GitHub Desktop.
Save pb10005/c89dd27afcc7d1235375c2090fb8da5c to your computer and use it in GitHub Desktop.
-- 2つのリストの差を求める
difList::[Int]->[Int]->Int->[Int]
difList xs ys n = [(xs!!x) - (ys!!x)|x<-[0..n-1]]
readIntList::IO[Int]
readIntList = map read . words <$> getLine
-- IO Stringから整数のリストを読み込む
readArray::IO String -> IO[Int]
readArray = fmap $ map read . words
-- 文字列に含まれる指定した文字の個数を数える
count::[Char] -> Char -> Int
count xs c = length [x| x <- xs, x == c]
-- 2つのリストの差を求める
difList::[Int] -> [Int] -> [Int] -> [Int]
difList (x:xs) (y:ys) sumList
|xs == [] = sumList++[x-y]
|otherwise = difList xs ys (sumList++[x-y])
--差の絶対値
absDif x y
| x > y = x - y
|otherwise = y - x
--クイックソート
qSort::[Int] -> [Int]
qSort [] = []
qSort (x:xs)
| xs == [] = [x]
| otherwise = qSort [y|y<-xs, y < x] ++ [x] ++ qSort [z|z<-xs, z >= x]
--クイックソート(降順)
qSortDesc::[Int] -> [Int]
qSortDesc [] = []
qSortDesc (x:xs)
| xs == [] = [x]
| otherwise = qSort [y|y<-xs, y > x] ++ [x] ++ qSort [z|z<-xs, z <= x]
--中央値
median::[Int] -> Int
median xs =
let l = length xs
n = div l 2
ms = qSort xs
in if mod l 2 == 0 then
div ((ms !! (n - 1)) + (ms !! n)) 2
else
(ms !! n)
--n以下のm乗数のリストを生成
genExp m n = takeWhile (<=n) (iterate (*m) m)
--n以下の累乗数のリストを作成
genExp' n = concat [takeWhile (<= n) (iterate (*x) (x*x))|x<-[2..n]]
--累積和
cumulativeSum:: [Int]->[Int]->[Int]
cumulativeSum [] list = list
cumulativeSum (x:xs) sumList = cumulativeSum xs (sumList++[x + last sumList])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment