I am trying to create a generic sequence, that would behave the following:
val generic_sequence= fn : (int -> int) -> int seq
that is, It should receive as an input a function:
foo: int -> int
and create a sequence that activates foo on all natural numbers.
I wrote the following auxiliary code (works fine):
datatype 'a seq = Nil
| Cons of 'a * (unit-> 'a seq);
fun head (Cons(x,_)) = x;
fun tail (Cons (_,xf)) = xf();
fun naturals k = Cons(k,fn()=>naturals (k+1));
and when I tried implementing the generic sequence I got stuck. This is where I've got.
fun aux (Cons(x,xf))= (Cons(foo x,(fn=>aux((xf())))));
fun generic_seq foo = (aux (from 0));
I have 2 problems:
It doesn't compile
I am not sure if my approach is correct
Would appreciate some help here.