1樓:Belleve
fix$(
<$>)<$>(:
)<*>
((<$>((:
[])<$>
))(=<<
)<$>(*
)<$>
(>>=)(+
)($))$1
2樓:Lee
對我來說 Aeson + Generics 就是黑魔法之一:
例子來自 What I Wish I Knew When Learning Haskell 2.3 ( Stephen Diehl )
import
Data.Text
import
Data.Aeson
import
GHC.Generics
import
qualified
Data.ByteString.Lazy
asBL
data
Refs
=Refs
deriving
(Show
,Generic
,FromJSON
,ToJSON
)data
Data
=Data
deriving
(Show
,Generic
,FromJSON
,ToJSON
)main
::IO
()main=do
contents
<-BL.
readFile
"example.json"
letJust
dat=
decode
contents
$name
datprint$a
(refs
dat)BL.
putStrLn
$encode
dat測試的 example.json 如下:}
3樓:Skitter
神奇的let:
> let 2 = 1 in 2
2一行求冪集:
> filterM (const [True, False]) [1, 2, 3, 4]
[[1,2,3,4],[1,2,3],[1,2,4],[1,2],[1,3,4],[1,3],[1,4],[1],[2,3,4],[2,3],[2,4],[2],[3,4],[3],[4],]
型別安全的強制型別轉換:
class Cast t u where cast :: t -> u
instance Cast Int Int where cast = id
instance Cast Int Double where cast = fromIntegral
instance (Cast u1 t1, Cast t2 u2) => Cast (t1 -> t2) (u1 -> u2) where cast f = \x -> cast $ f $ cast x
instance (Cast t1 u1, Cast t2 u2) => Cast (t1, t2) (u1, u2) where cast (x, y) = (cast x, cast y)
test :: Double -> (Double, Double) -> Int
test x (y, z) = round $ x * y + x * z
> cast test (5 :: Int) (1 :: Int, 2 :: Int) :: Double
15.0
真○OO虛函式,誰說type class只能靜態過載的?
data Exists f where Ex :: f t -> Exists f
data ShowableObject t where ShowableObject :: Show t => t -> ShowableObject t
mkShowableObject :: Show t => t -> Exists ShowableObject
mkShowableObject = Ex . ShowableObject
instance Show (Exists ShowableObject) where show (Ex (ShowableObject x)) = show x
test = [mkShowableObject 1, mkShowableObject "abc", mkShowableObject ('a', True, [0..5])]
> show test
"[1,\"abc\",('a',True,[0,1,2,3,4,5])]"
Haskell 有哪些威力十足的庫?
活動小丑 基本上可以將任何recursive strucutre視覺化。putStr drawTree fmap show Node1 Node2 Node3 1 2 3putStr drawForest map fmap show Node1 Node2 Node3 Node10 Node20 1...
大家倒時差有什麼神乎其技的技巧可以分享下嗎?
航旅縱橫 當你走進候機廳,看著停機坪上大大小小的飛機,一定會想 飛機都停在這裡了,為什麼不讓我們上飛機?當我候機時,機長空姐都在做什麼呢?下面小橫就來為你揭秘!機組乘務人員的健康評估就像汽車不能酒駕一樣,開飛機這件事兒更是馬虎不得。比如飛行前飛行員和乘務人員是否發燒 是否飲酒等等,甚至情緒問題也都必...
Haskell 的 Graph reduction 在現實機器中是怎樣實現的?
neo lin 那就來看我渣優秀員工十年前 2007 的formal proof 吧。我也是才知道的 MaskRay Implementing Functional Languages a tutorial 實現過兩章 template instantiation和Three Instruction...