73
\$\begingroup\$

First LOLCODE program, run here. This program inputs a Fibonacci number from the user, then determines and prints whether the number is in the Fibonacci series. What do you think?

HAI 1.2

    HOW IZ I GetNum

        VISIBLE "Entr yr numbr: "

        I HAS A WatzNum
        GIMMEH WatzNum

        FOUND YR WatzNum

    IF U SAY SO

    HOW IZ I ProovFibo YR Num

        I HAS A Fibo1 ITZ 1
        I HAS A Fibo2 ITZ 2

        IM IN YR Nums

            BOTH SAEM Num AN Fibo1, O RLY?
                YA RLY
                    VISIBLE "YEAH, FIBONACCI NUM!"
                    GTFO
                NO WAI
            OIC

            BOTH SAEM Num AN Fibo2, O RLY?
                YA RLY
                    VISIBLE "YEAH, FIBONACCI NUM!"
                    GTFO
                NO WAI
            OIC

            BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2
            O RLY?
                YA RLY
                    DIFFRINT Num AN Fibo1, O RLY?
                        YA RLY
                            DIFFRINT Num AN Fibo2, O RLY?
                                YA RLY
                                    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
                                    GTFO
                                NO WAI
                            OIC
                            GTFO
                        NO WAI
                    OIC
                NO WAI
            OIC

            Fibo1 R SUM OF Fibo1 AN Fibo2
            Fibo2 R SUM OF Fibo1 AN Fibo2

        IM OUTTA YR Nums

    IF U SAY SO

    I HAS A Num
    I HAS A MaxNum ITZ 50
    Num R I IZ GetNum MKAY
    Num IS NOW A NUMBR

    I IZ ProovFibo YR Num MKAY

KTHXBYE
\$\endgroup\$
4
  • 68
    \$\begingroup\$ Only here do we take LOLCODE seriously... \$\endgroup\$
    – RubberDuck
    Feb 2, 2015 at 2:19
  • 7
    \$\begingroup\$ LOLCODE at its finest \$\endgroup\$
    – user54490
    Feb 2, 2015 at 3:22
  • 6
    \$\begingroup\$ @RubberDuck PPCG does too. :P \$\endgroup\$
    – user95591
    Mar 28, 2016 at 17:36
  • \$\begingroup\$ @Riker *CGCC now \$\endgroup\$
    – MilkyWay90
    Jul 28, 2019 at 20:06

2 Answers 2

46
\$\begingroup\$
BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2
O RLY?
    YA RLY
        DIFFRINT Num AN Fibo1, O RLY?
            YA RLY
                DIFFRINT Num AN Fibo2, O RLY?
                    YA RLY
                        VISIBLE "BOO, IZ NO FIBONACCI NUM!"
                        GTFO
                    NO WAI
                OIC
                GTFO
            NO WAI
        OIC
    NO WAI
OIC

equals this pseudo code

if (Fibo2 >= max(Num, Fibo2))
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else
                }
                break
            else
        }
    else
}

Basically, what you've made has some strange bits.

Let's clean it up.

if (Fibo2 >= max(Num, Fibo2))//weird check, replace with Fibo2 > Num instead
{
    then
        if (Num != Fibo1) {
            then
                if (Num != Fibo2) {
                    then
                        print "BOO, IZ NO FIBONACCI NUM!"
                        break
                    else//remove useless else
                }
                break
            else//remove useless else
        }
    else//remove useless else
}
if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
            break
        }
        break
    }
}

Huh. Well, we don't need a double break.

if (Fibo2 > Num) {
then
    if (Num != Fibo1) { 
    then
        if (Num != Fibo2) { 
        then
            print "BOO, IZ NO FIBONACCI NUM!"
        }
        break
    }
}

Well, neither of these two can be true, due to the way your algorithm works (you do a check for Fibo1 == Num and Fibo2 == Num to determine if it is a fibonacci number). Remove the unneeded checks.

if (Fibo2 > Num) {
then
    print "BOO, IZ NO FIBONACCI NUM!"
    break
}

And translate back to LOLCODE.

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num
O RLY?
YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC

Well, that simplified things!

Indentation

I still don't like the indentation, though. Three lines for a single if statement? Let's combine them:

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num
O RLY?, YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC

This is the minimum I'd do. Even shorter would be:

DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num, O RLY?, YA RLY
    VISIBLE "BOO, IZ NO FIBONACCI NUM!"
    GTFO
OIC

But then we're basically having the brace-style argument again; Put them on a new line or on the next?

LOLCODE internal workings

I confused myself a couple times during the writing of this answer.

For instance, you had a BOTH SAEM Fibo2 AN BIGGR OF Num AN Fibo2 check, but this is what the spec says:

BOTH SAEM <x> AN BIGGR OF <x> AN <y>   BTW x >= y
BOTH SAEM <x> AN SMALLR OF <x> AN <y>  BTW x <= y
DIFFRINT <x> AN SMALLR OF <x> AN <y>   BTW x > y
DIFFRINT <x> AN BIGGR OF <x> AN <y>    BTW x < y

And you have BOTH SAEM <x> AN BIGGR OF <y> AN <x>. So what is your version going to do?

Well, if we split it into two statements...

BIGGR OF <x> AN <y>   BTW Math.max(x, y)
SMALLR OF <x> AN <y>  BTW Math.min(x, y)
I HAZ A RESULT ITZ BIGGR OF <x> AN <y>  BTW RESULT = Math.max(x, y)
BOTH SAEM <x> AN RESULT  BTW x = Math.max(x, y)

Apparently DIFFRINT Fibo2 AN SMALLR OF Fibo2 AN Num is what you need. It basically checks if "the smaller of Fibo2 and Num" is "Fibo2".

\$\endgroup\$
0
26
\$\begingroup\$

Separation of concerns

I like that you extracted ProovFibo... but I find it's doing too many things: I would have expected a function that returns a Boolean (Troof), and does nothing else.. but your procedure is keeping that information all for itself, and takes the responsibility of also outputting the result.

Readability/naming

You are declaring a Num variable, and the ProovFibo procedure is taking a Num parameter... and the loop is named Nums - Sure, naming loops is (AFAIK) very LOLCODE-specific and it's hard, but I'd prefer to see it named Sequence or something; pluralizing the argument name just feels wrong, and the name of the global-scope argument being the same as the local-scope parameter makes it a bit confusing.

While

A LOLCODE loop that isn't UPPIN YR {variable} is essentially a while loop... but you should be only looping up to a certain MaxNum, right? ...but MaxNum is never used!

It looks like the first two conditions (Num vs. Fibo1 and vs. Fibo2) could be combined into one.

Input Validation

The user input is never validated - what happens when user inputs LOL? Or -200? The script happily casts the input into a number - you're leaving it entirely up to the language implementation to determine what happens... and that's not pretty: the program simply crashes with a cast error.

\$\endgroup\$
2
  • 8
    \$\begingroup\$ The parameter should definitely be called MebbeFibo or similar \$\endgroup\$
    – Ben Voigt
    Feb 2, 2015 at 3:30
  • 1
    \$\begingroup\$ Oops, that MaxNum is from back when I restricted it to values below 50 and checked them manually. That value is never used. \$\endgroup\$
    – user34073
    Feb 2, 2015 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.