g ͱɺఆٛҬ Y ͔ΒҬ Z ͷ͋Δؔ f ͕͋Δͱ͖ ҎԼͷࣜΛຬͨ͢ఆٛҬ X ͔Β Ҭ Z ͷؔ h ͕ଘࡏ͠·͢ h (x) = f ( g ( x ) ) h Λ f ◦ g ͱॻ͖fͱgͷؔ߹ʹ ΑΔ߹ؔͱݴ͍·͢ (f ◦ g)( x ) = f( g ( x ) ) countChar :: String -> String -> Char -> String countChar fmt str c = printf fmt $ length $ filter (== c) str main = do putStrLn (countChar "a num %d" "a characters" 'a') putStrLn (countChar "c num %d" "a characters" 'c') X Y Z g f f ° g h ؔ߹
= let x = undefined : x in null x nullTake :: Int nullTake = let x = undefined : x in length (take 10 x) main = do print (take 10 [0..]) -- ແݶͷྻΛѻ͏ print (length [ undefined, undefined, undefined ]) -- ະఆٛͳͷྻѻ͑Δ print emptyList -- ۭϦετνΣοΫʹະఆٛΛ͢ print nullTake -- ະఆٛͳΛ10ճऔಘ ԆධՁͳͷͰແݶΛѻ͏͜ͱՄೳ
-> Int -> Int -> Int tarai x y z | x <= y = y | otherwise = tarai (tarai (x - 1) y z) (tarai (y - 1) z x) (tarai (z - 1) x y) main = do args <- getArgs let nums = map cv args where cv str = read str :: Int if length nums >= 3 then print (tarai (nums !! 0) (nums !! 1) (nums !! 2)) else print "illegal argument !!" // C #include <iostream> #include <cstdlib> int tarai(int x, int y, int z) { return x <= y ? y :tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y) ); } int main(int argc, char* argv[]) { if (argc < 4) return 1; int x = std::atoi(argv[1]); int y = std::atoi(argv[2]); int z = std::atoi(argv[3]); std::cout << tarai(x, y, z) << std::endl; return 0; } ./tarai 20 10 0 # ऴΘΒͳ͍ ./tarai 20 10 0 # ऴΘΔ
ɾX,Y∈O ʹରͯ͠A(X,Y)ɺରX͔ΒରYͷࣹͷू߹ ɾ߃ࣹ X∈Oʹରͯ͠ idxɿX → X ͕ఆ·͍ͬͯΔ ɾ߹ɹ݁߹ɿҙͷࣹʹରͯ͠ h◦(g◦f) = (h◦g)◦f ɹ୯Ґɿҙͷࣹ X→Yʹରͯ͠ id y ◦ f = f = f ◦ id x X Y Z f g f ◦ g ʁʁʁ ʁʁʁ