Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 복잡한 구조 프로그래밍 프로그램 짤 때의 마음가짐 invariant list set

Similar presentations


Presentation on theme: "Lecture 7 복잡한 구조 프로그래밍 프로그램 짤 때의 마음가짐 invariant list set"— Presentation transcript:

1 Lecture 7 복잡한 구조 프로그래밍 프로그램 짤 때의 마음가짐 invariant list set
value-oriented vs. object-oriented

2 List ., 1-., 1-2-., 1-2-3-4-., 모든 List 는 . 이거나

3 정수 List를 C로 구현 typedef struct {int v; node *next;} node;
typedef node* list; list Null = 0; list link(int x; list l) { node *n; n = (node *)malloc(sizeof(node)); n->v = x; n->next = l; return n; } “리스트는 . 이거나” “있는 리스트에 하나 덧 붙인것”

4 List 만들기 link(1,Null); link(1,link(2,Null));
link(1,link(2,link(3,Null)));

5 List 사용하기 int head(list l) { if (isNull(l)) {
printf(“no head of empty list.”); exit(1); } else return l->v; list rest(list l) { printf(“no rest of empty list.”); else return l->next;

6 List 주무르기 두개의 list를 붙이기 list append(list l1, list l2) { … } list를 뒤집기
list reverse(list l) {…} list의 원소들을 모두 합하기 int sum(list l) {…} list에서 원하는 원소만 가지고 list만들기 list filter(list l) {…}

7 프로그램 짤 때의 마음가짐 있는 것을 변화시키는 것으로? object-oriented imperative
있는 것은 변하지 않도록? value-oriented applicative

8 싱거운 질문들 a = 1; b = 2; c = a + b; d = a + b; 1. c의 3이 d의 3과 같은가?
2. a를 바꾸면 c도 바뀔까? 3. d를 바꾸면 c도 바뀔까? 4. e = c 를 수행하면 c를 복사해야하나? 5. c갖고 일 봤으면, 그 3을 없애도 되나?

9 아직도 싱거울까? a = link(1,link(2,Null)); b = link(4,link(5,Null));
정수보다는 복잡한(컴퓨터에 구현할 때 하는일이 많은) 경우 a = link(1,link(2,Null)); b = link(4,link(5,Null)); c = append(a,b); d = append(a,b); 1. c의 리스트가 d의 리스트와 같은가? 2. a를 바꾸면 c도 바뀔까? 3. d를 바꾸면 c도 바뀔까? 4. e = c 를 수행하면 c를 복사해야하나? 5. c갖고 일 봤으면, 그 리스트를 없애도 되나?

10 물건 object vs value 값 물건 값 mutable, changing
반응이 물건에서 일어나므로, 물건의 상태를 항상 고려하면 ok immutable, unchanging 값은 변하지 않으므로 값만 알면 ok

11 list append(list l1, list l2)
물건이 변하게: append(list l1, list l2)는 list 물건 l1이 변해서 l2를 뒤에 매단것이되고 그렇게 변한 l1이 결과가 된다. 값으로서 변하지 않게: list 값들을 변화시키지 않고 l1과 l2가 붙어진 리스트를 뜻하는 새로운 리스트가 결과가 된다.

12 list를 물건으로 생각하기 list append(list l1, list l2) {
if (isNull(l1)) {return l2;} else if (isNull(l2)) {return l1;} else if (isNull(rest(l1)) { l1->next = l2; return l1; } else {append(rest(l1),l2); return l1;} list를 물건으로 생각하기

13 list를 값으로 생각하기 list append(list l1, list l2) {
if (isNull(l1)) {return l2;} else if (isNull(l2)) {return l1;} else if (isNull(rest(l1))) { return link(l1->v, append(rest(l1),l2) ); } list를 값으로 생각하기


Download ppt "Lecture 7 복잡한 구조 프로그래밍 프로그램 짤 때의 마음가짐 invariant list set"

Similar presentations


Ads by Google