Product Documentation
Cadence SKILL Language Reference
Product Version IC23.1, November 2023

letrec

letrec(
 (
  ( s_var1 s_initExp1 )
  ( s_var2 s_initExp2 )
 ... 
 )
body
) 
=> g_result 

Description

(SKILL++ mode) A letrec expression can be used in SKILL++ mode only. All the bindings are in effect while their initial values are being computed, thus allowing mutually recursive definitions. Use letrec to declare recursive local functions.

Recursive let form. Each binding of a variable has the entire letrec expression as its scope, making it possible to define mutually recursive procedures.

Use letrec when you want to declare recursive local functions. Each initialization expression can refer to the other local variables being declared, with the following restriction: each initialization expression must be executable without accessing any of those variables.

For example, a lambda expression satisfies this restriction because its body gets executed only when called, not when it’s defined.

Arguments

s_var

Name of a local variable. The variables are bound to fresh locations holding undefined values. Each variable is assigned to the result of the corresponding initExp.

s_initExp1

Expressions evaluated for the initial value. The initExps are evaluated in the resulting environment (in some unspecified order).

body

A sequence of one or more expressions. The expressions in body are evaluated sequentially in the extended environment.

Value Returned

g_result

Value of the last expression of body.

Examples

The following example declares a single recursive local function. The local function f computes the factorial of its argument. The letrec expression returns the factorial of 5.

letrec( 
( ;;; variable list
( f
lambda( ( n )
if( n > 0 then n*f(n-1) else 1
) ; if
) ; lambda
) ; f
) ; variable list
f( 5 )
) ; letrec
=> 120

Related Topics

Function and Program Structure


Return to top
 ⠀
X