Ralph
Ralph

Reputation: 3039

SML: Does the function foldl take the list predefined, or list could be accumulated during the process?

The following is related to a homework question. Not looking for an answer, looking for a clarification

I am supposed to find the n-th Catalan number which is based on a recurrence rel that is not relevant to my question.

My question is:

The function foldl takes a function, an accumulator, and a list

When I apply foldl, it will look something like that foldl function_name initial_value_for_accumulator list

Our professor says in the question the following:

Implement a function Catalan:int -> int which, when given an integer n, computes the n-th Catalan number. For full credit, use the higher order function foldl in your solution.

Foldl has to take a list. So, the question is: Am I able to pass a list that is being built up during the recursion? or I have to compute the list and then pass it to foldl?

Upvotes: 1

Views: 231

Answers (1)

chi
chi

Reputation: 116174

Using both recursion and foldl is unusual, especially in basic exercises. I believe the professor is telling you to use foldl instead of recursion.

As you state, this requires you to define a list and pass it to foldl. Often, if you need to loop over the numbers from 1 to n, you start from the list of such numbers.

Upvotes: 4

Related Questions