2015年1月2日金曜日

150102(3)

Haskell


アプリカティブ・スタイル

リスト

ついでに141224分もやり直してみました。

Prelude> import Control.Applicative
Prelude Control.Applicative> [(*0),(+100),(^2)] <*> [1,2,3]
[0,0,0,101,102,103,1,4,9]
Prelude Control.Applicative> [(+),(*)] <*> [1,2] <*> [3,4]
[4,5,5,6,3,4,6,8]
Prelude Control.Applicative> import Data.List
Prelude Control.Applicative Data.List> sort [x + y | x <- [2,5,10], y <- [8,10,1
1]]
[10,12,13,13,15,16,18,20,21]
Prelude Control.Applicative Data.List> sort ((+) <$> [2,5,10] <*> [8,10,11])
[10,12,13,13,15,16,18,20,21]
Prelude Control.Applicative Data.List> [x | x <- [220,230..320]] \\ ((+) <$> [0,
50..300] <*> [0,80..320])
[220,270]

Zip リスト

Prelude> import Control.Applicative
Prelude Control.Applicative> getZipList $ (+) <$> ZipList [1,2,3] <*> ZipList [1
00,100..]
[101,102,103]
Prelude Control.Applicative> getZipList $ max <$> ZipList [1,2,3,4,5,3] <*> ZipL
ist [5,3,1,2]
[5,3,3,4]
Prelude Control.Applicative> getZipList $ (,,) <$> ZipList "dog" <*> ZipList "ca
t" <*> ZipList "rat"
[('d','c','r'),('o','a','a'),('g','t','t')]

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。