How to resolve the algorithm Mutual recursion step by step in the Smalltalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Mutual recursion step by step in the Smalltalk programming language
Table of Contents
Problem Statement
Two functions are said to be mutually recursive if the first calls the second, and in turn the second calls the first. Write two mutually recursive functions that compute members of the Hofstadter Female and Male sequences defined as:
(If a language does not allow for a solution using mutually recursive functions then state this rather than give a solution by other means).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mutual recursion step by step in the Smalltalk programming language
Source code in the smalltalk programming language
|F M ra rb|
F := [ :n |
(n == 0)
ifTrue: [ 1 ]
ifFalse: [ n - (M value: (F value: (n-1))) ]
].
M := [ :n |
(n == 0)
ifTrue: [ 0 ]
ifFalse: [ n - (F value: (M value: (n-1))) ]
].
ra := OrderedCollection new.
rb := OrderedCollection new.
0 to: 19 do: [ :i |
ra add: (F value: i).
rb add: (M value: i)
].
ra displayNl.
rb displayNl.
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the BASIC programming language
You may also check:How to resolve the algorithm Koch curve step by step in the C programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the AWK programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Component Pascal programming language