Is one of them supposed to be wrong?


#1

Is there any difference between

def shut_down(s):

if shut_down("yes"):
    return "Shutting down"

and

def shut_down(s):

if s=="yes":
    return "Shutting down"

#2

Hi @f4te ,

Yes, there is an important difference between the two code samples that you posted. This test calls the shut_down function from within the function itself ...

if shut_down("yes"):

Having a function call itself is known as recursion, but you should not use that approach for this exercise. Instead, you should check the value of s, which you have done here ...

if s=="yes":

#3

Some functional languages, like Haskell, have pattern matching which sort of do what you're trying to do with the first example, but Python does no such thing, except perhaps when unpacking values:

(a, b), (c, d) = (3, 8), (9, 2)
print a, b, c, d # 3 8 9 2

But that's not quite the same anyway.

For example, you can define the factorial function like this in Haskell:

factorial 0 = 1
factorial x = x * factorial (x - 1)

So when the argument is 0, the first version is matched and 1 is the result.
x will match a single number, which can be used in computing the result

Or to match the shut_down function..

shut_down "yes" = "Shutting down..."
shut_down _ = "Not shutting down!"

where _ matches anything, and ignores that value.

It's a terrible example though, because Haskell functions don't have side-effects, so it couldn't possibly shut anything down!