2

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:

  1. It doesn't compile

  2. I am not sure if my approach is correct

Would appreciate some help here.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
genericname
  • 167
  • 3
  • 11

1 Answers1

3

Ok, I figured it out,

I created a mapq function and it basically did everything for me.

 fun mapq f Nil = Nil 
   | mapq f (Cons (x,xf)) = Cons (f(x), fn() => mapq f (xf()));
sshine
  • 15,635
  • 1
  • 41
  • 66
genericname
  • 167
  • 3
  • 11