ML。如何正确使用序列

时间:2014-05-29 23:25:39

标签: sml ml sequences

所以我有这个“新的”双向序列:

datatype direction = Back | Forward; 
datatype 'a bseq =   bNil 
                | bCons of 'a * (direction -> 'a bseq); 

我需要一个函数seq2bseq:'seq - > 'seq - > 'bseq“将”两个常规序列“附加”到一个如此:

如果seqUp为0 1 2 3 4 ...并且seqDown为-1 -2 -3 -4 ...那么seq2bseq将创建.. -4 -3 -2 -1 0 1 2 3 4 ..换句话说,起始元素是seqUp(0)中的第一个,所以如果我向后移动,我将到达seqDown(-1)的第一个元素,如果我向前移动则到达seqUp(1)的第二个元素。 到目前为止,我写了以下内容:

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) );

我得到以下错误:

stdIn:28.101-28.153 Error: operator and 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 with function result type [tycon mismatch]
  expression:  _ seq -> _ bseq
  result type:  _ * _ -> ('Z seq -> 'Z seq) -> _ seq -> _
  in declaration:
    seq2bseq = (fn arg => (fn <pat> => <exp>))

我无法弄清楚出了什么问题(:/)。救命! 谢谢!

编辑:工作(?)代码:http://www.beetxt.com/mkX/

1 个答案:

答案 0 :(得分:2)

您的类型错误似乎来自缺乏括号。

如果您有foobar功能,并且希望就价值foo调用bar的结果致电baz,那么您需要如果您愿意,可以将其写为foo (bar baz)foo (bar (baz))

编写foo bar(baz)将导致使用参数foo调用bar,这可能不会进行类型检查。

尝试:

fun seq2bseq (DownSeq) (UpSeq) =    
    bCons(head(UpSeq),
          fn Forward => seq2bseq (appendq(head(UpSeq), DownSeq)) (tail(UpSeq)) 
           | Back => seq2bseq (tail(DownSeq)) (appendq(head(DownSeq), UpSeq))
         )