1

How can i generate the next rational number into 2 integer variables. For example, (1,1) (2,1) (1,2) (1,3) (3,1) .. I have the algorithm to generate it :

if(n % 2 == d % 2)
{  
  n++;
  if(d > 1) d--;
}
else{
  d++;
  if(n > 1) n--;
}

The problem is how to build a dynamic stream inAgdaa. I will start from (1,1) and then generate the next pair and add it to stream. Please anyone help.

ajayv
  • 641
  • 6
  • 21

1 Answers1

3

There is the iterate function just like in Haskell:

open import Relation.Nullary.Decidable
open import Data.Bool using (if_then_else_)
open import Data.Nat
open import Data.Product
open import Data.Stream

next : ℕ × ℕ -> ℕ × ℕ
next (n , m) = if ⌊ n ≟ 1 ⌋ then m + 1 , 1 else pred n , suc m

stream : Stream (ℕ × ℕ)
stream = iterate next (1 , 1)

open import Relation.Binary.PropositionalEquality
open import Data.Vec hiding (take)

test : take 10 stream ≡ (1 , 1) ∷ (2 , 1) ∷ (1 , 2) ∷ (3 , 1) ∷ (2 , 2) ∷ (1 , 3)
                                ∷ (4 , 1) ∷ (3 , 2) ∷ (2 , 3) ∷ (1 , 4) ∷ []
test = refl
effectfully
  • 12,325
  • 2
  • 17
  • 40
  • do you have any idea how to access the individual element?? http://stackoverflow.com/questions/29369643/accessing-element-from-stream-in-agda – ajayv Mar 31 '15 at 13:37