Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard ML: Iterative vs. Recursive

Tags:

recursion

sml

ml

I'm reading through ML for the Working Programmer and am a bit confused by the author's distinction between iterative and recursive. My understanding was that "recursive" just refers to a function that calls itself. Any function that isn't recursive is iterative (where an iterative algorithm usually involves some kind of looping).

However, in this book, the author will say something like "luckily the obvious recursive solution is iterative." So my understanding of these terms is definitely different from how the author is using them.

Can someone clarify where I'm misunderstanding these terms?

Thanks, bclayman

like image 229
anon_swe Avatar asked Aug 27 '26 09:08

anon_swe


1 Answers

Iterative means you can express it with a loop. However, generally speaking, a loop is just a special case of recursion. For example, in SML, the loop

while A do B

is literally defined as a syntactic abbreviation for the recursive definition

let fun loop() = if A then (B; loop()) else () in loop() end

That's why "iterative" is not understood as the opposite of "recursive", but as a special case: some recursive definitions are iterative, others are not. More specifically, it is a special case of linear recursion, where the recursive definition is invoked at most once.

like image 199
Andreas Rossberg Avatar answered Aug 30 '26 01:08

Andreas Rossberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!