Search code examples
functional-programmingsmlsmlnjcons

Interpreting []::[], []::[]::[] in sml


I would like to ask how []::[] and []::[]::[] are interpreted conceptually in SML/NJ.

My thought :

I thought that []::[] would generates error or generates a empty list but in fact it generates val it = [[]] : 'a list list.

After I know that []::[] generates [[]] : 'a list list, I thought that []::[]::[] would generate [[[]]] : 'a list list list but in fact it generates [[],[]] : 'a list list


Solution

  • :: is right-associative: In a::b::c::[] a,b,c must be elements of the same type and

    a::b::c::[] = a::(b::(c::[]))
                = a::(b::[c])
                = a::[b,c]
                = [a,b,c]
    

    This remains true even if a,b,c are []:

    []::[]::[]::[] = [[],[],[]]