GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • WOLFRAM
  • Products
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App
  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs
  • Wolfram AI Ecosystem

This view

  • Original page
  • New address

GeezerVison is showing reference.wolfram.com — Nest—Wolfram Documentation

Nest[f,expr,n]

gives an expression with f applied n times to expr.

Nest[f,n]

represents an operator form of Nest that can be applied to expressions.

Details
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Applications  
Properties & Relations  
Neat Examples  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page

Nest

Nest[f,expr,n]

gives an expression with f applied n times to expr.

Nest[f,n]

represents an operator form of Nest that can be applied to expressions.

Details

  • You can use Throw to exit from Nest before it is finished. »

Examples

open all close all

Basic Examples  (2)

Apply f to x from 0 to 4 times:

Wolfram Language code: Nest[f, x, 4]

The function to nest can be a pure function:

Wolfram Language code: Nest[(1 + #) ^ 2&, 1, 3]

Scope  (4)

Nesting a function can build a formula:

Wolfram Language code: Nest[(1 + #) ^ 2&, x, 5]

Nesting can return a single number:

Wolfram Language code: Nest[Sqrt, 100.0, 4]

The nested function can operate on a list:

Wolfram Language code: With[{ϵ = 0.1}, Nest[Function[xy, {2ϵ, 0} + {{1 - 2ϵ, 0}, {0, 1 - 2ϵ}}.xy], {1.0, 1.0}, 5]]

Apply the operator form to different starting expressions:

Wolfram Language code: op = Nest[Sin, 3];
Wolfram Language code: {op[0], op[π / 4], op[π / 2]}

Generalizations & Extensions  (2)

Use Throw to exit a Nest:

Wolfram Language code: NestList[# ^ 2&, 2, 6]
Wolfram Language code: Catch[Nest[If[# > 10 ^ 6, Throw[#], # ^ 2]&, 2, 6]]

To nest a function of more than one argument, the arguments can be put into a list:

Wolfram Language code: Nest[{(#[[1]] + #[[2]]/2), Sqrt[#[[1]] #[[2]]]}&, {0.5, 1.0}, 10]

Applications  (8)

Continued fraction:

Wolfram Language code: Nest[1 / (1 + #)&, x, 5]

Power tower:

Wolfram Language code: Nest[x ^ #&, x, 6]

Growth of annually compounded capital in 10 years:

Wolfram Language code: Nest[#(1 + 0.05)&, 1000, 10]

Newton iterations for :

Wolfram Language code: Nest[(# + 2 / # ) / 2&, 1.0, 5]

Iterated string replacements:

Wolfram Language code: Nest[StringReplace[#, {"A" -> "BA", "B" -> "AB"}]&, "A", 5]

Consecutive pairs of Fibonacci numbers:

Wolfram Language code: Nest[{{1, 1}, {1, 0}}.#&, {0, 1}, 10]

Functional composition for higher-order Newton iteration (for ):

Wolfram Language code: Function[x, Evaluate[Together[Nest[Function[z, (z + 3 / z) / 2], x, 4]]]]
Wolfram Language code: FixedPointList[%, 1`50]

Generate a bifurcation diagram for an iterated logistic map:

Wolfram Language code: ListPlot[Table[Thread[{r, Nest[r # (1 - #)&, Range[0, 1, 0.01], 1000]}], {r, 0, 4, 0.01}]]

Properties & Relations  (6)

Nest gives the last element of NestList:

Wolfram Language code: NestList[f, x, 4]
Wolfram Language code: Nest[f, x, 4]

Nest is effectively Composition using the same function multiple times:

Wolfram Language code: Nest[f, x, 3]
Wolfram Language code: Composition[f, f, f][x]

Use RSolve to symbolically compute Nest operations:

Wolfram Language code: f[x_] := (2x + 3/x + 4)
Wolfram Language code: RSolve[y[k + 1] == f[y[k]] && y[0] == 0, y, k]
Wolfram Language code: g = y /. First[%]

Different length compositions or nests give the same result:

Wolfram Language code: Table[g[k], {k, 1, 5}]
Wolfram Language code: Table[Nest[f, 0, k], {k, 1, 5}]

FixedPoint automatically goes on until the result no longer changes:

Wolfram Language code: Nest[Floor[# / 2]&, 20, 6]
Wolfram Language code: FixedPoint[Floor[# / 2]&, 20]

NestWhile goes on while a condition is true:

Wolfram Language code: NestWhile[If[EvenQ[#], # / 2, (3 # + 1) / 2]&, 29, # > 1&]

Fold automatically inserts second arguments from a list:

Wolfram Language code: Nest[f[#, a]&, x, 3]
Wolfram Language code: Fold[f, x, {a, a, a}]

Neat Examples  (4)

Wolfram Language code: Nest[Framed, x, 6]

Binary tree:

Wolfram Language code: Nest[{#, #}&, x, 3]
Wolfram Language code: Nest[Framed[Row[{#, #}]]&, "", 5]
Wolfram Language code: Nest[p[#][#]&, x, 4]
Wolfram Language code: Nest[Subsuperscript[#, #, #]&, x, 5]

Gray codes of length 4:

Wolfram Language code: Nest[Join[#, Length[#] + Reverse[#]]&, {0}, 4]
Wolfram Language code: BaseForm[%, 2]

See Also

NestList  NestGraph  NestWhile  Fold  Function  FixedPoint  Do  Composition

Tech Notes

    ▪
  • Applying Functions Repeatedly

Related Guides

    ▪
  • Functional Iteration
  • ▪
  • Functional Programming
  • ▪
  • Iterated Maps & Fractals
  • ▪
  • Looping Constructs
  • ▪
  • Scientific Models
  • ▪
  • Language Overview
  • ▪
  • Procedural Programming

Related Links

  • An Elementary Introduction to the Wolfram Language : Applying Functions Repeatedly
  • An Elementary Introduction to the Wolfram Language : Debugging Your Code
  • NKS|Online  (A New Kind of Science)

History

Introduced in 1988 (1.0) | Updated in 1996 (3.0) ▪ 2024 (14.1)

Wolfram Research (1988), Nest, Wolfram Language function, https://reference.wolfram.com/language/ref/Nest.html (updated 2024).

Text

Wolfram Research (1988), Nest, Wolfram Language function, https://reference.wolfram.com/language/ref/Nest.html (updated 2024).

CMS

Wolfram Language. 1988. "Nest." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/Nest.html.

APA

Wolfram Language. (1988). Nest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Nest.html

BibTeX

@misc{reference.wolfram_2026_nest, author="Wolfram Research", title="{Nest}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/Nest.html}", note=[Accessed: 12-November-2026]}

BibLaTeX

@online{reference.wolfram_2026_nest, organization={Wolfram Research}, title={Nest}, year={2024}, url={https://reference.wolfram.com/language/ref/Nest.html}, note=[Accessed: 12-November-2026]}

Top
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.