2.1.3 데이터란 무엇인가 ?

본문의 예제코드들과 다르게, exercise 코드들은 모두 functional programming language 인 haskell 로 구현되어 있습니다.



앞선 절에서 - 데이터의 집합으로 - ( 포스트에는 현재 누락 ) 유리수를 정의해봤다. 

(간략히 소개하자면 유리수 데이터를 정의하고, 활용, 유리수 데이터로부터 분자와 분모를 확인하여 산술연산을 하는 프로시저의 집합이었다 )


 하지만 데이터란 진짜 무엇일까 ?

단순 프로시저들의 어떤 조합이 데이터라고 할 수 있을까 ?

물론 아니다. -


이 책에서는 데이터는 그 데이터의 '조건'이 검증되어야 - 데이터라고 할 수 있다 라고 이야기한다.

유리수를 예로 들자면, 지켜야하는 조건은


* 데이터 - 유리수의 조건



유리수 X 가 n 과 d 를 통해 얻어졌다면, x 의 분자 ( numor ) 를 x 의 분모 ( denom ) 으로 나눈 값은 

n을 d 로 나눈 값과 동일해야한다. 


이 된다. 


여기서 재밌는 생각을 하나 해본다.


데이터가 필요로하는 조건을 만족하면 데이터가 될 수 있다면, 

흔히 생각하는 structure 와 같은 구조를 이용하지 않고 데이터를 구성할 수 있는가 ?


cons / car / cdr 프로시저를 생각해보자. 

이 프로시저는 각각 pair 를 생성 / 첫번째 값을 가져오기 / 두번째 값을 가져오기를 수행한다.

예를 들어, (car (cons 1 2)) 의 값은 1, (cdr (cons 1 2)) 의 값은 2가 되는 식이다.


다음과 같이 구현해 볼 수도 있다.


* structure 없는 데이터의 필요조건만으로 구성한 cons 데이터 (pair)


(define (cons x y)

(define (dispatch m)

(cond 

((= m 0) x)

((= m 1) y)

(else (error "Argument not 0 or 1 -- CONS" m))))

dispatch)

(define (car z) (z 0))

(define (cdr z) (z 1))


cons x y 는 dispatch 라는 function 을 리턴하는데, 이 function 은 1개의 인자를 받아 이 인자가 0이면 x 를 반환하고, 1이면 y 를 반환한다. ( 그리고 둘 다 아닌 값이면 에러 출력 )

car 와 cdr 은 z 함수를 받아 인자를 각각 0과 1로 전달한다.


car 의 인자로 cons 2 3 을 전달한다면, (car (cons 2 3)) 

(car (dispatch 0)) 이 호출되는 셈이 되고, 결국 2 가 리턴된다.


Exercise 2.4 

Here is an alternative procedural representation of pairs. For this representation, verify that

(car (cons x y)) yields x for any objects x and y.

(define (cons x y)

(lambda (m) (m x y)))

(define (car z)

(z (lambda (p q) p)))

What is the corresponding definition of cdr? (Hint: To verify that this works, make use of the substitution

model of section 1.1.5.)



Exercise 2.5. Show that we can represent pairs of nonnegative integers using only numbers and arithmetic

operations if we represent the pair a and b as the integer that is the product 2a 3b. Give the corresponding

definitions of the procedures cons, car, and cdr.



Exercise 2.6. In case representing pairs as procedures wasn't mind-boggling enough, consider that, in a

language that can manipulate procedures, we can get by without numbers (at least insofar as nonnegative

integers are concerned) by implementing 0 and the operation of adding 1 as

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)

(lambda (f) (lambda (x) (f ((n f) x)))))

This representation is known as Church numerals, after its inventor, Alonzo Church, the logician who

invented the calculus.

Define one and two directly (not in terms of zero and add-1). (Hint: Use substitution to evaluate

(add-1 zero)). Give a direct definition of the addition procedure + (not in terms of repeated

application of add-1).



'SICP > 2. Building Abstractions with Data' 카테고리의 다른 글

2.5.3 Example: Symbolic Algebra  (0) 2015.11.01
2.5 System with Generic Operations  (0) 2015.09.13
2.3.4 Huffman Encoding Trees  (3) 2015.08.05

