[英]For Haskell type signature, like babel :: a -> b -> c, is c the return type?
[英]How to use types like this: type a = b -> c in haskell
我想創建一個存儲變量名稱和值的類型,所以我這樣做:
type Variable = String
type Val = Int
type Store = Variable -> Val
現在,我該如何使用這個商店?
) that returns value of a variable according to its or a function( ) to initial all the variables( assign a default value,like 0): 例如,我想編寫一個函數( ),它根據或函數( )返回一個變量的值,以初始化所有變量(分配一個默認值,如0):
fetch:: Store -> Variable -> Val
initial:: Store
我怎樣才能做到這一點?
您的Store
類型只是特定類型功能的別名,我可以寫一個
constStore :: Store
constStore _ = 1
你可以做一個更復雜的:
lenStore :: Store
lenStore var = length var
-- or
-- lenStore = length
然后fetch
就是函數應用程序
fetch :: Store -> Variable -> Val
fetch store var = store var
商店是功能,因此您可以將商店應用於變量:
fetch :: Store -> Variable -> Val
所以
fetch :: (Variable -> Val) -> Variable -> Val
從而
fetch store var = store var
但那會更簡單
fetch = id
或完全省略,所以如果myStore :: Store
,我可以做到
myStore "myVariable"
我會得到適當的價值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.