Suppose we have a language $\mathfrak{L}=\{\bf{C},\bf{P},\bf{F},\#\}$ consisting of constant, predicate, function symbols and an arity symbol for functions, $\#$, with alphabet $\Sigma(\mathfrak{L})$. We also include in this language all the logical symbols: conjunction, negation etc. as well as punctuation symbols: parentheses, commas ets.
Let the class of terms of $\mathfrak{L}$, denoted $Terms_{\mathfrak{L}}$, be defined as the least subset $X$ of $\Sigma(\mathfrak{L})^*$ such that
1) $X$ contains the variables and constant symbols of the language $Var(\mathfrak{L})\cup \bf{C} \subset X$.
2) $f\frown ( \frown t_1\frown...\frown t_n\frown) \in X$ whenever $t_1, ..., t_n \in X$.
Note: $^*$ is the kleene star, and $\frown$ denotes concatenation. I'm no expert, but I believe this is a standard way of defining a formal language.
Suppose we are in the universe of sets, so every element of the language (i.e. a sequence of symbols concatenated) is a set (here is one way to do this: Codification of a formal language in set theory.) I want to make a characteristic function for the class $Terms_{\mathfrak{L}}$, i.e. a function that returns 1 for every element of $\Sigma(\mathfrak{L})^*$that satisfies conditions 1) and 2) above, and returns 0 for all others. In other words, I want to construct the set of terms of this language.
Clearly, by condition 1) all constants and variable symbols are terms, so the difficulty here is in identifying all the terms of the form $f\frown ( \frown t_1\frown...\frown t_n\frown)$ for arbitrary $n \in N$
Naturally, the characteristic function on $Terms_{\mathfrak{L}}$ must be recursive. I have very little experience with recursion, so I'm not sure how to proceed. If I fix $n$ and take the string $f\frown ( \frown t_1\frown...\frown t_n\frown)$, then I know it is a term because it has parentheses and commas separating the terms $t_i$, and the first element of the string is a symbol from the set $\bf{f}$. But how can my characteristic function tell that a string looks like this for any arbitrary string?