Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does State need a value?

Tags:

haskell

monads

Just learning about State monad from this excellent tutorial. However, when I tried to explain it to a non-programmer they had a question that stumped me.

If the purpose of the State is to simulate mutable memory, why is the function that state monad stores is of the type:

s -> (a, s)

and not simply:

s -> s

In other words, what is the need for the "intermediate" value? For example, couldn't we, in the cases where we need it, simulate it by simply defining a state as a tuple of (state, value)?

I'm sure I confused something, any help is appreciated.

like image 666
Andriy Drozdyuk Avatar asked Jul 20 '12 16:07

Andriy Drozdyuk


People also ask

What is the value of the state?

Schmitt argues that the value of the state consists in the realization of law in the world, while the significance of the individual is that of fulfilling the roles that the state assigns and ascribes to them for the completion of the state's task of realizing law and right.

What are the need states?

A need state is something that motivates a consumer to purchase; it is the primary purpose for which a consumer is purchasing a product or solution – the driver.

What is a need state analysis?

One of the more entrenched paradigms in food innovation remains the concept of the “need state.” A need state is essentially a benefit linked to some kind of behavioral moment that researchers can locate in surveys and then put into classificatory framework.

What is an unmet consumer need?

Unmet needs are opportunities that can spell differentiation in a crowded marketplace. It may take some effort to uncover what these unmet needs are, but as soon as you do, it positions you front and center and exactly where your customers are looking. This is how brands become disrupters to create growth and success.

Why is it important to know your values?

We are not always aware of our values, but knowing what they are can help you more easily make decisions that are right for you, such as taking the job that has good opportunities for variety, change and spontaneity or good opportunities for security and tenure.

Why do we need a state?

Above all, we need a state in order to have democracy, which is the system by which the many non-rich join together to overcome the power of the rich and thus deny them control over society. It's not that I implicitly "trust" the state; that question is a side issue.

What is the purpose of values ​​taking decisions?

That purpose is the satisfaction of collective or individual needs. When using the values ​​for take decisions , A deliberate decision is made to focus on things that are important to each other. When values ​​are shared, they build internal cohesion within a group.

Why is it important to have a value structure?

They make society and people work better. A society with a good value structure makes people better able to relate. Ideally, each value means the same for each individual. All the individuals of a society must share the same basic values ​​in order to build a good culture and a good society.


2 Answers

To draw a parallel with an imperative language like C, s -> s corresponds to a function with the return type void, which is invoked purely for side effects (such as mutating the memory). It is isomorphic to State s ().

And indeed, it is possible to write C functions which communicate only through global variables. But, as in C, it is often convenient to return values from functions. That's what a is for.

Of course it's possible that for your particular problem s -> s is a better choice. Although it's not a Monad, it is a Monoid (when wrapped in Endo). So you can construct such functions using <> and mempty, which correspond to >>= and return of Monad.

like image 149
Roman Cheplyaka Avatar answered Oct 05 '22 22:10

Roman Cheplyaka


To expand a bit on Nick's answer:

s is the state. If all your functions were s -> s (state to state), your functions would not be able to return any values. You could define your state as (the actual state, value returned), but that conflates the state with the value the state-ful functions are computing. And it's also the common case that you'll want functions to actually compute and return values...

like image 29
Claudiu Avatar answered Oct 05 '22 20:10

Claudiu