[HELP] Single questions & Help Thread [n00b] (66)

1 Name: chester copperpot : 2008-04-30 00:02 ID:Heaven

Why do we need a method to determine if a stack is empty?

2 Name: #!/usr/bin/anonymous : 2008-04-30 01:51 ID:pPcLAyBH

>>1
Context, numb-nuts? Comprende? Now give me 20 push-ups and a reworded question.

3 Name: #!/usr/bin/anonymous : 2008-04-30 02:12 ID:Heaven

>>2
context: stack implementation

4 Name: #!/usr/bin/anonymous : 2008-05-01 07:55 ID:nEXP+bJr

why not? makes things clearer.

5 Name: #!/usr/bin/anonymous : 2008-05-03 13:51 ID:V7mp15x0

Can anyone explain to me what side-effects are?
I read about them in wikipedia, but I'm still kinda confused.
Are they bad, good, why do we care about them? A nice little practical example would be much appreciated.

6 Name: #!/usr/bin/anonymous : 2008-05-03 14:36 ID:PGHeLRr1

>>5
side effects are things like changing the state of some variable in a program and IO. Without side effects, the order in which you execute two expressions, s1 and s2, makes no difference. They aren't good or bad per se. Purely functional languages don't have side effects.

This: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-19.html#%_chap_3 is a good read too.

7 Name: #!/usr/bin/anonymous : 2008-05-03 15:46 ID:mxqboWJt

>>6

They must have some side effects or they would never interact with the anything outside of themselves. You just side IO was a side effect.

8 Name: #!/usr/bin/anonymous : 2008-05-03 22:12 ID:PGHeLRr1

>>7

Well... you don't really need a print statement, the result of whatever function you evaluate in the repl will just be returned to the interpreter and printed. But yeah, for a practical language, you need IO.

9 Name: #!/usr/bin/anonymous : 2008-05-03 23:50 ID:Heaven

>>8
For a practical language, you need a lot more than just IO.
You need a system that allows side effects (look at monads in haskell)
Also, C doesn't have actual IO. It's all file streams writing/reading.
You can make all read calls to return EOF, and all write calls to return success without doing anything.
Replace fopen() calls with NULL macros, etc etc, and your implementation would still be valid. Not practical, but C allows it.

10 Name: CSS Question : 2008-05-04 18:46 ID:V7mp15x0

How do I define a CSS rule for an HTML element with just an id and not a class?

11 Name: #!/usr/bin/anonymous : 2008-05-04 23:06 ID:5o1YB8rR

>Why do we need a method to determine if a stack is empty?

Otherwise the program might allow the user to try to take another element out, which might make the program end up in weird memory locations that it shouldn't be.

12 Name: #!/usr/bin/anonymous : 2008-05-04 23:06 ID:5o1YB8rR

>Why do we need a method to determine if a stack is empty?

Otherwise the program might allow the user to try to take another element out, which might make the program end up in weird memory locations that it shouldn't be.

13 Name: #!/usr/bin/anonymous : 2008-05-04 23:49 ID:Heaven

>>12
well, that was easy.

14 Name: #!/usr/bin/anonymous : 2008-05-05 00:19 ID:Heaven

>>10

#id { text-decoration: blink; }

15 Name: #!/usr/bin/anonymous : 2008-05-18 02:42 ID:R8Zq53Q9

Run in the Python interpreter:

>>> 2 / (2.0+3)

0.40000000000000002

How did the .00000000000000002 get into that float?

17 Name: #!/usr/bin/anonymous : 2008-05-18 06:08 ID:Heaven

Run in the clisp interpreter:
(/ 2.0 (+ 2.0 3))
0.4

How is the answer accurate? :P

18 Name: #!/usr/bin/anonymous : 2008-05-18 14:00 ID:Heaven

>>17
Either because it's truncating the number before the 16th decimal place, or because it uses a much slower method for calculating floating-point which doesn't use the native FPU.

19 Name: #!/usr/bin/anonymous : 2008-05-24 21:13 ID:Heaven

I need a little help with Perl. I was studying it recently, read few books and reached the starter or so level, but there is one question bothering my mind: What is the use of the -> symbol? Some of you might refer me to C, but being a C begginer myself I don't recall anything similar to that.
From my understanding, the -> symbol dereferences - but here's my problem, I understand the uses, but how would I use it and why would I need to use it? So far I haven't encountered the need of using complex structures in my code, but this symbol bothers me for some reason, and I won't have a clue what to do if I encounter it in the future. Please present me the uses, the causes and the hows of the -> symbol. Thanks!