Tutorial 1. sync_timer

Programming/Boost asio 2015. 2. 4. 22:58

다음 내용에 기반합니다. 


http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/tutorial/tuttimer1.html


시작하기에 앞서, 환경을 구성한다.

boost windows prebuilt 를 받아 적당한 경로에 준비시켜 두었으며,

그 적당한 경로를 Project > Properties 에서 include header 설정 해두었고, 



마찬가지로 lib 파일의 경로도 잡아준다. 




lib 파일의 경로는 잡아줬지만, 실제 링킹되어야 하는 파일명은 dependencies 에 지정하지 않은 상태다.

직접 링크에러가 날때마다 properties 에 추가하거나, pragma comment 로 소스코드에서 linking 할 수 있는데, 

편의상 소스코드에 넣었다.


sync_timer 소스코드는 다음과 같다. 



첫 튜토리얼 답게 굉장히.. 간단하다.

그냥 asio 의 deadline_timer 를 이용, 생성시 지정한 시간만큼 대기하다 종료하는 프로그램.

deadline_timer 의 wait() 함수는 지정된 시간이 지나기 전까지 리턴되지 않는다.


io_service 라는 생소한 클래스와 deadline_timer 라는 녀석이 있는데, 

deadline_timer 는 그냥 timer 기능을 제공하는 녀석이구나 정도로 쉽게 이해가 갈텐데, 

이 녀석이 사용하는 io_service 라는 클래스가 앞으로도 상당히 자주 보게될 클래스. 

I/O 기능을 제공한다 - 라고 튜토리얼에서는 소개하고 있는데, 단독으로 사용되는 경우는 없고,(아는한) 이 예의 deadline_timer 같은 io_object 와 결합이 되어 사용된다. 


IO_SERVICE 

> io_object 로부터 시작 요청을 받으면 OS 와 io 동작을 제공해주는 클래스. 

sync 의 경우에,

시작함수 (initiating function) 가 호출된 스레드에서 I/O 를 수행한 후 리턴


async 의 경우에, 

io_object 의 시작 함수가 (initiating function) 호출되면 io_object 에 지정된 io_service 가 OS 영역-정확히는 아니지만 이렇게 이해해도 큰 무리가 없다고 boost 개발자가 설명한바있다..(로 들렸다)-에 work 라는 객체와 핸들러를 함께 전달하여 queuing 해뒀다가, io_service.run() 이 호출되면 OS 로 실제로 요청 동작을 실행하고, 

OS 로부터 io_service 로 완료 알림이 오면 io_service 는 생성했던 work 객체와 handler 를 dequeue 하여 핸들러를 호출한다. 


실행결과는 - 지정한 5초동안 아무 반응이없다가 hello world 가 출력되고 프로그램이 종료되는 것이다.





'Programming > Boost asio' 카테고리의 다른 글

Tutorial 5. Synchronising handlers in multithreaded programs  (0) 2015.04.11
Tutorial 4. member function handler  (0) 2015.03.17
Tutorial 3. Binding arguments to a handler  (0) 2015.03.15
Tutorial 2. async_timer  (0) 2015.03.14
0. 소개  (0) 2015.02.02

0. 소개

Programming/Boost asio 2015. 2. 2. 00:22

C++ Third-party 라이브러리 (라기엔 너무 메이저한) Boost 의 부분인 

asio 의 tutorial 을 정리할 예정입니다. 


다음 내용에 기반합니다.


http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/tutorial.html

Basic Skills

The tutorial programs in this first section introduce the fundamental concepts required to use the asio toolkit. Before plunging into the complex world of network programming, these tutorial programs illustrate the basic skills using simple asynchronous timers.

Introduction to Sockets

The tutorial programs in this section show how to use asio to develop simple client and server programs. These tutorial programs are based around the daytime protocol, which supports both TCP and UDP.

The first three tutorial programs implement the daytime protocol using TCP.

The next three tutorial programs implement the daytime protocol using UDP.

The last tutorial program in this section demonstrates how asio allows the TCP and UDP servers to be easily combined into a single program.