Filter a list using DA.List.filter

Hi everyone! I’m new to daml and I’m working through the tutorial at this link: https://daml.talentlms.com/. I’m currently on lab 2 question 2. The prompt is:

Write a function named termFrequency that takes a word and a string of words and returns the frequency of that word in the string. For example,

debug $ termFrequency “long” “a long long way to run”

should print

0.3333333333

I was able to get the string into a list and the length of the list, but I can’t seem to use the filter function properly to get the list to be just the word I’m looking for. The closest I’ve been able to get so far is this:

wordList = “a long long way to run”
sepWords = words(wordList)
filtList = DA.List.filter(\x → x.sepWords /= “long”)

Also, for learning purposes I think an answer key for the labs would be super helpful as well as maybe putting examples of how the functions are used in the documentation or at least the proper syntax of how to apply the functions.

Hi @austin.hodges,

Searching on Hoogle, you can find the documentation for the filter function, which says:

Filters the list using the function: keep only the elements where the predicate holds.

“The predicate holds”, in case you’re not familiar with this kind of verbiage, is a fancy way of saying the function you give to filter returns True for that element.

Does that help? If not, please let me know and I’ll try to find another take on this.

1 Like