20 Name: #!/usr/bin/anonymous : 2008-05-24 21:14 ID:/rb+P5XG

Saged by mistake, bump.

21 Name: #!/usr/bin/anonymous : 2008-05-24 21:40 ID:Heaven

>>19

Normally, it's mostly used for OO code. $obj->method($stuff).

You can also do it for other things, like $hashref->{stuff} instead of $$hashref{stuff}. But then again you could just use the latter if you prefer. That kind of code shows up if you have, say, an array of hashrefs and iterate over it. For instance:

my @array;
$array[0]{name}="Stuff";
$array[0]{value}=1;
$array[1]{name}="Things";
$array[1]{value}=2;

foreach my $hashref (@array)
{
print $hashref->{name}; # These two lines are the same.
print $$hashref{name}; #
}

22 Name: #!/usr/bin/anonymous : 2008-05-24 22:36 ID:/rb+P5XG

Why do you need references in your code? How does it help me? What is their purpose?

23 Name: #!/usr/bin/anonymous : 2008-05-25 00:31 ID:Heaven

>>22

In Perl, you can't put an array inside another array, or a hash in an array, and so on. You have to use references to them instead.

Also, because the arguments to a function are an array, you can only give a single array or hash in the argument list unless you use references.

24 Name: #!/usr/bin/anonymous : 2008-05-25 11:37 ID:1ifcRx/v

>>23
I see, interesting, the latter could be fixed with some shift functions, though.
So far I haven't encountered the need of using such code, but I will probably use it when the need shows up. I shouldn't worry about it as long as I don't need it, should I?

25 Name: #!/usr/bin/anonymous : 2008-05-25 18:27 ID:Heaven

>>22,24

Another use, similar to what >>23 said, is returning multiple arrays or hashes from a function. You cannot return two or more arrays from a function in Perl without references. By default Perl will smash them all together into one array.

References can also be useful for efficiency. If you are passing a large data structure around between functions, it's going to be more efficient to do so with references.

26 Name: PHP QUESTION : 2008-06-09 03:06 ID:V7mp15x0

I assumed that the keyword private, and similarly protected, were
meant to do more than just prevent a class member from being modified
from outside of the class (by foreign functions).
The inability to print or copy a private property's value implies that
the value is hidden, and therefore not expected to be visible from
outside of the class.
Yet, any value of an object, whether private or protected, can be
easily be seen by converting the object to an array. The array not
only holds the values, but also the names of each member.
Does every OOP language allow such easy access to private properties?

function get_object_values( $any_object ) {
$values = (array)$any_object;
return (array_values( $values ));
}

27 Name: #!/usr/bin/anonymous : 2008-06-09 13:35 ID:8LZSGSWU

>>26

Yes. With CL it's the SLOT-VALUE accessor, with C++ simply -Dprivate=public -Dprotected=public, with Smalltalk, use Smalltalk at:put:.

The inability to print or copy a private property is a delusion. Protected/private members serve no useful purpose except to make programming more difficult.

28 Name: #!/usr/bin/anonymous : 2008-06-11 22:22 ID:V7mp15x0

>>27
I guess OOP is more about design than security.
I did figure out how to prevent that >>26 from working.
If you declare members of a class static and private and use protected methods to handle them, then you can prevent their visibility (in this case).

<?php
class foo {

  private static $key = "password";
  protected function show() {
echo( self::$key );
}

}

$obj = new foo();
$a = (array)$obj;
echo $a; // An empty array gets printed
echo foo::$key; // Fatal error
echo $obj->key; // Fatal error
foo::show(); // Fatal error
$obj->show(); // Prints key as expected
?>

So it seems that $key is nicely hidden this way.

29 Name: #!/usr/bin/anonymous : 2008-06-11 23:21 ID:8LZSGSWU

>>28

Unfortunately, static will make them class members rather than instance members, which is probably not what you want.

Privacy is a delusion. Make everything public and stop worrying about it so much.

30 Name: #!/usr/bin/anonymous : 2008-06-11 23:59 ID:V7mp15x0

>>29
that's true but there still is no way to access those members or methods from outside of the class. The only way to access those members is through an instance of that class. This is especially true if I were to declare the class as final. So, really, this is what I want.

31 Name: #!/usr/bin/anonymous : 2008-06-12 12:19 ID:FIoHC8bz

>>30

> The only way to access those members is through an instance of that class.

Static members do not belong to any instance, they belong to the entire class, so every instance of the class will share the same value for that member. Whether or not the class is final wouldn't effect this.

