2011-10-11
(6) sort と sortBy
function |
- http://zvon.org/other/haskell/Outputlist/sort_f.html
- http://zvon.org/other/haskell/Outputlist/sortBy_f.html
import List で使えます。
module Main where import List p = print . show firstDigitOrder a b | (mod a 10) < (mod b 10) = LT | otherwise = GT main = do p $ sort [45, 12, 34, 58, 27, 16] p $ sortBy firstDigitOrder [45, 12, 34, 58, 27, 16]
sortBy は、ルッビ~的なイメージで変換用関数を渡すのではなく、ソート対象の二値を取って Ordering を返す関数を渡す。
ルッビ~風 sortBy は、適当に toOrdering のような関数を定義すればできそう、というか、
module Main where import List p = print . show toOrdering fn a b | (fn a) < (fn b) = LT | otherwise = GT main = do p $ sort [45, 12, 34, 58, 27, 16] p $ sortBy (toOrdering (\x -> mod x 10)) [45, 12, 34, 58, 27, 16]
import Data.Function
http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base/Data-Function.html#v%3Aon
この辺と見受けられるので次回見てみます