I don't know how to re-assign a variable in a function.
For example,
elephant = 0
function x = elephant = x
Why doesn't this work?
I don't know how to re-assign a variable in a function.
For example,
elephant = 0
function x = elephant = x
Why doesn't this work?
Haskell is a great imperative language, and writing programs that can re-assign state is a really fun advanced topic! This is definitely not the approach you want right now, but come back to it some day 🙂
It takes a bit of effort to define an environment that models global mutable variables. Once you get the hang of it, though, the precision of the types ends up being pretty handy.
We're going to be using the lens and mtl libraries.
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Control.Monad.State
I'll stick with using integers as your question does, but we'll throw in a type alias to remind ourselves that they are being used as the type of the elephant
variable.
type Elephant = Integer
You wanted a program whose global mutable state has an elephant. So first let's define what it means to have an elephant. Lens
captures this notion nicely.
class HasElephant a
where
elephant :: Lens' a Elephant
Now we can define function
, which assigns a new value to elephant
.
function :: (MonadState s m, HasElephant s) => Elephant -> m ()
function x =
elephant .= x
The constraints MonadState s m
and HasElephant s
are saying our program must be able to hold mutable state of some type s
, and the type s
must have an elephant.
Let's also define a program that prints the elephant.
printElephant :: (MonadState s m, HasElephant s, MonadIO m) => m ()
printElephant =
use elephant >>= (liftIO . print)
This program does I/O (printing), so we have an additional constraint MonadIO m
that says our program type m
must be able to do I/O.
The elephant
variable is probably only one part of some larger program state. Let's define a data type here to represent the entire state (which we'll name Congo just to be cute because the Congo Basin is one place where elephants live).
data Congo = Congo
{ _congoElephant :: Elephant
}
makeLenses ''Congo
(See Control.Lens.TH for a little bit about makeLenses
does here using Template Haskell.)
We must define the way in which the Congo
has an elephant.
instance HasElephant Congo
where
elephant = congoElephant
Now we can write an example program. Our program will print the value of elephant
, then change the value of elephant
, then print it again.
main' :: StateT Congo IO ()
main' =
do
printElephant
function 2
printElephant
Then we can run this program.
main :: IO ()
main = Congo 0 & runStateT main' & void
The output is:
0
2
StateT
, monad transformers, type classes and lenses... If someone is unaware of this aspect of Haskell (not having assignment in the way a language like C does) and haven't previously come across this difference, I suspect they don't yet have the background for this and it might even scare them off (I could be wrong, of course, but I have a pretty strong suspicion...). –
Vardhamana {-# LANGUAGE TemplateHaskell #-}
? –
Soper im trying to re-assign an existing variable
You can't do that in Haskell. You can do something close by using IORef
s, but this is very rarely the proper solution to a problem - certainly not in situations a beginner might encounter.
Instead you should re-design your program logic, so that it does not require mutable variables to function.
Haskell is a leader in the functional programming world and functional programming is often called "programming without assignment." It's almost the entire point of functional programming to not use assignment. As soon as you've used it, you're not really doing it in a "functional" way any more. Of course there are times for it, but FP tries to minimize those times.
So, to answer your question, "Why doesn't this work?" First of all the syntax is not correct. =
does not mean assignment in Haskell. It binds a name to an expression. You cannot do that twice (in the same scope). In other words, "variables" are immutable (like in math). Second, mutation is a side-effecting action and Haskell treats those as impure actions which must be done in the IO
world.
I could show you how to actually mutate a reference in Haskell, but I don't think that's what you need at this point.
The most primitive way to bind a variable x
to a value v
is to write a function taking x
as argument, and pass v
to that function.
This can sometimes be used to "simulate" the effect of a mutable variable.
E.g., the imperative code
// sum 0..100
i = s = 0;
while (i <= 100) {
s = s+i;
i++;
}
return s;
becomes
final_s = f 0 0 -- the initial values
where
f i s | i <=100 = f (i+1) (s+i) // increment i, augment s
| otherwise = s // return s at the end
The above code is not pretty FP code, but at least it is close enough to imperative code to make it possible to spot the connections.
A final digression:
When one first notices this, it is usually lured to fall into the Blub paradox. One could easily think: "What!? Haskell needs all that stuff to simulate a simple assignment? If in language Blub assignment is trivial, and simulating that in Haskell requires so much effort, then clearly Blub is much better than Haskell!". And this would be a perfect case of the Blub paradox: when a Blub programmer moves to another language, they immediately perceive what can not be directly translated from Blub, and do not notice all the other features of the new language which were not present in Blub. Their mind now thinks in "Blub", and it requires a great effort to adapt to new models.
Almost as paradoxically, learning both FP and imperative programming is useful precisely because it's non trivial to learn the other paradigm when used to only one of those. If the step between them were narrow, it would not be worth the effort to learn two close approaches to the same problem.
In general this doesn't work because you usually make immutable declarations, rather than specifying a sequence of operations. You can do:
elephant = 3
main = print elephant
But you can also do:
main = print elephant
elephant = 3
Because the code doesn't specify an order of execution, there is no way to interpret multiple assignments as anything other than an error.
If you want to specify a sequence of operations, use do notation:
main = do
let elephant = 0
print elephant
let elephant = 1
print elephant
let elephant = 2
print elephant
The code in a do block is executed in order, so you can effectively reassign variables the way you can in most programming languages.
Note that this code really just creates a new binding for elephant. The old value still exists:
main = do
let elephant = 1
print elephant
let printElephant = print elephant
let elephant = 2
print elephant
printElephant
Because the printElephant function I define is still using the old value of elephant, this prints:
1
2
1
let elephant = 1
and let elephant = 2
do not re-assign the variable elephant
. Instead, they just introduce a new variable, which happens to also be called elephant
. The print elephant
then picks the nearest previous occurence (more precisely speaking: the one in the narrowest shared scope), but outside of this scope the old elephant
always keeps the same value. I recommend this should not be done in actual Haskell code (in fact ghc -Wall
warns about it). –
Lissotrichous Using the same example as @Chi, you can use the State
monad to simulate an imperative loop with recursion:
C code:
// sum 0..100
i = s = 0;
while (i <= 100) {
s = s+i;
i++;
}
return s;
Haskell code:
import Control.Monad.State
final_s :: Int
final_s = evalState sum_loop (0, 0) -- evaluate loop with initial state (0, 0)
sum_loop :: State (Int, Int) Int
sum_loop = do
(i, s) <- get -- retrieve current state
if i <= 100 -- check loop condition
then do -- if condition is true:
let new_s = s + i
let new_i = i + 1
put (new_i, new_s) -- update state with new tuple
sum_loop -- recursively call loop with new state, simulate iteration with recursion
else
return s -- if condition is false, return s as final result
main = print final_s
As you can see this is quite similar to the C code, we just have 3 more lines:
(i, s) <- get
to get the current state.put (new_i, new_s)
to update the current state with the new statesum_loop
to recursively call loop with new state, simulating iteration with recursionYou can add debug only printing with put $ traceShowId (new_i, new_s)
instead of put (new_i, new_s)
, but you should only use this for debugging because it cheats the type system.
So a few things more things have to handled "manually" but it is possible to write reasonably readable imperative code in Haskell.
© 2022 - 2024 — McMap. All rights reserved.
elephant
variable. – Haig