ListInsert

Inserts an element at a position in a list.

声明

语法

ListInsert(expr a, expr pos, expr ele)

参数

Argument

描述

a

the list.

pos

the index to insert at.

ele

the element to insert.

返回值

无。

描述

Everything from pos onwards moves up by one. Inserting at the length of the list appends to it.

示例

(ListInsert
  :a (Var
    :v (SymbolRef 1 "a")
  )
  :pos (IntegerConstant
    :n 0
    :type (Integer
      :kind 4
    )
    :intboz_type :Decimal
  )
  :ele (IntegerConstant
    :n 0
    :type (Integer
      :kind 4
    )
    :intboz_type :Decimal
  )
)

It comes from this complete ASR text document:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "main" (Program
        :symtab (SymbolTable
          :id 1
          :symbols {
            "a" (Variable
              :parent_symtab 1
              :name "a"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (List
                :type (Integer
                  :kind 4
                )
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
              :common_block nil
            )
            "b" (Variable
              :parent_symtab 1
              :name "b"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (List
                :type (Integer
                  :kind 4
                )
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
              :common_block nil
            )
            "i" (Variable
              :parent_symtab 1
              :name "i"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Integer
                :kind 4
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
              :common_block nil
            )
            "p" (Variable
              :parent_symtab 1
              :name "p"
              :dependencies []
              :intent :Local
              :symbolic_value nil
              :value nil
              :storage :Default
              :type (Logical
                :kind 4
              )
              :type_declaration nil
              :abi :Source
              :access :Public
              :presence :Required
              :value_attr false
              :target_attr false
              :contiguous_attr false
              :bindc_name nil
              :is_volatile false
              :is_protected false
              :pass_attr :NotMethod
              :self_argument nil
              :codims []
              :common_block nil
            )
          }
        )
        :name "main"
        :dependencies []
        :body [
          (Assignment
            :target (Var
              :v (SymbolRef 1 "a")
            )
            :value (ListConstant
              :args [
                (IntegerConstant
                  :n 1
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
                (IntegerConstant
                  :n 2
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
                (IntegerConstant
                  :n 3
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
              ]
              :type (List
                :type (Integer
                  :kind 4
                )
              )
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "i")
            )
            :value (ListLen
              :arg (Var
                :v (SymbolRef 1 "a")
              )
              :type (Integer
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "b")
            )
            :value (ListConcat
              :left (Var
                :v (SymbolRef 1 "a")
              )
              :right (Var
                :v (SymbolRef 1 "b")
              )
              :type (List
                :type (Integer
                  :kind 4
                )
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "b")
            )
            :value (ListRepeat
              :left (Var
                :v (SymbolRef 1 "a")
              )
              :right (IntegerConstant
                :n 2
                :type (Integer
                  :kind 4
                )
                :intboz_type :Decimal
              )
              :type (List
                :type (Integer
                  :kind 4
                )
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "p")
            )
            :value (ListCompare
              :left (Var
                :v (SymbolRef 1 "a")
              )
              :op :Eq
              :right (Var
                :v (SymbolRef 1 "b")
              )
              :type (Logical
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "i")
            )
            :value (ListCount
              :arg (Var
                :v (SymbolRef 1 "a")
              )
              :ele (IntegerConstant
                :n 1
                :type (Integer
                  :kind 4
                )
                :intboz_type :Decimal
              )
              :type (Integer
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "p")
            )
            :value (ListContains
              :left (Var
                :v (SymbolRef 1 "a")
              )
              :right (IntegerConstant
                :n 2
                :type (Integer
                  :kind 4
                )
                :intboz_type :Decimal
              )
              :type (Logical
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "i")
            )
            :value (ListItem
              :a (Var
                :v (SymbolRef 1 "a")
              )
              :pos (IntegerConstant
                :n 0
                :type (Integer
                  :kind 4
                )
                :intboz_type :Decimal
              )
              :type (Integer
                :kind 4
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (Assignment
            :target (Var
              :v (SymbolRef 1 "b")
            )
            :value (ListSection
              :a (Var
                :v (SymbolRef 1 "a")
              )
              :section (array_index
                :left (IntegerConstant
                  :n 0
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
                :right (IntegerConstant
                  :n 2
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
                :step (IntegerConstant
                  :n 1
                  :type (Integer
                    :kind 4
                  )
                  :intboz_type :Decimal
                )
              )
              :type (List
                :type (Integer
                  :kind 4
                )
              )
              :value nil
            )
            :overloaded nil
            :realloc_lhs false
            :move_allocation false
            :new_target false
            :new_allocation false
          )
          (ListAppend
            :a (Var
              :v (SymbolRef 1 "a")
            )
            :ele (IntegerConstant
              :n 4
              :type (Integer
                :kind 4
              )
              :intboz_type :Decimal
            )
          )
          (ListInsert
            :a (Var
              :v (SymbolRef 1 "a")
            )
            :pos (IntegerConstant
              :n 0
              :type (Integer
                :kind 4
              )
              :intboz_type :Decimal
            )
            :ele (IntegerConstant
              :n 0
              :type (Integer
                :kind 4
              )
              :intboz_type :Decimal
            )
          )
          (ListRemove
            :a (Var
              :v (SymbolRef 1 "a")
            )
            :ele (IntegerConstant
              :n 1
              :type (Integer
                :kind 4
              )
              :intboz_type :Decimal
            )
          )
          (ListClear
            :a (Var
              :v (SymbolRef 1 "a")
            )
          )
        ]
        :global_init nil
      )
    }
  )
  :items []
  :global_init nil
)

也可以看看

ListAppend, ListRemove, List