sml - ML. How to correctly work with sequences -
so have "new" 2 way sequence:
datatype direction = | forward; datatype 'a bseq = bnil | bcons of 'a * (direction -> 'a bseq);
and need function seq2bseq : 'a seq -> 'a seq -> 'a bseq "appends" 2 regular sequences 1 so:
if sequp 0 1 2 3 4 ... , seqdown -1 -2 -3 -4 ... seq2bseq create .. -4 -3 -2 -1 0 1 2 3 4 .. . in other words starter element first of sequp(0) if move first element of seqdown(-1) , 2 second of sequp(1) if move forward. far wrote following:
fun appendq (nil, yq) = yq | appendq (cons(x,xf), yq) = cons(x,fn()=>appendq(xf(),yq)); fun seq2bseq (downseq) (upseq) = bcons(head(upseq), fn (forward) => seq2bseq appendq(head(upseq), downseq) tail(upseq) | (back) => seq2bseq tail(downseq) appendq(head(downseq), upseq) );
for following errors:
stdin:28.101-28.153 error: operator , operand don't agree [tycon mismatch] operator domain: 'z seq * 'z seq -> 'z seq operand: 'y seq -> 'y seq in expression: seq2bseq tail stdin:27.5-28.155 error: right-hand-side of clause doesn't agree function result type [tycon mismatch] expression: _ seq -> _ bseq result type: _ * _ -> ('z seq -> 'z seq) -> _ seq -> _ in declaration: seq2bseq = (fn arg => (fn <pat> => <exp>))
i can't figure out what's wrong(:/). help! thanks!
edit: working(?) code at: http://www.beetxt.com/mkx/.
your type errors appear coming lack of parenthesization.
if have functions foo
, bar
, , want call foo
on result of calling bar
on value baz
, need write foo (bar baz)
or foo (bar (baz))
, if prefer.
writing foo bar(baz)
cause foo
called argument bar
, not type-check.
try:
fun seq2bseq (downseq) (upseq) = bcons(head(upseq), fn forward => seq2bseq (appendq(head(upseq), downseq)) (tail(upseq)) | => seq2bseq (tail(downseq)) (appendq(head(downseq), upseq)) )
Comments
Post a Comment