문제

I'm trying to add an int list list with another int list list using the append function, but I can't get it to work the way I want.

Say that I want to append [[1,2,3,4,5]] with [6,7] so that I get [[1,2,3,4,5,6,7]].

Here's my attempt: [1,2,3,4,5]::[]@[6,7]::[], but it just gives me the list I want to append as a list of its own instead of the two lists combined into one, like this: [[1,2,3,4,5],[6,7]].

How can I re-write the operation to make it return [[1,2,3,4,5,6,7]]?

도움이 되었습니까?

해결책

Your question is too unspecific. You are dealing with nested lists. Do you want to append the second list to every inner list of the nested list, or only the first one? Your example doesn't tell.

For the former:

fun appendAll xss ys = List.map (fn xs => xs @ ys) xss

For the latter:

fun appendHd [] ys = raise Empty
  | appendHd (xs::xss) ys = (xs @ ys)::xss

However, both of these should rarely be needed, and I somehow feel that you are trying to solve the wrong problem if you end up there.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top