'하노이의 탑' 알고리즘에서 재귀함수란 것을 접해보고, 그 후론 거의 사용할 일이 없었는데....

Functional Language들을 살펴보다보니 재귀 알고리즘에 보다 관심이 생겨난다.


특히, 일반적인 재귀보다 Tail Recursion을 유심히....


아래 예제는 정수 n을 입력인자로하여 1부터 n까지의 합을 구하는 로직이다.

먼저, 일반적인 재귀함수는 대강 이렇게 생겨먹었다.

int sum(int n)

{

    if(n == 1) return 1;

    return (n + sum(n-1));

}


이번에는 Tail Recursion으로 구현해본다.

int sum(int n)

{

    return sum_tail_recursion(n, 0);

}

int sum_tail_recursion(int n, int acc)

{

    if(n == 0) return acc;

    return sum_tail_recursion(n-1, n+acc);

}


코드 길이 따위에 관심을 가질것이 아니라, "함수 호출"에 의한 스택 메모리 사용에 관심을 가져야하겠다.

첫번째 코드상에서 return ( n + sum(n-1) );을 보면

sum(n-1)이 리턴된 후에 다시 n과 더해지기 위해서 스택메모리에 정보가 계속 누적되는것을 알수있다.

반면에 Tail Recursion을 적용했을때는 그럴필요가 없어진다.

정수 n을 충~~~분히 크게 한다면 그 차이가 극명하게 드러난다.


좀 똑똑한 컴파일러라면 Tail Recursion으로 구현된 재귀함수를 반복루프로 바꿔버린다.(어셈블리 언어로 보면 신기하게도 같게 나온다)

물론 최적화 옵션을 줬을때 얘기지만....

(사용하는 컴파일러에서 해당부분을 꼭 살펴봐야한다. 하위버전에서 지원되던것이 상위버전에서 사라지는 경우도 분명 있으니까.. )



--- ...

Posted by Jason Ryu
,


(http://stackoverflow.com/questions/5276967/python-in-xcode-4)




I figured it out! The steps look lengthy but it's not that bad at all.


  1. Open Xcode 4.
  2. In the menu bar, click "File" → "New" → "New Project…".
  3. Select "Other" under "Mac OS X".
  4. Select "External Build System" and click "Next".
  5. Enter the product name.
  6. For the "Build Tool" field, type in /usr/local/bin/python3 for Python 3 or /usr/bin/python for Python 2 and then click "Next". Note that this assumes you have Python installed in the typical location(s). if you are unsure as to where your Python executables are enter these commands into Terminal: which python3 and which python.
  7. Choose where to save it and click "Create".
  8. In the menu bar, click "File" → "New" → "New File…".
  9. Select "Other" under "Mac OS X".
  10. Select "Empty" and click "Next".
  11. Navigate to the project folder (it will not work, otherwise), enter the name of the Python file (include the ".py" extension), and click "Save".
  12. In the menu bar, click "Product" → "Edit Scheme…".
  13. Click "Run" in the left column.
  14. In the "Info" tab, click the "Executable" field and then click "Other…".
  15. Navigate to the executable from Step 6. You may need to use ⇧⌘G to type in the directory if it is hidden.
  16. Select the executable and click "Choose".
  17. For the "Debugger" field, select "None".
  18. In the "Arguments" tab, click the "Base Expansions On" field and select the target that is named the same as your project.
  19. Click the "+" icon under "Arguments Passed On Launch". You may have to expand that section by clicking on the triangle pointing to the right.
  20. Type in $(SOURCE_ROOT)/ and then the name of the Python file you want to test. Remember, the Python program must be in the project folder. Otherwise, you will have to type out the full path (or relative path if it's in a subfolder of the project folder) here. If there are spaces anywhere in the full path, you must include quotation marks at the beginning and end of this.
  21. Click "OK".
  22. Start coding.


Note that if you open the "Utilities" panel, with the "Show the File inspector" tab active, the file type is automatically set to "Default - Python script". Feel free to look through all the file type options it has to gain an idea as to what all it is capable of doing. The method above can be applied to any interpreted language. As of right now, I have yet to figure out exactly how to get it to work with Java; then again, I haven't done too much research. Surely there is some documentation floating around on the web about all of this.

Say, "Hello, code completion, auto-indentation, and syntax highlighting." Note that it's not as advanced as it is with C, C++, or Objective-C but it is better than using IDLE in my opinion.



Posted by Jason Ryu
,