Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple declaration goes to infinite loop

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 ?

like image 545
Santa Claus Avatar asked Dec 21 '25 14:12

Santa Claus


2 Answers

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)
like image 169
Alec Avatar answered Dec 24 '25 10:12

Alec


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.

like image 32
n. 1.8e9-where's-my-share m. Avatar answered Dec 24 '25 11:12

n. 1.8e9-where's-my-share m.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!