32 Name: #!/usr/bin/anonymous : 2008-06-12 13:54 ID:V7mp15x0

>>31
you're right, that sucks. i guess privacy really is a delusion.

33 Name: #!/usr/bin/anonymous : 2008-06-16 17:20 ID:8LZSGSWU

>>32

Why does it suck? What exactly are you trying to accomplish?

34 Name: #!/usr/bin/anonymous : 2008-08-01 05:20 ID:94xnnzPm

Okay - this question is kind of a rant. When I was learning to use C++ in university, you could just compile a program, and switch it from computer to computer and the application would still run fine. fast forward a couple of years - i'm using the express edition of Visual C++, everything's fine and dandy on my comp, but try to take it somewhere else and BLAM! Application Configuration file is incorrect! as far as i could tell from the internet visual now does some weird thing where you need to include a bunch of dll's or something with you appliation to make it run elsewhere. There were vague answers on how to get those files in your project, but any time someone mentioned a way to just make the linking "static" (which as far as I understand means that the dlls are just embedded in your exe) they never really mentioned how. when i think i figured it out, it just gave me a bunch of linking errors.

Anyway, here's the real question: DO any of you know how to make an app you've bui;t with Visual Studio '08 express to just be standalone and work on any machine capable of running exe's? if not, do you use some kind of MAGICAL compiler that doesn't do this whacked out shit? does Dev-C++ or mingW do it or something? your input would be VASTLY appreciated...

35 Name: #!/usr/bin/anonymous : 2008-08-01 21:51 ID:8LZSGSWU

>>34

gcc -static does what you want in theory, but DLLs might not be what you think they are.

Consider the fact that in addition to being an on-disk paging system for applications, they're also used as a loadable/driver capacity, and as an intermediate executable format: ActiveX, DCOM, and any "plugin" architecture uses DLLs in such a way that gcc -static won't work, and there's frankly no way to make them work.

36 Name: #!/usr/bin/anonymous : 2008-08-01 23:09 ID:94xnnzPm

>>35

i mean, I'm prepared to have to include certain DLL's in my game's folder, as I'm using SDL, but having to include this vcredist crap is, well, crap.

a hello world program should not need any freaking redistribution package, i don't care what you're doing...

37 Name: #!/usr/bin/anonymous : 2008-08-02 01:41 ID:cA4bH4MK

38 Name: #!/usr/bin/anonymous : 2008-08-02 16:43 ID:94xnnzPm

>>37

thanks for the link (and thanks to everyone's help, too) - but I managed to figure out my problem on my own!

turns out, I was using a microsoft product. God, how did I not see it before? Of course it would be overly bloated and not work simply!

I switched over to Dev-C++ using the MingW compiler thingy, and it was a freakin BREEZE. 50 meg install, no crazy fiddlin' with the settings. whatever i compile will just work like tasty tasty exe's should.

FUCK
YOU
VC++EXPRESS

39 Name: #!/usr/bin/anonymous : 2008-08-05 21:12 ID:Heaven

i should note (for you people you find they have a simmilar problem) that your exe file will probably balloon in size if you switch. it'll probably increase in size about 500kb.

40 Name: #!/usr/bin/anonymous : 2008-08-09 23:16 ID:xETlsF3q

Question: I was reading an article on data structures for maps in roguelikes, and this section confused me.

"You can use a 2d array of flags. Again, if you use ints, you can fit more than enough flags there. For start, let's have HAS_OBJECT, HAS_MONSTER and HAS_ITEM flags. Then you don't have to do lookups for every square -- only for the squares that really contain something, and these are pretty rare. Every time you add/remove anything form the square, you update the flags."

What do they mean by a "2d array of flags using ints"? Is each int an index number in a array of flags? Considering that you'd have to account for multiple flags, this seems unlikely. Can anyone help me figure this out?

Full article here: http://roguebasin.roguelikedevelopment.org/index.php?title=Data_structures_for_the_map

41 Name: #!/usr/bin/anonymous : 2008-08-10 00:57 ID:cA4bH4MK

>>40

unsigned char map[100][100];

This is your map.

#define HAS_MONSTER 1
#define HAS_ITEM 2
#define HAS_OBJECT 4
#define HAS_STAIRS 8

These are your flags.

struct thing { struct thing *next; /* other fields */ };
struct thing *monsters, *items, *objects, *stairs;

These are your linked lists which actually contain interesting things.

Drawing the map is easy; simply scan the map[][] array and then fill in the blanks.

Pathfinding is easy; only map[][] needs to be consulted.

42 Name: #!/usr/bin/anonymous : 2008-08-10 03:13 ID:uMFg46Po

But what if one of the cells in map[][] contains both an item and a monster? How do you indicate multiple flags in one cell?

43 Name: #!/usr/bin/anonymous : 2008-08-10 04:03 ID:OZjcrF84

>>42

Use bitwise OR (HAS_MONSTER | HAS_ITEM).

44 Name: #!/usr/bin/anonymous : 2008-08-10 04:09 ID:Heaven

>>42

Stop programming immediately.

45 Name: #!/usr/bin/anonymous : 2008-08-10 18:04 ID:Heaven

>>44
I second that motion. Go back to school.

46 Name: #!/usr/bin/anonymous : 2008-08-10 23:57 ID:cA4bH4MK

>>42

Seriously, you should consider taking a few classes, and get a proper teacher. If you can't afford it- even the continuing education courses at many community colleges will help.

That said, those numbers in >>41 aren't magic. They're powers of two which means they each represent a single on (1) bit, just in a different position in binary.

1     = 00000001
2 = 00000010
4 = 00000100
8 = 00001000

16 = 00010000

This was done so that they can be added (binary-or) and tested (binary-and) independently, and very quickly. Look for yourself what the binary result is of the sum of all those flags, and what happens if you binary-and (&) each of them in turn.

47 Name: #!/usr/bin/anonymous : 2008-08-15 12:11 ID:t1gpNPSR

I'm searching for a simple code editor, that works well with ASP (VBScript and JScript), C#, (X)HTML (even when it's broken), JavaScript and CSS. Notepad++ apparently doesn't, I know that VIM does, but forcing people to learn it probably would just be counterproductive.

