Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use types like this: type a = b -> c in haskell

Tags:

types

haskell

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?

like image 498
DMST Avatar asked Jan 13 '14 17:01

DMST


People also ask

What is -> in Haskell?

(->) 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.

How are types used in Haskell?

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.

How do you declare types in Haskell?

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.

How do you read type in Haskell?

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.


1 Answers

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
like image 145
bheklilr Avatar answered Nov 01 '22 13:11

bheklilr