Drkcore

18 11 2009 Haskell Tweet

Haskellの関数合成

ご近所haskellerのringtaroさんがつぶやいてたので。

Prelude> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Prelude> :t ($)
($) :: (a -> b) -> a -> b

(.)はb->cとa->bっていう関数を受け取ってa->cという関数を返す。だから関数合成

Prelude> :t head
head :: [a] -> a
Prelude> :t (drop 1)
(drop 1) :: [a] -> [a]
Prelude> :t head . (drop 1)
head . (drop 1) :: [a] -> a

($)は dropを評価した結果のリストをheadにわたすだけ。なので、下の書き方はだめ。

Prelude> :t ($) head (drop 1)

<interactive>:1:10:
    Couldn't match expected type `[a]'
           against inferred type `[a1] -> [a1]'
    In the second argument of `($)', namely `(drop 1)'
    In the expression: ($) head (drop 1)

括弧省略の記法なのに括弧入れまくり

Prelude> :t ($) head ((drop 1) [1,2,3])
($) head ((drop 1) [1,2,3]) :: (Num a) => a

ProductName プログラミングHaskell
Graham Hutton
オーム社 / ¥ 2,940 ()
通常3~6週間以内に発送

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021