recursion 결과를 구하는 시간

Haskell 초보입니다. recursive하게 구현된 factorial 함수를 이용하여

map factorial [1..1000]

와 같이 1~1000까지에 대해 factorial을 모두 구할 때, factorial(1000)은 앞서 계산된 factorial(999)의 값을 이용하여 단시간에 계산할 수 있을텐데 실제로 ghc의 수행시간을 보면 매번 factorial(1)부터 새로 계산하는 것 같습니다. 아마 Haskell 언어 스펙상으로는 factorial(999)가 factorial(1000)보다 먼저 계산된다는 보장은 안될 것 같습니다만, 그래도 인터프리터의 내부 구현상 이미 계산된 값을 이용하여 속도를 향상시키는 옵션 정도는 있지 않을까 싶은데 찾아봐도 그런게 안보이네요.

댓글 보기 옵션

원하시는 댓글 전시 방법을 선택한 다음 "설정 저장"을 누르셔서 적용하십시오.

...

바로 가능하지 않을까 생각해봤는데 그렇지 않은가 보네요.

우선 "haskell memoization"으로 검색해본 결과

* http://mikeburrell.wordpress.com/2007/04/04/memoizing-haskell/
* http://okmij.org/ftp/Haskell/#controlled-memoization
* http://groups.google.com/group/fa.haskell/browse_thread/thread/21e0d5d5a0d34957/3ceeb42f0d69f7c0?lnk=raot#3ceeb42f0d69f7c0
* http://www.kimbly.com/blog/000263.html
...등처럼 template haskell을 이용하거나 infinite list을 응용하거나 하는 방법, 그리고 monad을 이용하는 방법이 있더라구요.

ghc에서 명시적으로 최적화를 하는 방법은 인라이닝이나 특정 타입에 대해 특화 해주는 방식등으로 RULES을 이용하는 방법이 있는데 이는 아닌거 같네요.

음... haskell 같은 purely functional language은 memoization을 기본제공할것 같은데... 흠.

명시적으로 이를 사용하는 예는 Factor language에서 다음처럼 합니다. (저도 예전에 똑같은 예를 이용해 이를 보인적이;;;)

http://ageldama.egloos.com/3189250

----
블로그: http://ageldama.egloos.com

Recursion, in mathematics

Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition; specifically it is defining an infinite statement using finite components. The term is also used more generally to describe a process of repeating objects in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion accuplacer study guide. A convenient mental model of recursion defines the recursive object (whether that object is an equation, an algorithm, an image, or a rule) in terms of "previously defined" objects of the same class. For example acsm study guide: How do you move a stack of 100 boxes? Answer: you move one box, remember where you put it, and then solve the smaller problem: how do you move a stack of 99 boxes? Eventually, you're left with the problem of how to move a single box, which you know how to do act study guide.
--------------------------------------------------------
aepa study guide

지난 3 년 동안 나는

지난 3 년 동안 나는 f를 설계, silk pyjamas 개발 등 제품, 생산 또는 鄚 Zust의 파고를 구입 僡 주위에 다른 지역을했다. 그 이후로 우리는 외국의 성장, mens pyjamas 즉 내가, 羦 거의 독점적으로 친구에 의해 오전 케이 단순한 悳를 받았습니다 나는 끊임없이 새로운 지속 가능한 브랜드, 컬렉션에서 찾고 찾고 및 bananas pyjamas 구매를합니다.

이런 경우

이런 경우에 쓰기 좋은 함수로 scanl 함수가 있습니다. factorial n = foldl (*) 1 [1..n] 임을 생각하면 scanl (*) 1 [1..n] 하시면 원하는 결과가 나옵니다. scanl 함수의 정의를 참고하시면 감이 오실 겁니다.

OCaml 은 strongly

OCaml 은 strongly statically typed 언어 입니다. 때문에 실행전에 타입을 유추합니다.OCaml 은 타입추론기를 통해 646-276 자동으로 타입을 찾기 때문에 여러분은 타입에 대해 작성하실 필요가 없습니다. 위의 코드를 Ocaml topLevel 로 실행시켜보면 Ocaml 은 위의 함수가 문제가 없다고 알려 줄 것입니다.Ocaml은 어떠한 묵시적 형 변환을 허용하지 않습니다. 만약 float를 원한다면, 2.0 이라 작성하셔야 NS0-163 합니다. 2는 int 형이기 때문입니다.Ocaml은 어떠한 묵시적 형 변환을 허용하지 않습니다. 따라서 연산자 역시 각기 다릅니다. "두개의 정수를 더한다 " (는 + 646-985 연산자를 사용한다) vs. "두개의 소수를 더한다" (는+. 연산자를 사용한다. - note 점이 찍혀 있음을 유의하세요). 다른 산술연산자도 이렇습니다.Ocaml은 마지막 문장을 리턴합니다. 때문에 70-630 C처럼 return을 쓰실 필요가 없습니다.더욱 자세한 내용은 다음 섹션에서 다루도록 하겠습니다.