Trying this in Prelude
Prelude> (i, j) = (3, 4)
Prelude> (i, j) = (j, i)
Prelude> i
I was expecting 4, but I get an infinite loop
Why ?
How to do this in a short elegant way ?
If you define
ghci> factorial n = if n == 0 then 1 else n * (factorial (n - 1))
you expect the use of factorial on the RHS to be the same factorial as the one on the LHS, right?
Well in the same way, the following defines i and j in terms of themselves:
ghci> (i,j) = (j,i)
The shortest sequence I can think of is this:
(i', j') = (i, j)
(i, j) = (j', i')
This is of course only valid for GHCi, just like the original sequence. In a normal Haskell module such definitions cannot occur in the same lexical scope.
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