Navigation
Synopsis add an element in front of a list
Syntax Exp1 + Exp2
Types
Exp1 Exp2 Exp1 + Exp2
T1 list[T2] list[lub(T1,T2)]
Description The + operator can insert an element in front of a list. Note that + is one of the Operators that is overloaded, it is also List/Concatenation and List/Append for example.
Examples
rascal>1 + []
list[int]: [1]
rascal>1 + [2]
list[int]: [1,2]
rascal>1 + [2,3]
list[int]: [1,2,3]
Pitfalls
  • If the first operand before the + is a list, + acts as List/Concatenation and not as List/Insert

    This is concatenation:
    rascal>[1] + [2]
    list[int]: [1,2]
    
    To insert a list as an element, use extra brackets:
    rascal>[[1]] + [2]
    list[value]: [
      [1],
      2
    ]
    

Questions
Question [1]. When you insert an element in a list, the number of elements in the result is always:




Question [2].
The type of "W" + ["Pepper", "Rosemary", "Fig", "Kiwifruit"] is

Question [3].
"Joh Yowza" + ["Strawberry", "Telly Monster"] == 



Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.