[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
You have 30 seconds to justify why array indices shouldn't
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 62
Thread images: 4
File: lua.gif (5 KB, 256x255) Image search: [Google]
lua.gif
5 KB, 256x255
You have 30 seconds to justify why array indices shouldn't start with 1.
>>
>>53335003
You have 30 seconds to justify why they shouldn't start at -1
>>
It fits better with hardware values starting at 0.
>>
>>53335058
Fortunately, Lua allows both.
>>
Protip: Lua lets you start at any number, or character, or string.
>>
Why not compromise and make it 0.5?
>>
>>53335003
base+offset
>>
I can take longer than 30 seconds and OP will never know
>>
>>53335003
because fuck you faggot
>>
>>53335221
It's an honor system.
>>
Everytime I need to iterate through an array with a for loop it saves me one character.
>>
I don't know.
i prefer offsets?
>>
It's wasteful
>he wants len(array) to be at most 255
>>
>>53335003

Why does it matter? Lua itself doesn't care - it just so happens that its standard library uses 1 as the first index. If you write your own functions (why not, it's just C), you can use 0, -1, or whatever the fuck you want.
>>
>>53335289
how?
>>
You have 30 seconds to justify not starting programming with C.

Because if you did, you would know why array indices start with 0.

Fucking children.
>>
>>53335003
0x00

>>53335102
That's a key.
>>
>>53335400
for (int i = 0; i < array.Length; i++)
Vs.
for (int i = 1; i <= array.Length; i++)
>>
>>53335406

Start with C? Are you nuts?

If you didn't start with QBasic, get the fuck out of here.
>>
>>53335289
>>53335439
>using loops

Grandpa, please
>>
>>53335003
iterate a matrix stored as a 1d array
>>
>>53335454
> not using loops
OK gramps
>>
>>53335454
>i use a language so abstracted from the actual code that i have no idea what it is actually doing
Fizzbuzz me.
>>
>>53335102
Does the stdlib assume arrays start at 1 though? Forgive me if I've misremembered, I've only used lua on a summer weekend trying out love2d.
>>
>>53335003
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

Apparently Dijkstra favors ordinals starting at 0. I'm kinda conflicted, because I feel it's more intuitive to start with 1, but at the other hand Dijkstra's pretty based. Nullius in verba and all that,but it would be childish to disregard an established scientist like Dijskstra.
>>
>>53335512
dijkstra is a very smart man but too many people suck his dick.
>>
>>53335003
Also, make no mistake, that's probably not why C adopted base+offset.
>>
>>53335454
autism
you have
>>
>>53335454
...
>>
>>53335485
>i use a language so abstracted from the actual code that i have no idea what it is actually doing

But a function tells you significantly more about what will happen compared to a loop. With a map/fold/filter function, I know exactly what will happen and exactly what will not happen without even looking at anything beyond the first word.
>>
>>53335620
What the fuck are you on about?
Do me a favor and write a FizzBuzz pls.
>>
>>53335620
>map/fold/filter
aren't all of these things loops?
>>
>>53335657
he's one of the blubs who thinks C is archaic but doesn't even understand how his "functional" code even works!
>>
>>53335406
I started with Scheme because my dad said it was good.

p.s. my dad is john carmack
>>
File: option base 1.jpg (120 KB, 770x522) Image search: [Google]
option base 1.jpg
120 KB, 770x522
>>53335088
and so does Basic, at least since... 30 years ago
>>
>>53335656
Pseudo-code to prevent language wars when this should be about concepts only. I'll try to shit post less in this post.

[1..100].foreach(print fizzbuzz)

fizzbuzz(x) {
x % 15 == 0 -> "fizzbuzz"
x % 5 == 0 -> "fizz"
x % 3 == 0 -> "buzz"
else -> x
}


So there's the foreach function. In terms results, it's the same as the loop, but with the properties I've described above. In addition, I don't need to care about what starting index the language uses.

There are rare occurrences where I find a loop less cumbersome, but the vast majority of the cases, I don't find any benefit, but do find drawbacks.

