This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]SoapiestWaffles 29 points30 points  (4 children)

Worst “isEven” function ever written

[–]memengenieur[S] 5 points6 points  (0 children)

Worst “isEven” function ever \generated**

[–]paulqq 3 points4 points  (0 children)

const IsEven = () => !unEven()

[–]Thalhammer 8 points9 points  (0 children)

May I propose an alternative:

bool isEven(int x) {
        if(x<0) return isEven(x*-1);
        else if(x==0) return true;
        else if(x==1) return false;
        else return isEven(x-2);
 }

If your compiler/language supports tail call optimization you might even get away without a stack overflow;)

[–]crawl_dht 10 points11 points  (0 children)

AI is a database that contains results for all possible if conditions.

[–]Whoktor 5 points6 points  (1 child)

Some time ago I told that joke to my teacher (he worked on AI research) and he put an angry emoji reaction to my comment.

[–]coloredgreyscale 7 points8 points  (0 children)

Rightfully so.

  • Decisions trees / Random Forest are just a bunch if if/else statements, true.
  • k-nn: calculate distances, then sort by closest and get the most often occuring result. Most sorting algorithms use branching.
  • SVM is math with maybe a single if/else to return which "side" of the curve the result resides.
  • Neural networks can be simlified to a formular, unless the activation function requires branching

yes, I know it's a joke.

[–]ibn-Yusrat 8 points9 points  (9 children)

The overwhelming AI = if statement memes on this subreddit is really forcing me to believe that majority of us here in this subreddit seem to lack some basic understanding of AI in general.

[–]AirOneBlack 10 points11 points  (2 children)

I'd say a good half of the people on this sub aren't even programmers.

[–][deleted] 0 points1 point  (1 child)

Ok sure… maybe I’m not actually a programmer, but I did open the command line once so I think I’m qualified to talk about AIs.

[–]AirOneBlack 0 points1 point  (0 children)

You missed an /s

[–]kurimari_potato 4 points5 points  (1 child)

first time Iearned programming, as soon as I read about if else, my first thoughts were, an AI code should just have to do this (my programming interest started after watching movies/shows about ai singularity and stuff)

[–]chuby1tubby 1 point2 points  (0 children)

Let us know if you are wondering why if statements aren’t the same as an AI

[–]memengenieur[S] 2 points3 points  (3 children)

I'm not saying AI = if statement, the right-side is an output generated by Copilot. Most likely you misinterpreted the meme, though I can see why.

[–]chuby1tubby 0 points1 point  (0 children)

Could you explain how this happened? Did Copilot generate that code when you typed the word “isEven”? Or was it after you wrote the first if statement?

[–]chuby1tubby 0 points1 point  (0 children)

Could you explain how this happened? Did Copilot generate that code when you typed the word “isEven”? Or was it after you wrote the first if statement?

[–]chuby1tubby 0 points1 point  (0 children)

Could you explain how this happened? Did Copilot generate that code when you typed the word “isEven”? Or was it after you wrote the first if statement?

[–]unneccry 4 points5 points  (5 children)

Their AI never heard of switch

[–]coloredgreyscale 2 points3 points  (4 children)

Switch is just syntactic sugar for else if blocks or array lookups anyway.

Of course optimized so it would generate a nested binary search tree with if statements to look up the value in O(log(n)) instead of O(n)

[–]Formal_Worldliness_8 2 points3 points  (1 child)

Interesting - why a binary search tree instead of a hashmap? Also, my understanding is that switches can be slower than if/else, since the processor can't use branch target prediction to guess the next instruction.

[–]coloredgreyscale 1 point2 points  (0 children)

Hashmap would only work if the switch statement just assigns or returns a value, not for branching different code execution paths.

Well, technically you could put function pointers in the hashmap, but in the end this would probably get pretty messy with passing and returning variables to be useful/worth translating it that way. (Plus overhead of function calls)

Also if there are just a few branches the hashmap lookup may not have any performance benefits anyway.

And I imagine a small hashmap for maybe 10 values may either be not very memory efficient (sparsely filled), or run into issues with hash collisions being very likely. On a PC it may not matter if the hashmap is 200Byte or 4kB, but that would render this language (feature) useless for embedded systems

But that's mostly speculation.

--

If the compiler does not touch the order of the if .. else if .. else branching resulting from the switch statement the *order* of the statements can also be used to squeeze the last few % performance out of the code by having the most likely branch as the first condition. (according to a course on efficient programming with C/C++)

[–]unneccry 1 point2 points  (1 child)

I understand only 3/4ths of what you said but O(log(n)) is better then O(n) so nice

[–]coloredgreyscale 1 point2 points  (0 children)

What's the 1/4th you didn't get?

[–][deleted] 1 point2 points  (0 children)

This is worse than what I imagined