I need a straight answer, no BS, no religion wars, please.

48 Name: #!/usr/bin/anonymous : 2008-08-15 13:07 ID:Heaven

>>47

When you learn how to code, you'll realize that whatever editor you're using is a code editor.

If you're looking for an editor that will help you learn how to code, then you should ask for that. Visual Studio and Eclipse seem to be enjoyed by beginners.

49 Name: #!/usr/bin/anonymous : 2008-08-15 14:29 ID:Heaven

>>48
There you go.. can't you simply answer a question, especially when I ask for no bullshit? Did you even read my question? I asked for a "simple code editor", not a full blown IDE, not to even mention that neither Visual Studio nor Eclipse satisfy my basic requirements. STFU if you have nothing worthwhile to say.

BTW, seeing as you don't know what a code editor is: http://en.wikipedia.org/wiki/Source_code_editor

50 Name: #!/usr/bin/anonymous : 2008-08-15 14:51 ID:Heaven

>>49

http://www.crimsoneditor.com/

I don't know exactly what you mean by "works well with," but the editor has syntax-highlighting support for the languages you listed. Has some basic tools for things like compiling, but isn't very fancy.

51 Name: #!/usr/bin/anonymous : 2008-08-15 17:11 ID:Heaven

>>49

Programmer's Notepad is pretty lightweight and has syntax support for those languages.

http://www.pnotepad.org

52 Name: 48 : 2008-08-15 17:15 ID:8LZSGSWU

>>49

You out of hand rejected a large class of editors; the kind that are "hard to use". I couldn't possibly recommend "ex" because at best you would whine about how "ex" isn't anything like a "source code editor" because it lacks syntax hilighting, bracket matching and autocompletion. Or maybe you would just say it is hard to use.

Your editor is just a crutch. emacs may be less of a crutch when used by an expert, but emacs can also be just as much of a crutch as anything else if you use it like notepad. The things that can make emacs a good source code editor are obscured by it's menu bar and graphics.

If you keep crying about how people keep calling you inexperienced, you're never going to get any better. Learn "ex" and you'll become a better programmer simply because you bothered to learn something new. Learn "ex" and you'll become a better programmer because you'll have learned a new way to edit programs.

But stop crying like such a girl.

53 Name: #!/usr/bin/anonymous : 2008-08-15 21:14 ID:Heaven

Same goes for vim

54 Name: #!/usr/bin/anonymous : 2008-08-18 13:39 ID:Heaven

>>50
Thanks, that works good enough.

55 Name: #!/usr/bin/anonymous : 2008-09-04 07:52 ID:VsF/SKrW

What's the relationship between the Linux kernel and glibc? Is there some kind of circular dependency or does one not link against the other?

56 Name: #!/usr/bin/anonymous : 2008-09-04 11:46 ID:Heaven

