当前位置: 动力学知识库 > 问答 > 编程问答 >

SML - Recursion of inserting a value and creating a list in a list

问题描述:

I'm trying to do the following:

fun help(L)=if null(L) then nil else hd L::help(tl L);

fun inseach(a,L)=if null(L) then [[a]] else (a::L)::help(inseach(a,tl L));

inseach(4,[1,2,3]);

It needs to return: [[4,1,2,3],[1,4,2,3],[1,2,4,3],[1,2,3,4]]

What I'm getting is:

val help = fn : 'a list -> 'a list

val inseach = fn : 'a * 'a list -> 'a list list

val it = [[4,1,2,3],[4,2,3],[4,3],[4]] : int list list

I want to change the command:

help(inseach(a,tl L))

to:

help((hd L)::inseach(a,tl L))

So it appends the head to each list I'm building, but I get errors every time. I can't figure this out. I'm sure there is something simple and obvious I'm missing. I've had 15 of these things and this is the last one to solve.

分享给朋友:
您可能感兴趣的文章:
随机阅读: