2017-06-10 6 views
2

、以下の機能オペレータP(または概念の名前)が何である:一般的なパターンマッチング機能オペレータの名前、関数型プログラミングにおいて

二つの機能fgを考えると、そして、述語関数pP(p, f, g)は、私は番目のかどうかを疑問に思って

x → if (p(x)) f(x) else g(x) 

機能です演算子は確立された名前を持っているので、私は自分のコードでその名前を使うことができます。 (すなわち、私は従来の名前のPを与えたいと思っています)

答えて

2

私は、関数モナドに持ち込まれたifの演算子だと言います。

Haskellではたとえば、あなたは文字通りブールライブラリに

import Control.Monad 
let if' c t f = if c then t else f -- another common name is `ite` 
let ifM = liftM3 if'    -- admittedly the type of this is too generic 
--  ^^^^^^^^^^ 
let example = ifM even (\x -> "t "++show x) (\x -> "f "++show x) 
example 1 -- "f 1" 
example 2 -- "t 2" 
0

別のHaskellの例Point-wise conditionalを行うことができ、それはブール値を保持しているApplicative取る

cond :: (Applicative f, IfB a, bool ~ BooleanOf a) => f bool -> f a -> f a -> f a 

、2 TrueFalseの値を持つ別のApplicatives場合はApplicative結果を生成します。

Applicativeという種類があり、機能はそのうちの1つのみです。

> f = cond (\x -> x > 1) (\x -> x/10) (\x -> x * 10) 
> f 2.0 
# 0.2 
> f 0.13 
#1.3 

オプション値Maybe

> cond (Just True) (Just 10) (Just 20) 
# Just 10 
> cond (Just True) (Just 10) Nothing 
# Nothing 

ListApplicative

> cond [True, False, True] [10] [1, 2] 
# [10,10,1,2,10,10] 
> cond [True, False, True] [10] [1] 
# [10, 1, 10] 
> cond [True, False, True] [10] [] 
# [] 
ある別の有用な例であります
関連する問題