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

看板PLT (程式語言與理論)作者 ( )時間13年前 (2011/07/11 11:47), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串8/11 (看更多)
※ 引述《huei1224 (渣)》之銘言: : : ====================== 我是 Scala 程式分隔線 ========================= : : val xs = List(-1, -2, -3, 0, 1, 2, 3) : : val square = (n: Int) => n * n : : val isGreaterThan5 = (n: Int) => n > 5 : : val result = xs.map(square).filter(isGreaterThan5).length : : // 上面那行和下面這行等價 : : // val result = xs.map(n => n * n).filter(n => n > 5).length : : println (result1) : : ====================================================================== : 最近在學 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) -- And in that line now was a whiskered old man, with a linen cap and a crooked nose, who waited in a place called the Stardust Band Shell to share his part of the secret of heaven: that each affects the other and the other affects the next, and the world is full of stories, but the stories are all one. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 128.36.232.45 ※ 編輯: scwg 來自: 128.36.232.45 (07/12 11:29)

07/12 11:29, , 1F
Fix "flip ($)" which was written as "flip (.)"
07/12 11:29, 1F
文章代碼(AID): #1E6d8qoo (PLT)
討論串 (同標題文章)
文章代碼(AID): #1E6d8qoo (PLT)