>>53335657
They can be loops or recursion. I guess I should clarify that I'm not arguing against the concept of looping, but rather that for loops aren't very good for looping in most cases (that is to say, there are still some cases where they're preferred).
>>
>>53335869
>literally just a for loop calling a function
>>
>>53335657
Yes. The idea isn't that it does anything you can't do in another way, but that it expresses intent better
compare:
return reduce(add, map(parse, strings)) 

to
sum = 0
for (i = 0; i < length(strings); i++)
sum += parse(strings[i])

They do the same thing, but B forces you to think about what the initial values are and how you iterate over the list of strings
>>
>>53335928
did you even read anything I wrote?
>>
>>53335942
I read your code.
>>
>>53335003
Because fuck you, pig bastard.

0 best fist index
>>
>>53335978
>letting emotion affect your decisions regarding fucking machines
pleb
>>
>>53335522
I know. Also, in any case, you should consider which way's safer and more intuitive.
>>
>>53335929
No, it forces you to adopt that particular way of doing things to the point that it becomes automatic, you stop thinking about it at some point. A language should help you avoid mistakes. EVERYONE makes mistakes. Attention is a finite resource.
>>
>>53335959
So then I'll just assume you agree with me, then.
>>
>>53336080
All I see is that you fell back to a loop immediately.
>>
>>53336045
>No, it forces you to adopt that particular way of doing things to the point that it becomes automatic
I don't see why that's a good thing when I could just _not_ do that. And of course, as the domain become more complex and specialized then you lose that muscle memory. Here's a new prompt: Given a list of numbers as strings [a, b, ... z] return the result of a - b ... - z
>A language should help you avoid mistakes. EVERYONE makes mistakes. Attention is a finite resource.
Agreed. I don't see how that advances your argument in any way.
>>
>>53335869
That pseudocode syntax has me rock hard
>>
>>53336115
Read the bottom of my post. I never had any intention of arguing against the concept of a loop, only the over-use of for loops.

As you'll notice, I didn't use a for loop. I'm not exactly sure what you're trying to argue.
>>
>>53336197
But you did use a for loop, it is just under a different notation.
>>
>>53336211
And that for loop is a jump instruction under a different notation. I might be wrong, but I assume you prefer a for loop to a goto statement.
>>
>>53336211
This is "functional paradigm". For-loops are mostly "imperative paradigm".

Stop posting and pick up any functional programming language, e.g. Haskell or Scheme, before commenting any further. Learning one of those is one of biggest enlightenment in my life.
>>
>>53335406
careful
>>
File: 1452918673328.jpg (97 KB, 562x560) Image search: [Google]
1452918673328.jpg
97 KB, 562x560
>>53335440
tfw started with QBASIC
>>
File: 1305139527817.jpg (21 KB, 202x204) Image search: [Google]
1305139527817.jpg
21 KB, 202x204
>>53335003
You also have 30 seconds (not 29) to justify why array indices should not be inclusive-inclusive
>>
>>53336269
>>53336355
Not the one originally debating, but if there's any benefit to that style the example provided is shit as it only serves to add additional and redundant lines of code.
>>
>>53336145
>I don't see why that's a good thing when I could just _not_ do that
I wasn't implying that's a good thing, quite the contrary.
>Agreed. I don't see how that advances your argument in any way.
My argument was that juggling array indices is error-prone. It does support my point; you will make mistakes.
>>
>>53336172
What about this pseudocode syntax, does it tickle your pickle?

main = map (putStrLn . fizzbuzz) [1..100]

fizzbuzz x =
case (mod x 5, mod x 3) of
(0, 0) -> "fizzbuzz"
(0, _) -> "fizz"
(_, 0) -> "buzz"
_ -> show x
>>
>>53336145
>Given a list of numbers as strings [a, b, ... z] return the result of a - b ... - z

let numbers = map read strings
foldl (-) 0 numbers


or more compactly:

subtractEverythingInList = foldl (-) . map read
>>
>>53335003
a[i] == *(a+i)
>>
>>53335003
Because arrays start at a memory location pointed to by the variable, and adjust based on that location.
>>
Using array indices starting at 0 means you calculate the location in memory as a base plus an offset. Using array indices starting at 1 means you calculate the location in memory as a base plus an offset and minus 1. There's an additional instruction being used to calculate an offset.

Also, nearly every language these days using offset 0 as the first. It has become a standard, and going against common language standards leads to silly things like left associative ternary operators.
Thread replies: 62
Thread images: 4

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.