【发布时间】:2016-06-09 21:24:30
【问题描述】:
我正在尝试编写一个有两个参数的 SML 函数,第一个是
int,第二个是列表列表。目标是将第一个参数插入到第二个参数中每个列表的前面。例如,append_to_front(1,[[3,4],[6,8],[]]) 应该返回 [[1,3,4],[1,6,8],[1]]。
我有代码:
fun append_to_front(a:int, L:int list list) =
if L = []
then []
else a::hd(L)::append_to_front(a, tl(L));
我收到错误消息:错误:运算符和操作数不同意 [tycon mismatch]。为什么?
【问题讨论】:
-
你也可以用高阶函数解决这个问题:
fun append_to_front (x, L) = map (fn xs => x::xs) L
标签: sml