Programming 언어/Skill
[SKILL 강좌] list 함수들 (cons, append, car, cdr, nth, xCoord, yCoord...)
호드맨
2017. 1. 31. 07:22
list에 자주 사용되는 함수들을 알아봅시다. car, nth, xCoord 이런 것들과 cons, append까지~
;# 리스트 만들기
list(1 2 3)
(1 2 3)
`(1 2 3)
(1 2 3)
temp_list = list(1 2 3)
(1 2 3)
temp_list
(1 2 3)
;# 리스트 인지 판단
listp(temp_list)
t
;# 리스트 앞쪽에 연결
cons(0 temp_list)
(0 1 2 3)
;# 리스트 뒤에 리스트 연결
append(temp_list list(4))
(1 2 3 4)
append(temp_list list(4 5))
(1 2 3 4 5)
;# 리스트 뒤에 원소 연결
append1(temp_list 4)
(1 2 3 4)
;# 리스트의 첫번째 값
car(temp_list)
1
nth(0 temp_list)
1
xCoord(temp_list)
1
;# 리스트의 첫번째 값을 제외한 두번째부터 끝까지의 리스트
cdr(temp_list)
(2 3)
;# 리스트의 두번째 값
car(cdr(temp_list))
2
cadr(temp_list)
2
nth(1 temp_list)
2
yCoord(temp_list)
2
;# 리스트의 세번째 값
car(cdr(cdr(temp_list)))
3
caddr(temp_list)
3
nth(2 temp_list)
3
;# 리스트의 길이 (원소 개수)
length(temp_list)
3
조금 더 깊이 가보면 table(array) 도 있습니다. 저 같은 경우 그다지 쓸 일은 없었지만요~
myTable = makeTable("mTable1")
table:mTable1
myTable[0] = "first element of table"
"first element of table"
myTable[1] = "second element of table"
"second element of table"
myTable[2] = "third element of table"
"third element of table"
언젠가 어느 분이 여쭤봤었는데.. 2D array도 가능은 합니다. 간단하게는 list를 이용하여
myTable[list(0 0)] = "first of first element of 2D array"
"first of first element of 2D array"
myTable[list(0 1)] = "second of first element of 2D array"
"second of first element of 2D array"
myTable[list(1 0)] = "first of second element of 2D array"
"first of second element of 2D array"
myTable[list(1 1)] = "second of second element of 2D array"
"second of second element of 2D array"
;# 이렇게 사용하거나 다음을 참조하면 Multi Dimension Array 사용이 가능합니다.
;# http://www.cadence.com/Community/forums/p/14088/23293.aspx
간단하게 list 관련 함수를 알아봤고 눈으로만 보면 감이 잘 안 올 테니 몇 번 써보면 됩니다. xCoord yCoord의 경우 대부분 좌표가 list( x좌표 y좌표 )로 이루어져 있기에 좌표값 컨트롤할 때 사용되기도 합니다. 대부분의 데이터들이 list로 이루어져 있기 때문에 나중에 코딩하게 되면 자주 사용하게 될 겁니다.
다음 강좌는 어느 쪽으로 방향을 잡아야 할지 고민인데.. 생뚱맞지만 정규표현식으로 가겠습니다.