multi-directional infinte sequence - ML -
i want use datatype sequence defined follows:
datatype 'a seq = nil | cons of 'a * (unit-> 'a seq); exception emptyseq; fun head(cons(x,_)) = x | head nil = raise emptyseq; fun tail(cons(_,xf)) = xf() | tail nil = raise emptyseq;
which has option iterate on functions backward , forward:
datatype direction = | forward; datatype 'a bseq = bnil | bcons of 'a * (direction -> 'a bseq);
and defined well:
fun bhead(bcons(x,_)) = x | bhead bnil = raise emptyseq; fun bforward(bcons(_,xf)) = xf(forward) | bforward bnil = raise emptyseq; fun bback(bcons(_,xf)) = xf(back) | bback bnil = raise emptyseq;
now, i'm trying create function "create_seq" gets int "k" , returns infinte sequence can iterated , forth. example:
- create_seq 2; val = bcons (2,fn) : int bseq - bforward it; val = bcons (3,fn) : int bseq - bforward it; val = bcons (4,fn) : int bseq - bback it; val = bcons (3,fn) : int bseq - bback it; val = bcons (2,fn) : int bseq - bback it; val = bcons (1,fn) : int bseq - bback it; val = bcons (0,fn) : int bseq - bback it; val = bcons (~1,fn) : int bseq
this i've been trying , can't figure out why doesn't work:
fun create_seq k = (k,fun check forward = create_seq(k+1) | check = create_seq(k-1));
nor this:
fun create_seq k = (k,fn x => case x of forward => create_seq(k+1) | => create_seq(k-1));
or this:
fun create_seq k = (k,fn forward => create_seq(k+1) | => create_seq(k-1));
it seems forgot constructor:
fun intbseq(k:int) = bcons(k,fn forward => intbseq(k+1)| => intbseq(k-1));
this should work.
Comments
Post a Comment