>>5
5
glibc is an implementation of the C library.
gcc is an implementation of a C compiler.
These reside in the outer layer of an operating system;

See http://learnlinux.tsf.org.za/courses/build/images/diagram30.png

Nothing to do with the kernel, a kernel can work without them.

57 Name: #!/usr/bin/anonymous : 2008-09-04 11:46 ID:Heaven

>>56
Of course, I meant to quote >>55 not >>5.

58 Name: #!/usr/bin/anonymous : 2008-09-05 08:21 ID:wncl2fNG

So i just read through some basic tutorials on Ruby and I'm trying to get the following to work with my laziness, limited Ruby and general programming knowledge.
I basically have a hex string that looks like "0x0011223344..." and I want to cut out every byte/2 hex values into an array where I then append a "%" before every byte so I can decode it in another program (I know this is probably possible with Ruby itself).
Thats what I've got so far:

*x = "0x11223344"*
*a = 0*
*e = 1*
*y = [x.slice(a..e)]*

Now how I thought about looping that and incrementing a, and e, in each loop, but this will only get y[0] filled. How do I loop it so that the entire content of x is put into the y[] array byte by byte?

59 Name: #!/usr/bin/anonymous : 2008-09-05 13:38 ID:8LZSGSWU

>>58

x.gsub(/[0-9a-fA-F]{2}/,'%\0')

or was there a particular reason you wanted it to be an array?

out=[]; x.gsub(/[0-9a-fA-F]{2}/) { |s| out.push("%" + s); }

Really. Nobody writes ruby like that. What exactly are you trying to do?

60 Name: #!/usr/bin/anonymous : 2009-03-27 12:05 ID:t1gpNPSR

Good questions to ask a programmer during an interview.

61 Name: #!/usr/bin/anonymous : 2009-03-27 12:25 ID:t1gpNPSR

>>60
Sorry, that was meant to be a question. I'm making a list of questions to ask programmers in few upcoming interviews. I know all the classic questions and answers to expect. Just wondering, what do you think was a good/interesting question asked in an interview?

62 Name: dmpk2k!hinhT6kz2E : 2009-03-27 19:26 ID:Heaven

I like the following approach:

First ask them some basic questions to filter out the utterly clueless. E.g.:

  • reverse a string
  • replace all a's with b's in this string
  • write a recursive factorial function and explain problems with it

If they pass that sanity check, give them a small test project that takes about an hour or two to complete. Let them use their own computer, tools, and the internet, and see how they solve it.

63 Name: #!/usr/bin/anonymous : 2009-03-30 18:08 ID:8IXmdi23

>>61

Well, what are the job requirements?

Tell us the job requirements first.

64 Name: #!/usr/bin/anonymous : 2009-04-05 06:35 ID:dv6wkleh

>>62

(reverse "abcab")
(loop for i across "abc" collect (if (char= #\a i) #\b i))
(defun f (x) (do ((f-iter (lambda (y z) (if (> y 1) (funcall z (progn (setf x (* x (1- y))) (1- y)) z))) f-iter)) (nil nil) (funcall f-iter x f-iter)))

65 Name: #!/usr/bin/anonymous : 2009-04-15 14:33 ID:8LZSGSWU

>>62

(reverse input)
(substitute #\a #\b input)
(defun f (n &optional (a 1))
(declare (optimize speed))
(if (<= n 1) a (f (1- n) (* a n))))

>>64

Code like that would make anyone afraid of lisp...

66 Name: #!/usr/bin/anonymous : 2009-04-17 12:08 ID:Heaven

>>65
You failed the assignment! Your code is not recursive.

Actually, it is recursive, and you did not fail the assignment, at least not for the reason I'm about to mention. The CL standard doesn't require an implementation to implement tail-recursion, therefore you can't assume this will be compiled with iterative steps, but as you see this is a special case that your code happends to fall in. You might have to explain both the caveats of a recursive implementation of the factorial sequence and the compiler optimization that might be applied to your code in this instance.

Had it been scheme lisp, your code wouldn't follow the assignemnt specification. However, your second line actually replaces all b's with a's, and not all a's with b's.

>>64 failed the assignment for sure. His last line of code, the factorial implementation, simply does not work.

Line #2 returns a list instead of a string; but due to the apparently relaxed rules the spec imposes, this might pass. Why relaxed? Note there's no mention of what replace means. Can a fully functional program comply to the spec? Ie, does it matter if I really modify the object or return a modified copy of it?

It probably boils down to a macro named substitute! or not.

This thread has been closed. You cannot post in this thread any longer.