The interest of musicians and computer scientists in AI-based automatic melody harmonization has increased significantly in the last few years. This research area has attracted the attention of both teachers and students of Theory, Analysis and Composition, looking for support tools for the learning process. The main problem is that the systems designed and developed up to now harmonize a melody written by a user without considering the didactic and therefore cognitive aspects at the basis of a "significant learning": given a melody, the system returns a harmonization finished without any user input. This paper describes a self-learning algorithm capable of harmonizing a musical melody, with the aim of supporting the student during the study of Theory, Analysis and Composition. The algorithm, on the basis of the ascending and descending movement of the sounds of the melody (soprano), proposes the sounds for the bass line: the Viterbi algorithm was applied to evaluate the probability of the bestmatch between themelody sounds and the provided Markov chains, to reach the "optimal" state sequences. Subsequently, the algorithm allows the user to complete the chords for each sound of the bass line (tenor and alto), or to create the complete chords. Examples of musical fragments harmonized in thisway demonstrate that the algorithm is able to respect the concatenation rules of the tonal functions which characterize classical tonal music.