Iteration

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Wiki Markup
{style}
cite{font-family:Courier New; font-style:normal}
{style}

Loops

...

and

...

recursion

...

are

...

equivalent

...

a

...

language

...

with

...

only

...

one

...

is

...

just

...

as

...

powerful

...

(meaning

...

it

...

can

...

solve

...

as

...

many

...

problems,

...

no

...

comment

...

about

...

speed)

...

as

...

one

...

with

...

both.

...

The

...

functional

...

paradigm

...

encourages

...

using

...

recursion,

...

but

...

there

...

are

...

times

...

when

...

iteration

...

is

...

simpler

...

or

...

faster.

...

Recursion

...

vs

...

For

...

Loop

...

In

...

the

...

Julia

...

set

...

example

...

,

...

we

...

use

...

recursion

...

to

...

build

...

a

...

3-dimensional

...

list

...

to

...

represent

...

an

...

image.

...

This

...

function

...

is

...

provided

...

on

...

the

...

left.

...

On

...

the

...

right

...

is

...

an

...

alternative.

...

It

...

creates

...

a

...

2-dimensional

...

array

...

and

...

iteratively

...

assigns

...

a

...

3-list

...

to

...

each

...

cell.

...

Wiki Markup
{table}{tr}{td}{code: title=Recursion}
(defun calculateMatrix (rows cols)
  (labels ( 
    (calcMatrix-util (i rows cols)
      (if (= i rows)
        '()
        (cons (calculateRow i 0 rows cols)  
              (calcMatrix-util (+ i 1) rows cols) )
      ))
      (calculateRow (i j rows cols)
        (if (= j cols)
          '()
          (cons (getColor (+(* (/ i rows) 3) -1.5)  
                          (+ (* (/ j cols) 3) -1.5) )
                (calculateRow i (+ j 1) rows cols) )
        ) 
      ))
   (calcMatrix-util 0 rows cols) )
)
{code}{td}{td}{code: title=Iteration}



(defun calcMatrix-loop (rows cols)
  (let ((image (make-array (list rows cols)) ))
    (loop for r from (- rows 1) downto 0 do
      (loop for c from (- cols 1) downto 0 do
        (setf (aref image r c) 
              (getColor (+(* (/ r rows) 3) -1.5)  
                        (+ (* (/ c cols) 3) -1.5) ))
      )
     )
    image)
)




{code}{td}{tr}{table}

Though

...

these

...

two

...

functions

...

look

...

very

...

different,

...

they

...

use

...

approximately

...

the

...

same

...

amount

...

of

...

memory.

...

The

...

CMUCL

...

implementation

...

of

...

Lisp

...

prints

...

out

...

the

...

garbage

...

collection

...

events

...

as

...

they

...

occur,

...

and

...

both

...

functions

...

require

...

about

...

50

...

collections.

...

This

...

is

...

not

...

to

...

say

...

that

...

all

...

forms

...

of

...

iteration

...

are

...

equal.

...

The

...

CMUCL

...

manual

...

points

...

out

...

that

...

apply

...

and

...

mapcar

...

,

...

which

...

are

...

functions

...

to

...

iteratively

...

modify

...

the

...

contents

...

of

...

an

...

array

...

or

...

list,

...

are

...

slower

...

than

...

some

...

iterative

...

alternatives.

...