Skip to content

Instantly share code, notes, and snippets.

View motokiee's full-sized avatar
🎯
Focusing

motokiee motokiee

🎯
Focusing
  • Mercari, Inc.
  • Tokyo
  • 10:53 (UTC +09:00)
  • X @motokiee
View GitHub Profile
initials :: [(String, String)] -> [String]
initials xs = [pickup first last | (first, last) <- xs]
where pickup first last = [head first] ++ ". " ++ [head last] ++ "."
squareList :: [Int] -> [Int]
squareList xs = [ square' | x <- xs, let square' = x ^ 2]
calcBmis :: [(Double, Double)] -> [Double]
calcBmis xs = [ bmi | (w,h) <- xs, let bmi = w / h ^ 2, bmi > 30.0]
head' :: [a] -> a
head' [] = error "No Head"
head' (x:_) = x
head2' :: [a] -> a
head2' xs = case xs of [] -> error "No Head"
(x:_) -> x
describeList :: [a] -> String
describeList ls = "This list is" ++ case ls of [] -> " empty"
@motokiee
motokiee / CodePiece.hs
Created October 2, 2015 00:42
けさのHaskellの勉強はここまで!そしてCodePieceを使ってみた。 #CodePiece
replicate' :: Int -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x : (replicate' (n-1) x)
maximum' :: (Ord a) => [a] -> a
maximum' xs = case xs of [] -> error "maximum of empty list"
[x] -> x
(x:xs) -> max x (maximum' xs)
@motokiee
motokiee / CodePiece.hs
Created October 2, 2015 00:43
けさのHaskellの勉強はここまで #CodePiece
replicate' :: Int -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x : (replicate' (n-1) x)
maximum' :: (Ord a) => [a] -> a
maximum' xs = case xs of [] -> error "maximum of empty list"
[x] -> x
(x:xs) -> max x (maximum' xs)
@motokiee
motokiee / CodePiece.hs
Created October 6, 2015 04:20
すごいH本の再帰関数あたり #CodePiece
zip' :: [a] ->[b] -> [(a,b)]
zip' _ [] = []
zip' [] _ = []
zip' (x:xs) (y:ys) = (x,y) : zip' xs ys
elem' :: (Eq a) => a -> [a] -> Bool
elem' a [] = False
elem' a (x:xs)
| a == x = True
@motokiee
motokiee / CodePiece.hs
Created October 7, 2015 04:57
すごいくだらない関数だけどちゃんと自分で考えて再帰関数書けた記念 #CodePiece
largestDivisible :: [Int] -> Bool
largestDivisible [] = False
largestDivisible (x:xs)
| (x `mod` 3829) == 0 = True
| otherwise = largestDivisible xs
@motokiee
motokiee / CodePiece.hs
Created October 7, 2015 05:15
コラッツ列とか #CodePiece
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let smallOrEqual = filter (<= x) xs
larger = filter (> x) xs
in quicksort smallOrEqual ++ [x] ++ quicksort larger
largestDivisible :: [Int] -> Bool
largestDivisible [] = False
largestDivisible (x:xs)
@motokiee
motokiee / CodePiece.hs
Created October 12, 2015 06:18
コラッツ列とflipをラムダで書いたもの #CodePiece
chain :: Integer -> [Integer]
chain 1 = [1]
chain n
| even n = n : chain (n `div` 2)
| odd n = n : chain (n * 3 + 1)
numLongChains :: Int
numLongChains = length (filter (\xs -> length xs > 15) (map chain [1..100]))
flip' :: (a -> b -> c) -> b -> a -> c