Converting Python functions with multiple statements to lambdas

50 Views Asked by At

I'm completely new to Python and I was given a task to rewrite two Python functions using an Y combinator. To this end, I have to transform them into lambdas first, and here's the problem - they both have multiple statements and I'm having troubles with making them lambda compatible. I've been reading about using tuples or lists, but that doesn't seem to be working in my case.

def count(word_list, stopwords, wordfreqs):
    if word_list == []:
        return
    else:
        word = word_list[0]
        if word not in stopwords:
            if word in wordfreqs:
                wordfreqs[word] += 1
            else:
                wordfreqs[word] = 1
        count(word_list[1:], stopwords, wordfreqs)

def wf_print(wordfreq):
    if wordfreq == []:
        return
    else:
        (w, c) = wordfreq[0]
        print(w, '-', c)
        wf_print(wordfreq[1:])

For instance, I tried to write the last function as the following lambda:

wf_print = (lambda wordfreq:
    None 
    if wordfreq == [] 
    else 
    [None, (w, c) = wordfreq[0], print(w, '-', c), wf_print(wordfreq[1:])][0]
)

But there is syntax error with the assignment in the list.

Could you point me in the right direction when it comes to both of these functions?

0

There are 0 best solutions below