If

Bob
Bob in F#
module Bob

let response (phrase: string): string =
    let isEmpty = System.String.IsNullOrWhiteSpace(phrase)
    let isYell = phrase <> phrase.ToLower() && phrase = phrase.ToUpper()
    let isQuestion = phrase.TrimEnd().EndsWith("?")

    if isEmpty then "Fine. Be that way!"
    elif isYell && isQuestion then "Calm down, I know what I'm doing!"
    elif isYell then "Whoa, chill out!"
    elif isQuestion then "Sure."
    else "Whatever."

Handling the different responses

We start out with a function named response that takes a string as its sole parameter and returns a string:

let response (phrase: string): string

Response for silence

If the input is an empty string (signifying silence), the response should be "Fine. Be that way!". To check this, we can use the built-in String.IsNullOrWhiteSpace() method:

let isEmpty = System.String.IsNullOrWhiteSpace(phrase)
Note

We opted for using System.String, but another option would be to open the System namespace and then we could omit the System. prefix for the String.IsNullOrWhiteSpace call:

open System

let isEmpty = String.IsNullOrWhiteSpace(phrase)

If you were to use multiple types from the System namespace, we'd recommend using the above approach where the namespace is explicitly opened.

Now that we can determine whether a phrase is empty, we can return the proper response using an if expression:

if isEmpty then
    "Fine. Be that way!"

Nice! Let's move on to the next type of phrase.

Response for yell

If the input's letters are all in uppercase, and there is at least one letter, then the phrase is considered to be a yell. We can check if calling ToUpper() on the phrase doesn't introduce any changes, which means that every letter was already in uppercase. However, it could be that there weren't any letters, so to handle that we can use ToLower(), and see if that is not equal to the phrase (which means that there were uppercase letters):

let isYell = phrase <> phrase.ToLower() && phrase = phrase.ToUpper()

The correct response can be returned via:

elif isYell then
    "Whoa, chill out!"

Response for question

If the input ends with a question mark, it is considered to be a question. We can use the EndsWith() method for that:

let isQuestion = phrase.EndsWith("?")

This doesn't pass all the tests though, as we need to ignore any trailing white space. The fix for that is easy: first remove the trailing white space using TrimEnd():

let isQuestion = phrase.TrimEnd().EndsWith("?")

The correct response can then be returned via:

elif isQuestion then
    "Sure."

Response for yelled question

Due to the fact that we have separate isYell and isQuestion bindings, checking for a yelled question is a simple as:

elif isYell && isQuestion then
    "Calm down, I know what I'm doing!"

Note that we need to check this before checking of the yell or question responses, as otherwise this will never match.

Default response

Finally, we'll need to return the default response, which we can do via:

else "Whatever."

Putting it all together

Putting our if/elif/else together gives us the following code:

if isEmpty then
    "Fine. Be that way!"
elif isYell && isQuestion then
    "Calm down, I know what I'm doing!"
elif isYell then
    "Whoa, chill out!"
elif isQuestion then
    "Sure."
else
    "Whatever."

Shortening

We can shorten this code a bit by putting the returned string values on the same line as the if/elif/else keywords:

if isEmpty then "Fine. Be that way!"
elif isYell && isQuestion then "Calm down, I know what I'm doing!"
elif isYell then "Whoa, chill out!"
elif isQuestion then "Sure."
else "Whatever."

Final code

And with that, we have an implementation of the response function that passes all the tests:

let response (phrase: string): string =
    let isEmpty = System.String.IsNullOrWhiteSpace(phrase)
    let isYell = phrase <> phrase.ToLower() && phrase = phrase.ToUpper()
    let isQuestion = phrase.TrimEnd().EndsWith("?")

    if isEmpty then "Fine. Be that way!"
    elif isYell && isQuestion then "Calm down, I know what I'm doing!"
    elif isYell then "Whoa, chill out!"
    elif isQuestion then "Sure."
    else "Whatever."
Note

We've defined the isEmpty, isYell and isQuestion bindings within the response function, as they're only used within that function.

11th Sep 2024 · Found it useful?