I want to create a type that stores name and value of a variable,so I did this:
type Variable = String
type Val = Int
type Store = Variable -> Val
now, how can I use this Store?
for example I want to write a function(fetch) that returns value of a variable according to its Store or a function(initial) to initial all the variables( assign a default value,like 0):
fetch:: Store -> Variable -> Val
initial:: Store
how can I do this?
(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.
In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time. To learn more about the Type, we will use the ":t" command.
Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.
In Haskell read function is used to deal with strings, if you want to parse any string to any particular type than we can use read function from the Haskell programming. In Haskell read function is the in built function, that means we do not require to include or install any dependency for it, we can use it directly.
Your Store
type is just an alias for a specific kind of function, I could write one as
constStore :: Store
constStore _ = 1
You could make a more complex one:
lenStore :: Store
lenStore var = length var
-- or
-- lenStore = length
Then fetch
is just function application
fetch :: Store -> Variable -> Val
fetch store var = store var
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With