수색…


게으른 평가 소개

F #을 비롯한 대부분의 프로그래밍 언어는 Strict Evaluation이라는 모델에 따라 즉시 계산을 평가합니다. 그러나 Lazy Evaluation에서는 계산이 필요하기 전까지 평가되지 않습니다. F #은 lazy 키워드와 sequences 통해 lazy evaluation을 사용할 수있게합니다.

// define a lazy computation
let comp = lazy(10 + 20)

// we need to force the result
let ans = comp.Force()

또한 Lazy Evaluation을 사용하면 계산 결과가 캐싱되므로 강제로 첫 번째 인스턴스를 실행 한 후에 결과를 강제로 실행하면 식 자체가 다시 평가되지 않습니다.

let rec factorial n = 
  if n = 0 then 
    1
  else 
    (factorial (n - 1)) * n


let computation = lazy(printfn "Hello World\n"; factorial 10)

// Hello World will be printed
let ans = computation.Force()

// Hello World will not be printed here
let ansAgain = computation.Force()

F #의 지연 평가 기능 소개

F #은 대부분의 프로그래밍 언어와 마찬가지로 기본적으로 엄격한 평가를 사용합니다. 엄격한 평가에서는 계산이 즉시 실행됩니다. 반대로 Lazy Evaluation은 결과가 필요할 때까지 계산 실행을 연기합니다. 게다가, Lazy Evaluation 하에서 계산 된 결과는 캐싱되어 표현의 재평가가 필요 없습니다.

lazy 키워드와 Sequences 통해 F #에서 지연 평가를 사용할 수 있습니다.

// 23 * 23 is not evaluated here
// lazy keyword creates lazy computation whose evaluation is deferred 
let x = lazy(23 * 23)

// we need to force the result
let y = x.Force()

// Hello World not printed here
let z = lazy(printfn "Hello World\n"; 23424)

// Hello World printed and 23424 returned
let ans1 = z.Force()

// Hello World not printed here as z as already been evaluated, but 23424 is
// returned
let ans2 = z.Force()


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow