자바스크립트에서 함수형 언어를 보다가 질문합니다.

doodoo의 이미지

http://functional.or.kr/javascript/functional
여기에 보면 마지막 부분에

function bind(func, x){
    return function(y){ return func(x,y) }
}
 
function plus(x,y){ return x+y } // 더하기 함수
 
inc = bind( plus, 1 ) // 말그대로 더하기 1을 하는 함수를 만든다.

라고 되어 있어서 결국 마지막줄 bind 합수가 1을 더하는 함수다 라고 했는데..

그럼 return function(y){ return func(x,y) } 이줄에 있는 y 값은 어떻게
보란 말인가요?

bind 에서 plus 를 호출했지만...plus 는 인자가 두개 받는것 아니었던가요?
그렇다면 제대로 호출하려면 bind(plus, 1, 1) 등으로 호출해야 하는것 아닌가요?

아...머리아퍼....

gamdora의 이미지

bind는 plus를 부르지 않습니다.

plus를 부르는 건 inc입니다.

d3m3vilurr의 이미지

function bind(func, x) {
  return function(y) {...}
}
 
inc = bind(plus, 1);
inc(2);

저 코드는 저런식으로 사용되는데 새 함수의 레퍼런스를 반환하기 때문입니다
즉,

function bind(func, x) {
  function tmp(y) {
    ...
  }
  return tmp;
}

와 같습니다.

여기서 내부 함수의 내용은 다시
return func(x, y); 임으로
bind는 인자를 2개 받는 함수에 첫 인자 값을 지정하여 1개의 인자만 받는 함수로 만드는 함수로 볼 수 있습니다.

gamdora의 이미지

치환법으로 살펴보면 다음과 같을 겁니다:

inc = bind( plus, 1 )
inc = function(y){ return plus(1,y) }
inc = function(y){ return 1+y }
function inc(y){ return 1+y }

그러므로 inc는 1 더해서 되돌리는 함수입니다.

doodoo의 이미지

어흑....ㅜ.ㅜ

알것 같으면서도 모를것 같은 ..... 머리가 굳었나봐...

winner의 이미지

아마 내부 mechanism을 생각하고 계신 것 같은데 그걸 잊고, 그냥 문법이다 생각하시면 어떨까요?

M.W.Park의 이미지

정말 진지하게 알고 싶으면, 함수형 언어(lisp, scheme 등) 한가지를 공부해보세요.
그리고, 참고로 gamdora님이 언급하신 치환법(substitution model?)은 SICP 첫부분에 등장했던 것으로 기억합니다.

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.