for example: when I want to concat a list of int: [1, 2, 3] with x my code is:
[1, 2, 3] @ [x]
but in another case I have a function that take a list is an argument,
val foo : int list
I have another function print_foo taking foo is an argument and a return type is
val print_foo : int list list
I write a function: print_foo [foo]
My question is that: [foo] in this case is accepted by type checking but is it correct in
the logical meaning? what is a good way to write this function? It is a same question for [[x]]
Could you please explain for me more about how and when I can use [x] or [[x]], etc…?
Thank you very much,
[x]means a list containing one element:x.[[x]]means a list containing one element: A list containing one element:x.So, if you say
xis4, then[x] = [4]is the list containing only a4, and[[x]] = [[4]]is the list containing only a list containing only a 4.As you see,
[x]simply putsxinto a list by itself. Ifx = [1, 2, 3](that is,xis a list itself), then you probably don’t want to call a function with[x] (= [[1, 2, 3]]), as you then give it a list containing your original list, rather than the list itself. Of course, this can be perfectly legitimate and needed in certain cases, but if you’re unsure, it’s most likely not needed.So, if you have that
foois anint list, and you then callprint_foo [foo], you’re saying: “Print this list containing the list I want to print.” What you probably want to say isprint_foo foo, where you leave out the redundant list wrapper. This could be interpreted as “Print this list I want to print.”