'Programming/언어론'에 해당되는 글 6건

  1. 2025.01.31 일급 함수 first-class function
  2. 2014.08.13 dangling if-else
  3. 2011.03.25 double의 정확도 자릿수
  4. 2010.09.16 함수 포인터 (function pointer)
  5. 2010.09.15 type system
  6. 2010.04.17 calling convention(콜링 컨벤션)
Programming/언어론2025. 1. 31. 14:39

람다와 클로저 보다보니

자바의 개념까지 언급이 되서 보는 중

 

함수를 다른 함수의 인자로 전달한다..

함수 포인터와는 다른 개념이겠지?

 

컴퓨터 과학에서 프로그래밍 언어는 함수를 일급 객체로 취급하는 경우 일급 함수(first-class function)를 갖는다고 한다. 이는 언어가 함수를 다른 함수에 대한 인수로 전달하고, 이를 다른 함수의 값으로 반환하고, 변수에 할당하거나 자료 구조에 저장하는 것을 지원한다는 것을 의미한다.[1] 일부 프로그래밍 언어 이론가들은 익명 함수(함수 리터럴)에 대한 지원도 요구한다.[2] 일급 함수를 사용하는 언어에서는 함수 이름에 특별한 상태가 없다. 이는 함수 유형의 일반 변수처럼 취급된다.[3] 이 용어는 1960년대 중반 크리스토퍼 스트래치가 "일급 객체로서의 기능"이라는 맥락에서 만들어졌다.[4]

일급 함수는 고차 함수의 사용이 표준 관행인 함수형 프로그래밍 스타일에 필수적이다. 고차 함수의 간단한 예는 함수와 목록을 인수로 사용하고 목록의 각 구성원에 함수를 적용하여 형성된 목록을 반환하는 map 함수이다. 언어가 맵을 지원하려면 함수를 인수로 전달하는 것을 지원해야 한다.

함수를 인수로 전달하거나 결과로 반환하는 데는 특정 구현상의 어려움이 있다. 특히 중첩 및 익명 함수에 도입된 비지역 변수가 있는 경우에는 더욱 그렇다. 역사적으로 이러한 문제는 "함수 인수"에서 유래한 이름인 funarg 문제라고 불렸다.[5] 초기 명령형 언어에서는 함수를 결과 유형(예: 알골 60, 파스칼)으로 지원하지 않거나 중첩 함수 및 비지역 변수(예: C)를 생략하여 이러한 문제를 방지했다. 초기 함수형 언어 리스프는 동적 범위 지정의 접근 방식을 취했다. 여기서 비지역 변수는 함수가 정의된 위치 대신 함수가 실행되는 지점에서 해당 변수의 가장 가까운 정의를 참조한다. 어휘 범위가 지정된 일급 함수에 대한 적절한 지원이 스킴에 도입되었으며 함수에 대한 참조를 기본 함수 포인터[4] 대신 클로저로 처리해야 하므로 쓰레기 수집이 필요하다.

