Re: [比較] 我為何鍾情於用 Scala 做為兵刃(三)

看板PLT (程式語言與理論)作者 (godfat 真常)時間13年前 (2011/07/11 22:23), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串9/11 (看更多)
※ 引述《scwg ( )》之銘言: : ※ 引述《huei1224 (渣)》之銘言: : : 最近在學 haskell ,發現 haskell 也可以寫成像上面的型式,分享一下 : : let x -: f = f x : : let xs = [-3..3] : : let square = (^2) : : let isGreaterThan5 = (>5) : : let result = xs -: map square -: filter isGreaterThan5 -: length : : -- let result = xs -: map (^2) -: filter (>5) -: length : 通常用 Haskell 的人會這樣寫 : result = length $ filter (>5) $ map (^2) $ xs : 不過如果想用 flip (.) 的話不如試試 : import Control.Monad.List : result = length $ xs >>= return . (^2) >>= guard . (>5) 獻醜一下 XD import Control.Category ((>>>)) result = flip ($) xs $ map (^2) >>> filter (>5) >>> length -- #!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》 # if a dog nailed extra legs that http://www.ptt.cc/bbs/Ruby/index.html # walks like an octopus, and Welcome ~Ruby@ptt~ # talks like an octopus, then ◢█◣ http://www.ruby-lang.org/ # we are happy to treat it as http://www.ruby-doc.org/ # if it were an octopus. http://www.rubyforge.org/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.25.231.110

07/12 02:21, , 1F
Good thinking :) It's just that I think it's more natural
07/12 02:21, 1F

07/12 02:22, , 2F
to learn monad before arrow/category.
07/12 02:22, 2F

07/12 04:41, , 3F
true, thanks! actually i don't really get arrow yet..
07/12 04:41, 3F

07/12 05:34, , 4F
flip ($) xs = ($ xs)
07/12 05:34, 4F

07/12 15:18, , 5F
good point, thanks!
07/12 15:18, 5F
文章代碼(AID): #1E6mTdfu (PLT)
討論串 (同標題文章)
文章代碼(AID): #1E6mTdfu (PLT)