Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active December 18, 2015 17:09
Show Gist options
  • Save chomado/5816872 to your computer and use it in GitHub Desktop.
Save chomado/5816872 to your computer and use it in GitHub Desktop.
Lispみたいな式を簡単に書く練習メモ(すごいHaskell本より)
-- 関数適用演算子$
sum (filter (> 10) (map (* 2) [ 2 .. 10]))
sum $ filter (> 10) $ map (* 2) [ 2 .. 10]
-- 関数合成.
map (\x -> negate (abs x)) [5, -3, -6, 7, -3, 2, -19, 24]
map (negate . abs) [5, -3, -6, 7, -3, 2, -19, 24]
map (\xs -> negate (sum (tail xs))) [[1..5], [3..6], [1..7]]
map (negate . sum . tail) [[1..5], [3..6], [1..7]]
sum (replicate 5 (max 6.7 8.9))
sum . replicate 5 $ max 6.7 8.9
replicate 2 (product (map (+3) (zipWith max [1,2] [4,5])))
replicate 2 . product . map (+3) $ zipWith max [1,2] [4,5]
sum (takeWhile (<10000) (filter odd (map (^2) [1..])))
sum . takeWhile (<10000) . filter odd $ map (^2) [1..]
@edwardJeanx
Copy link

теперь я понимаю, что имели ввиду, когда рассказывали про китайский код :)

@chomado
Copy link
Author

chomado commented Jun 20, 2013

Thanks! I practiced how to shorten my code with the function composition and function application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment