# 6장: 목록, List

### 1. list 명령어

list는 주어진 인자들을 하나의 리스트로 만듭니다.

#### 1.1 기본 구문

```tcl
list 인자1 인자2 ...
```

#### 1.2 사용 예시

```tcl
set fruits [list apple banana cherry]
puts $fruits  # 출력: apple banana cherry

set mixed [list "Hello World" 42 3.14 {nested list}]
puts $mixed  # 출력: {Hello World} 42 3.14 {nested list}
```

#### 1.3 주요 특징

* 공백이나 특수 문자를 포함한 인자도 하나의 요소로 처리합니다.
* 빈 리스트를 만들 때도 사용할 수 있습니다: `set empty_var [list]`

### 2. lappend 명령어

lappend는 기존 리스트 변수에 하나 이상의 요소를 추가합니다.

#### 2.1 기본 구문

```tcl
lappend 변수명 값1 값2 ...
```

#### 2.2 사용 예시

```tcl
set fruits {apple banana}
lappend fruits cherry orange
puts $fruits  # 출력: apple banana cherry orange

set numbers {}
lappend numbers 1 2 3
puts $numbers  # 출력: 1 2 3
```

#### 2.3 주요 특징

* 기존 변수의 값을 변경합니다.
* 변수가 존재하지 않으면 새로운 리스트를 생성합니다.
* 여러 요소를 한 번에 추가할 수 있습니다.

#### 2.4 팁 💡

* 리스트에 요소를 추가할 때 `lappend`를 사용하면 `concat`이나 `list`보다 효율적입니다.
* 루프 내에서 리스트를 구축할 때 특히 유용합니다.

### 3. concat 명령어

concat은 주어진 인자들을 연결하여 하나의 리스트로 만듭니다.

#### 3.1 기본 구문

```tcl
concat ?인자1 인자2 ...?
```

#### 3.2 사용 예시

```tcl
set list1 {1 2 3}
set list2 {4 5 6}
set combined [concat $list1 $list2]
puts $combined  # 출력: 1 2 3 4 5 6

set result [concat "Hello" {World} [list How are you?]]
puts $result  # 출력: Hello World {How are you?}
```

#### 3.3 주요 특징

* 리스트, 문자열, 숫자 등 다양한 타입의 인자를 연결할 수 있습니다.
* 리스트 인자의 가장 바깥쪽 중괄호를 제거하고 연결합니다.
* 결과는 항상 평면화된(flattened) 리스트입니다.

#### 3.4 팁 💡

* 여러 리스트를 하나로 합칠 때 유용합니다.
* `join`과 달리 구분자를 사용하지 않고 연결합니다.

### 4. 명령어 비교

* **list**: 각 인자를 개별 요소로 취급하여 리스트 생성
* **lappend**: 기존 리스트에 요소 추가 (변수 수정)
* **concat**: 인자들을 연결하여 새로운 리스트 생성 (평면화)

### 5. 복합 사용 예시

이 명령어들은 종종 함께 사용되어 복잡한 리스트 조작을 수행합니다:

```tcl
# 중첩 리스트 생성 및 조작
set fruits [list apple banana]
set vegetables [list carrot potato]
set numbers [list 1 2 3]

set food_list [list $fruits $vegetables]
puts $food_list  # 출력: {apple banana} {carrot potato}

lappend food_list $numbers
puts $food_list  # 출력: {apple banana} {carrot potato} {1 2 3}

set flat_list [concat {prefix} $food_list {suffix}]
puts $flat_list  # 출력: prefix apple banana carrot potato 1 2 3 suffix
```

### 6. 성능 고려사항 🚀

* `lappend`는 리스트 끝에 요소를 추가할 때 가장 효율적입니다.
* 대량의 리스트를 연결할 때는 `concat`보다 `lappend`를 반복 사용하는 것이 더 효율적일 수 있습니다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vlsi-korea.gitbook.io/chase-tcl/6-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