[링크 : https://ko.wikipedia.org/wiki/일급_함수]

 

컴퓨터 프로그래밍 언어 디자인에서, 일급 객체(영어: first-class object)란 다른 객체들에 일반적으로 적용 가능한 연산을 모두 지원하는 객체를 가리킨다. 보통 함수에 인자로 넘기기, 수정하기, 변수에 대입하기와 같은 연산을 지원할 때 일급 객체라고 한다.

[링크 : https://ko.wikipedia.org/wiki/일급_객체]

 

[링크 : https://inpa.tistory.com/entry/CS-👨%E2%80%8D💻-일급-객체first-class-object]

'Programming > 언어론' 카테고리의 다른 글

dangling if-else  (0) 2014.08.13
double의 정확도 자릿수  (0) 2011.03.25
함수 포인터 (function pointer)  (0) 2010.09.16
type system  (0) 2010.09.15
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니
Programming/언어론2014. 8. 13. 17:16
문득.. 내가 쓰는 문법이 댕글링 if-else를 생성하는거 같아서 찾아보니..
딱히 문제는 없도록 C언어에서 문법적으로 처리한다고 해서 한숨을 쉬는중..

일단 자바와 C C++은 인접한 if에 else가 붙는다고 한다.
Java, C and C++ have chosen to resolve the Dangling-Else ambiguity uses the following rule: 
An else keyword always associates with the nearest preceeding if keyword that does NOT cause a syntax error

[링크 : http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/2-C/dangling-else.html]  

[링크 : http://en.wikipedia.org/wiki/Dangling_else]

'Programming > 언어론' 카테고리의 다른 글

일급 함수 first-class function  (0) 2025.01.31
double의 정확도 자릿수  (0) 2011.03.25
함수 포인터 (function pointer)  (0) 2010.09.16
type system  (0) 2010.09.15
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니
Programming/언어론2011. 3. 25. 21:40
IEEE 754 floating point 표준에 보면은,
float형은 7자리까지는 그 정밀도를 보장할 수 있고, 그 이후로는 부정확하다고 되어 있다.
그리고, double형은 15자리까지 정밀도를 보장할 수 있다고 한다.

[링크 : http://kimstar.pe.kr/blog/172]
[링크 : http://en.wikipedia.org/wiki/Double_precision_floating-point_format

'Programming > 언어론' 카테고리의 다른 글

일급 함수 first-class function  (0) 2025.01.31
dangling if-else  (0) 2014.08.13
함수 포인터 (function pointer)  (0) 2010.09.16
type system  (0) 2010.09.15
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니
Programming/언어론2010. 9. 16. 09:15
포인터는 메모리의 특정 지점을 찍어주는 역활을 한다.
즉, 데이터든 코드든 어디든 찍을수가 있다.

함수 포인터에서 의문점이 생긴것은 바로 함수 포인터의 선언부분이다.
#include <stdio.h>

void testout1(const char *s) {
  printf("시험출력1:%s\n", s);
}

void testout2(const char *s) {
  printf("시험출력2:%s\n", s);
}

// 함수포인터 변수
void (*funcPtr)(const char *s);

int main() {
  // 함수포인터를 testout1, testout2로 각각 대입해보고 실행해본다.
  funcPtr = testout1;
  funcPtr("테스트");
  funcPtr = testout2;
  funcPtr("테스트");
}

[링크 : http://www.redwiki.net/wiki/wiki.php/%C7%D4%BC%F6%C6%F7%C0%CE%C5%CD]

위의 코드를 보면,
 void (*funcPtr)(const char* s);
위의 부분에서 함수 포인터의 인자(arguments)들의 형은 존재하지만, 변수명은 존재하지 않는 경우가 많다.

물론 VS2010 에서 컴파일을 해보아도
변수명이 존재를 하던 변수형만 존재를 하던 상관은 없지만 왜 그럴까? 라는 궁금증이 생겼다.

왜 그럴까?
아마도, 함수 포인터에서 중요한건 연결되는 함수와,
넘겨지는 변수들의 형, 그리고 갯수이지 변수명이 아니기 때문이 아닐까?

어짜피 stdcall 형태로 calling convention을 유지한다면
넘어가는 변수명 따위는 내부적으로는 의미가 없고, 넘어가는 순서가 중요하니 말이다.
그런 이유로, 함수 포인터의 선언에는 변수형만 존재하고 변수명은 옵션으로 존재하는 것으로 생각된다.

[링크 : http://www.cplusplus.com/doc/tutorial/pointers/]

'Programming > 언어론' 카테고리의 다른 글

일급 함수 first-class function  (0) 2025.01.31
dangling if-else  (0) 2014.08.13
double의 정확도 자릿수  (0) 2011.03.25
type system  (0) 2010.09.15
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니
Programming/언어론2010. 9. 15. 11:38
일반적으로 많이 접하는 '컴파일'언어들은 '형(type)'이 존재한다.
이러한 형의 존재를 type system 이라고 하고, 동적 형변환과 정적 형변환 그리고 강한 형검사, 약한 형검사로 나윈다.

음.. 결론은 없는 문제이지만,
강형(강한 형검사)/약형(약한 형검사)는 상대적인 것이라 나누는 것이 딱히 의미는 없어 보이고,
중요한건 동적 형 변환/정적 형 변환이냐의 차이가 아닐까 생각이 된다.

강한 형검사(strong typing)
 In computer science and computer programming, a type system is said to feature strong typing when it specifies one or more restrictions on how operations involving values of different data types can be intermixed. The opposite of strong typing is weak typing.

약한 형검사(weak typing)
 In computer science, weak typing (a.k.a. loose typing) is a property attributed to the type systems of some programming languages. It is the opposite of strong typing, and consequently the term weak typing has as many different meanings as strong typing does.

동적 형 변환(dynamic typing)
 A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, values have types but variables do not; that is, a variable can refer to a value of any type. Dynamically typed languages include Erlang, Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (with respect to user-defined types but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk and Tcl.

정적 형 변환(static typing)
 A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Statically typed languages include Ada, AS3, C, C++, C#, Eiffel, F#, Go, JADE, Java, Fortran, Haskell, ML, Objective-C, Pascal, Perl (with respect to distinguishing scalars, arrays, hashes and subroutines) and Scala. Static typing is a limited form of program verification (see type safety): accordingly, it allows many type errors to be caught early in the development cycle.

[링크 : http://kldp.org/node/55577]
[링크 : http://en.wikipedia.org/wiki/Statically_typed]
    [링크 : http://en.wikipedia.org/wiki/Statically_typed#Dynamic_typing]
    [링크 : http://en.wikipedia.org/wiki/Statically_typed#Static_typing]
[링크 : http://en.wikipedia.org/wiki/Weak_typing]
[링크 : http://en.wikipedia.org/wiki/Strong_typing]


언어별로 대충 훑어 보자면,
정적 형 변환 언어는 C 처럼 변수형을 지정하고 사용하는 언어이고
동적 형 변환 언어는 python이나 스크립트 처럼 변수형이 없이 변수명에 임의의 값을 넣고 사용하는 언어로 보인다.

'Programming > 언어론' 카테고리의 다른 글

일급 함수 first-class function  (0) 2025.01.31
dangling if-else  (0) 2014.08.13
double의 정확도 자릿수  (0) 2011.03.25
함수 포인터 (function pointer)  (0) 2010.09.16
calling convention(콜링 컨벤션)  (0) 2010.04.17
Posted by 구차니
Programming/언어론2010. 4. 17. 17:39
convention 은

[링크 : http://engdic.daum.net/dicen/contents.do?query1=E255050]
이런 의미를 가지는데, 굳이 해석하자면, 호출 규약 혹은 관용적인 호출방법 이라고 하면 되려나?

아무튼 문득 Iczelion 의 강좌를 읽다보니 어셈블리 기본 구조에
STDCALL 이라는 용어가 나오고, calling convention 이 나오길래 자세히 읽어 봤더니
'함수를 호출시에 인자를 넘겨주는 데이터를 stack에 넣는 방법'을 의미한다.

.MODEL FLAT, STDCALL
.MODEL is an assembler directive that specifies memory model of your program. Under Win32, there's only on model, FLAT model.
STDCALL tells MASM about parameter passing convention. Parameter passing convention specifies the order of  parameter passing, left-to-right or right-to-left, and also who will balance the stack frame after the function call.
Under Win16, there are two types of calling convention, C and PASCAL
C calling convention passes parameters from right to left, that is , the rightmost parameter is pushed first. The caller is responsible for balancing the stack frame after the call. For example, in order to call a function named foo(int first_param, int second_param, int third_param) in C calling convention the asm codes will look like this:

push  [third_param]               ; Push the third parameter
push  [second_param]            ; Followed by the second
push  [first_param]                ; And the first
call    foo
add    sp, 12                                ; The caller balances the stack frame
PASCAL calling convention is the reverse of C calling convention. It passes parameters from left to right and the callee is responsible for the stack balancing after the call.
Win16 adopts
PASCAL convention because it produces smaller codes. C convention is useful when you don't know how many parameters will be passed to the function as in the case of wsprintf(). In the case of wsprintf(), the function has no way to determine beforehand how many parameters will be pushed on the stack, so it cannot do the stack balancing.
STDCALL is the hybrid of C and PASCAL convention. It passes parameter from right to left but the callee is responsible for stack balancing after the call.Win32 platform use STDCALL exclusively. Except in one case: wsprintf(). You must use C calling convention with wsprintf().


[링크 : http://win32assembly.online.fr/tut1.html]

예를 들어, C언어의 경우에는  STDCALL을 사용하며,
인자(argument)들은 오른쪽 부터 stack에 push() 된다.

즉,
push(arg4);
push(arg3);
push(arg2);
push(arg1);
이런식으로 함수 호출시 인자를 넘겨주게 된다.


음.. 개인적인 생각이지만, C언어의 경우 heap은 아래에서 위로 커나가는데 그 방향을 맞추려고
stack에도 데이터를 이렇게 반대 순서로 넣는게 아닐까 싶다.

물론 argument 순서가 의미는 없기 때문에(받아들이는 쪽에서 알아서 받는다면)
가장 위에 첫 인자가 있을 이유는 없는데, 굳이 이런식으로 방향성을 가진다는건...
메모리 구조에 기인한게 아닐려나?

(아니면 말구 -ㅁ-!)

'Programming > 언어론' 카테고리의 다른 글

일급 함수 first-class function  (0) 2025.01.31
dangling if-else  (0) 2014.08.13
double의 정확도 자릿수  (0) 2011.03.25
함수 포인터 (function pointer)  (0) 2010.09.16
type system  (0) 2010.09.15
Posted by 구차니