## The "IT DOESN'T WORK!" thread

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

jaap
Posts: 2094
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

### Re: The "IT DOESN'T WORK!" thread

sourmìlk wrote:one more oddity though:
this code

Code: Select all

BYTE *array = new BYTE[270000];cout <<sizeof(array) <<endl

prints out "8".

At first it seems like it should, as I'm getting the size of 8 addresses, but sizeof(*array) does the same thing.

sizeof(array) == sizeof(BYTE*)
and pointers on your system are 8 bytes/64bits.

sizeof(*array) == sizeof(BYTE)
and this depends on what kind of type BYTE is. This number is however many bytes are used to store a BYTE, though it might not be the case that all those bytes are actually needed. Most computers prefer addresses to be multiples of 4 or even 8, so padding might have been added by the compiler.

Remember also that sizeof() is not a function, but it is something that is calculated during compilation. So it only looks at the type it is given and gives that size. Pointers do not 'know' the size of whatever it is they are pointing to.

Here is an even more curious example:

Code: Select all

#include <iostream>int test( char arr[] ){   return sizeof(arr);}int main(){   char myArray[10];   char *ptr = myArray;   std::cout << sizeof(myArray) << std::endl;   std::cout << sizeof(ptr) << std::endl;   std::cout << sizeof(*ptr) << std::endl;   std::cout << test(myArray) << std::endl;}

This prints 10, 4, 1, and 4 on my machine (on some the 4s could be 8s).
In the local scope sizeof(myArray) works as you expect, because myArray has type char[10] which is of size 10.
The pointer ptr has type char*, which has size 4 on my machine.
*ptr has type char, which has size 1 always.
The curious one is test(myArray). The parameter of test looks like an array, but it is not. It is exactly equivalent to:

Code: Select all

int test( char* arr ){   return sizeof(arr);}

So arr is a parameter variable of type char*, so it is a normal pointer, and will have the same size as ptr did earlier.
Even if you explicitly write in the length of the array,

Code: Select all

int test( char arr[10] ){
you get the same thing. Parameter variables are never array types, but only pointers.

wombatkombat
Posts: 1
Joined: Fri Mar 18, 2011 5:16 pm UTC

### Re: The "IT DOESN'T WORK!" thread

hi whenever i input my code here it comes up when i compile it...

swap.hs:7:23: parse error on input ''='

Code: Select all

module Main whereimport Systemmain = do(arg1:arg2:_) <- getArgsxs <- getContentsputStr (transform xs arg1 arg2)transform input w1 w2 = unlines [ process line | line <- lines input ]where process line = unwords [ f word | word <- words line ]f w | w == w1 = w2| otherwise = w

guys i really need help on this i cant get it to work... im using cygwin on windows, i also used terminal on ubuntu and it still wont work

Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

### Re: The "IT DOESN'T WORK!" thread

<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Cheese> I love you

Keand64
Posts: 35
Joined: Wed Dec 16, 2009 1:05 am UTC
Location: Somewhere, presumably

### Re: The "IT DOESN'T WORK!" thread

gyuu...

So, I've this regex

Code: Select all

(?i)(?<=(^|[.?!:;]))[^.?!:;]*?bel[^.?!:;]*?(?=$|[.?!:;]) whose purpose is to find a segment of a string containing "bel" separated by various forms of punctuation (e.g.: running it on "Various irrelevant text. Bel, do some stuff. More irrelevant text" would find " Bel, do some stuff". Well, in theory at least.) However, it doesn't seem to like removing irrelevant text from the beginning of the string. E.g.: running it on "Bel, do some stuff. Blah" finds "Bel, do some stuff" fine, but running it on"Blah. Bel, do some stuff" doesn't find anything. I assumed it might be an error with the lookbehind having two different sized options, but replacing the "(^|[.?!:;])" with "(^.|[.?!:;])" solves nothing. The crazy part is that the expression works fine in RegexBuddy, the software I'm using to test it, so I have no idea why it won't work with JRegex... EDIT: I got it to work by removing changing the lookbehind to Code: Select all (^|(?<=[.?!:;])) but it still bothers me that the original regex works fine in RegexBuddy but not JRegex... CorruptUser wrote:That's how the robots will takeover. Sentient spambots. In the future, man will be required by law to enlarge his penis and use vicodin, and on occasion, donate$5000 to a Nigerian.

Ephphatha
Posts: 625
Joined: Sat Sep 02, 2006 9:03 am UTC
Location: Bathurst, NSW, Australia

### Re: The "IT DOESN'T WORK!" thread

How do I vertically align equations in a latex document when I want them displayed in a table?

The below gives me similar output to what I want, but there is an excessive amount of whitespace before the last two equals signs.

Code: Select all

\begin{tabular}{ccrll}$n_b=8$ & $\bar{x}_b=103.75$ & $s^2_b$&=$\frac{1}{7}(87672-87362)$&=$110$ \\$n_a=8$ & $\bar{x}_a=113.25$ & $s^2_a$&=$\frac{1}{7}(102982-102604.5)$&=$53.93$\end{tabular}

Using the array environment is no better.

Code: Select all

$\begin{array}{ccrll}n_b=8 & \bar{x}_b=103.75 & s^2_b&=\frac{1}{7}(87672-87362)&=110 \\n_a=8 & \bar{x}_a=113.25 & s^2_a&=\frac{1}{7}(102982-102604.5)&=53.93\end{array}$

The output:
$\begin{array}{ccrll} n_b=8 & \bar{x}_b=103.75 & s^2_b&=\frac{1}{7}(87672-87362)&=110 \\ n_a=8 & \bar{x}_a=113.25 & s^2_a&=\frac{1}{7}(102982-102604.5)&=53.93 \end{array}$

Do I just need to do this and use negative space to pull the alignment back or something?
I'm not lazy, I'm just getting in early for Christmas is all...

Inglonias
Posts: 126
Joined: Mon Jun 28, 2010 5:54 pm UTC

### Re: The "IT DOESN'T WORK!" powwow

I was just working on my Jeopardy game, when the "Open last file" command was being executed no matter which button I pressed. It took me fifteen minutes to find that I had done this:

Code: Select all

if (actionCommand.equals("Open"));

Rather than this

Code: Select all

if (actionCommand.equals("Open"))

I had put a semicolon right after the if statement, causing it to execute the code in brackets no matter what.

Ansain
Posts: 207
Joined: Sun Apr 15, 2007 1:15 am UTC
Location: Here

### Re: The "IT DOESN'T WORK!" thread

I am trying to write a Matlab code that gathers data from excel, runs some calculations on the data, and gives me a plot of the data. right now I can get everything to work with one small problem. I can't figure out how to make the code work for a general file name. right now it works if I do this.

Code: Select all

x(:,1)= xlsread('dataset1');

I would like to use the same code for dataset2, and 3, etc. Here is what I have tried.

Code: Select all

filename = input('Type filename below','s');xlsread('%s',filename);

When I do this it tries to get the data from a nonexistent file %s.xls. Does anyone know how to fix this.
Why put off till today what you could just as easily get done tomorrow?

I can mathematically prove that 1 equals 0!.

Parts a-x in my plan weren't that important anyways.

MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

### Re: The "IT DOESN'T WORK!" thread

Writing a pure haskell mersenne twister, but the god damn array indexes are off.

I really need to learn how to use a debugger.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

### Re: The "IT DOESN'T WORK!" thread

Do you mean "a debugger" or "a Haskell debugger"? The two are only tenuously similar in use and function.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Cheese> I love you

MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

### Re: The "IT DOESN'T WORK!" thread

headprogrammingczar wrote:Do you mean "a debugger" or "a Haskell debugger"? The two are only tenuously similar in use and function.

You know what I mean!

(I rarely touch anything not haskell)
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

squareroot
Posts: 548
Joined: Tue Jan 12, 2010 1:04 am UTC
Contact:

### Re: The "IT DOESN'T WORK!" thread

Ansain wrote:I am trying to write a Matlab code that gathers data from excel, runs some calculations on the data, and gives me a plot of the data. right now I can get everything to work with one small problem. I can't figure out how to make the code work for a general file name. right now it works if I do this.

Code: Select all

x(:,1)= xlsread('dataset1');

I would like to use the same code for dataset2, and 3, etc. Here is what I have tried.

Code: Select all

filename = input('Type filename below','s');xlsread('%s',filename);

When I do this it tries to get the data from a nonexistent file %s.xls. Does anyone know how to fix this.

I'm not positive, but shouldn't

Code: Select all

filename = input('Type filename below','s');xlsread(filename);

work? filename should be a string containing the name of the file, so you can just feed that directly in. You shouldn't expect a function made for reading files to have a built-in format command; according to [http://www.mathworks.com/help/techdoc/ref/xlsread.html the doc], it was probably using the second argument (filename) to select which sheet in the file to view, which is why it wasn't throwing an error.
<signature content="" style="tag:html;" overused meta />
Good fucking job Will Yu, you found me - __ -

sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

### Re: The "IT DOESN'T WORK!" thread

So, a question about inheritance of abstract classes in C++, which I can't seem to get right.

I declare an abstract class as follows:

Code: Select all

class eventListener{    public:        virtual void guiKeyPress(unsigned char key, int x, int y);        virtual void guiSpecialPress(int key, int x, int y);        virtual void guiMouseDown(int button, int state, int x, int y);        virtual void guiMouseMove(int x, int y);        gui *GUI;        eventListener(gui setGUI);        virtual void keyPress(unsigned char key, int x, int y) const = 0;        virtual void specialPress(int key, int x, int y) const = 0;        virtual void mouseMove(int x, int y) const = 0;        virtual void mouseDown(int button, int state, int x, int y) const = 0;};

And in the .cpp file

Code: Select all

eventListener::eventListener(gui *setGUI){   //a bunch of stuff}void eventListener::guiKeyPress(unsigned char key, int x, int y){   if(key == 27)      exit(0);   else      this->GUI->keyFunc(key, x, y);}void eventListener::guiSpecialPress(int key, int x, int y){   this->GUI->specialPress(key, x, y);}void eventListener::guiMouseDown(int button, int state, int x, int y){   this->GUI->mouseDown(button, state, x, y);}void eventListener::guiMouseMove(int x, int y){   this->GUI->mouseMove(x, y);}

And then I declare a child of that class

Code: Select all

class listener : protected eventListener{    public:        void keyPress(unsigned char key, int x, int y);        void specialPress(int key, int x, int y);        void mouseMove(int x, int y);        void mouseDown(int button, int state, int x, int y);};

And in the source file:

Code: Select all

void listener::keyPress(unsigned char key, int x, int y){    this->guiKeyPress(key, x, y);}void listener::specialPress(int key, int x, int y){    this->guiSpecialPress(key, x, y);}void listener::mouseDown(int button, int state, int x, int y){    this->guiMouseDown(button, state, x, y);}void listener::mouseMove(int x, int y){    this->guiMouseMove(x, y);}

However, this code:

Code: Select all

listener *someListener = new listener(GUI);

Gives me an error, saying that it can't instantiate an abstract class. But isn't listener not an abstract class? Am I not defining a virtual function I should be or something?
Obviously a lot of the code seems unnecessary now, but I'm adding stuff to the listener class later.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

### Re: The "IT DOESN'T WORK!" thread

All the virtual functions in your base class are "const", but the ones in your subclass aren't. So they're different signatures, so they don't override the ones in the baseclass. Instead, they just overload them... listener has both, for example, a defined keyPress(unsigned char,int,int) and an abstract keypress(unsigned char,int,int) const.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Laguana
Posts: 49
Joined: Sat Jan 19, 2008 10:13 pm UTC

### Re: The "IT DOESN'T WORK!" thread

Well, one problem is you haven't actually instantiated the virtual functions: They are const in eventListener but not in listener, and so they don't override it.

Once you fix that, you'll probably also have to give a constructor for listener which takes a gui as well.

Pre-post edit: Beaten, I see. I'll still post this since the constructor issue may still be there.

sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

### Re: The "IT DOESN'T WORK!" thread

Laguana wrote:Well, one problem is you haven't actually instantiated the virtual functions: They are const in eventListener but not in listener, and so they don't override it.

Once you fix that, you'll probably also have to give a constructor for listener which takes a gui as well.

Pre-post edit: Beaten, I see. I'll still post this since the constructor issue may still be there.

Yep, it was the constructor. (I'd toyed around with the arrangement of consts before, so I figured that wouldn't be it).
Last edited by sourmìlk on Thu Apr 07, 2011 1:33 am UTC, edited 1 time in total.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Vince Noir
Posts: 2
Joined: Wed Oct 28, 2009 3:58 am UTC

### Re: The "IT DOESN'T WORK!" thread

Hey guys, just wondering if someone could point me in the right direction regarding bit mask (sort of) things in C.
Pretty much, i'm trying to get the first six binary bits from a hexadecimal input, and store them into a variable "opcode" (project is a disassembler from machine into MIPS assembly)
I know how to print out the first six bits, like so:

Code: Select all

for(i=0;i<7;i++){    opcode = (1 && (input &(1<<(32-i))));    printf("%d", opcode);}

but i'm really stumped as to how i could put the entire sequence into the variable. I'm aware that i could make opcode into an array, but at first glance that seems to make the remainder of the project overly difficult/long-winded.

And yes, as you can probably guess, i'm pretty new to C, so baby steps would be appreciated if at all possible.

Cheers,
Vince

phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

### Re: The "IT DOESN'T WORK!" thread

It sounds like all you want is just

Code: Select all

opcode = (input >> 26) & 0x3F;

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Daelar
Posts: 83
Joined: Fri Apr 04, 2008 11:01 pm UTC

### Re: The "IT DOESN'T WORK!" thread

err, I hope this is the right area for this.

Using Haskell, this is uni project so I'm not looking for straight up answers (I fully agree with 'If I don't do it myself I'll never learn'), and the bit I'm stuck on is one small part of it. Making a film database with

Code: Select all

type Film = (String, [String], Int, [String])-- Title, Cast, Release Year, 'fans'

and the part I'm stuck on involves this:

Code: Select all

display [] = []display (x:xs) = printing x: display xsprinting (title, stars, year, fans) = do putStrLn $"Film Title: " ++ title; putStrLn$ "Film Stars: " ++ list stars; putStrLn $"Film Release Year: " ++ show year; putStr$ "Number of fans: "; putStrLn (show $length fans)fyi 'list' is another method The length of the fans part is deliberate as it's required, the printing part works on it's own if I simply provide one Film, I'm trying to use recursion to make display go through a list of Film types. As it is now, the error I get is: Code: Select all ERROR - Cannot find "show" function for:*** Expression : display testDatabase*** Of type : [IO ()] But I've tried using Code: Select all display x$ printing xsdisplay x . printing xs

This is the first time I've ever used putStrLn, do and I/O in general. I'm feeling really lost with it and I'm convinced I'm making a really obvious error.

Any hints are really appreciated, and apologies if this is in the wrong place/waste of time.

Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

### Re: The "IT DOESN'T WORK!" thread

It looks like somewhere, you are trying to show IO actions. So let's follow the types (which will sort of be guessing):

Code: Select all

putStrLn :: String -> IO ()show :: Show a => a -> Stringprinting :: Film -> IO () -- not the actual signature but close enoughdisplay :: [Film] -> [IO ()]

Your error says you are trying to show an IO action, but I can't tell why from the code you have currently pasted. It looks like the function you are looking for is of type [Film] -> IO (), and prints each film in some pretty way. That's almost possible with the functions you have now, but I will also refactor what you have a bit, so it is easier to read, using some common functions you probably haven't seen yet.

Code: Select all

-- (map f l) applies f to every individual element of l-- map :: (a -> b) -> [a] -> [b]-- map f [] = []-- map f (x:xs) = (f x) : (map f xs)displays :: [Film] -> [IO ()]displays films = map printing films-- alternate definition -- display = map printing-- sequence_ :: Monad m => [m a] -> m ()-- runs a list of monadic actions in sequence, then forgets the resultsdisplay :: [Film] -> [IO ()]display films = sequence_ (displays films)-- OR -- display = sequence_ . displays

Do-notation is hard to read all on one line:

Code: Select all

printing (title, stars, year, fans) = do    putStrLn $"Film Title: " ++ title putStrLn$ "Film Stars: " ++ list stars    putStrLn $"Film Release Year: " ++ show year putStr$ "Number of fans: "    putStrLn (show length fans) Finally, you should get in the habit of putting type signatures on all your functions, before writing out the function itself. Far from being a compile-time sanity check, types give you a LOT of information about their respective functions, and should help you make better sense of IO. For getting used to IO, and monadic style in general, I personally suggest starting out writing code that uses Maybe. It's dead simple and should help you get used to what goes on behind the scenes. <quintopia> You're not crazy. you're the goddamn headprogrammingspock! <Weeks> You're the goddamn headprogrammingspock! <Cheese> I love you Daelar Posts: 83 Joined: Fri Apr 04, 2008 11:01 pm UTC ### Re: The "IT DOESN'T WORK!" thread I can't believe I didn't think of using map, wow. You helped greatly there, thank you very much for the time, appreciated. (Your other comments were taken on board too I swear) Rysto Posts: 1460 Joined: Wed Mar 21, 2007 4:07 am UTC ### Re: The "IT DOESN'T WORK!" thread So all at once today, several people come up to me and tell me that a particular program I'm responsible for is broken. It didn't entirely surprise me -- I'd had a couple of reports that the program didn't work when compiled for 64-bit, even after I thought that I had fixed the issue. What did surprise me was that everybody was emphatic that it was the build that was done this week that had introduced the breakage. As far as I knew that was impossible -- literally nothing had been changed in several weeks. From the symptoms that people were reporting, I was pretty sure that this was another instance of a bug I had fixed a while ago. The program in question interacts with a particular bit of hardware via some memory-mapped registers. There's one very important rule for accessing those registers: your accesses must be 4-bytes wide or smaller. If you break this rule, you read back all-ones instead of a valid value. The previous bug that I had quashed happened because I got a bit too clever in how I pulled data out of those registers, and when compiled for 64-bit the program ended up trying a 64-bit (8-byte) read on a register. I made some changes to the program at that point to ensure that all accesses to the memory-mapped registers went through a single pair of read/write functions, and wrote those functions to try and guarantee that all accesses would be of a valid width. Knowing all that I had a really good starting point, but I was still mystified as to how things had been broken in the last week. I quickly confirmed that build X-1 worked while build X would always fail. But I also verified that they were built from exactly the same code. WTF? The first thing that I did was jump right into the disassembly of the two builds. I wanted to verify that I was actually issuing the read with the right width. I knew exactly which read was failing so it was easy to track it down in the disassembly in both cases. This is where I started to get very confused, though: the assembly that did the read was identical up to renaming of the registers and the offset at which a particular stack variable lived. Ok, so the next step is to single-step through the failing code. I set a breakpoint right after the instruction that read from the memory mapped regster. gdb blew right past it and the program completed. Damn, I must have been looking at the wrong bit of disassembly after all. I set a breakpoint on a function that gets called right after the failing read so that I can figure out what I'm actually supposed to be looking at. I do hit this breakpoint, but when I look backwards from where I am I see that the exact same read instruction that I set the first breakpoint on -- but I never hit it! Ok, this time I'll set the breakpoint just before the read, and single-step to it. I set the breakpoint, and pretty soon gdb hits a branch that I can't make heads or tails of. It doesn't correspond to any if statement that I can see in the code. I ignore it and step past it, and the program jumps away from what I believed to be the failing read instruction. Ok, that explains why my breakpoint wasn't triggering, but why isn't the program reading from the hardware? I keep stepping until gdb gets to an SSE instruction, and that's when the penny finally dropped. Later versions of gcc try to use SSE instructions for accessing pieces of memory larger than 16-bytes wide. However, gcc only ever emits an SSE instruction that accesses memory that's aligned to a 16-byte boundary -- the aligned versions of the instruction is way more efficient. Now, the C code that was failing looked like this: Code: Select all uint8_t array[count];for (i = 0; i < count; i++) array[i] = read_register(REGISTER_ARRAY_BASE + i); gcc was clever enough to inline read_register, and after doing that it realized that I was copying from one conituguous piece of memory to another. So it unrolls the loop and uses 16-byte SSE memory instructions to do the copy. But there's a catch: gcc only issues the aligned instructions, but it can't guarantee the the destination array is properly aligned. So that mysterious branch that I couldn't figure out? gcc was testing the alignment of the destination array: if it was properly aligned, it jumped to the unrolled loop with SSE instructions. If it wasn't aligned, it jumped to a fallback loop that only used 1-byte accesses. This fallback loop was where I kept setting that breakpoint that never got hit. So the reason that one build consistently worked while the other didn't was that the stack layout changed subtly between the two builds for whatever reason. In the failing build, the destination array happened to be aligned to the 16-byte boundary needed to use the SSE instructions, and everything went to hell. Let me tell you, I got some really funny reactions from some people at work today when I told them that the new build was broken because the stack layout happened to change. Rysto Posts: 1460 Joined: Wed Mar 21, 2007 4:07 am UTC ### Re: The "IT DOESN'T WORK!" thread Fun fact: GNU make has the following built-in rule: Code: Select all %: %.sh cat> > $@ This just reinforces my opinion that built-in make rules are an absolutely terrible idea. _Axle_ Posts: 253 Joined: Fri Sep 24, 2010 7:33 pm UTC ### Re: The "IT DOESN'T WORK!" thread Anyone here ever run into a problem with the command line version of SVN, the export command and spaces in the folder name? It can't seem to figure out the spaces, even with quotes around the folder. It always come up with : Folder%20With%20Spaces isn't under version control and I know 0x20 is a space. My only work around has been been doing Code: Select all Rename "Folder with Spaces" FolderWithSpacescall "svn" export FolderWithSpaces/Project/Data QuickCopy/DataRename FolderWithSpaces "Folder with Spaces" The name wasn't that, but changed cause it makes more sense in this context. Also, I feel real stupid for having this be an issue Yakk wrote:Computer Science is to Programming as Materials Physics is to Structural Engineering. Kag Posts: 1214 Joined: Thu Aug 23, 2007 1:56 am UTC ### Re: The "IT DOESN'T WORK!" thread full disclosure: this is homework. Don't give it away. So, I'm supposed to be making a Forth interpreter. It puts integers and commands into lists and processes them with this function: Code: Select all eval [] stack = return stackeval (x:xs) stack | isJust lookup = eval xs (f stack) | x == "." = do putStrLn (show (head stack)) eval xs (tail stack) | x =~ "^[0-9]+$" = eval xs ((read x :: Integer) : stack)  | otherwise       = do putStrLn ("Unknown word '" ++ x ++ "'")                         return stack  where lookup = assoc x dictionary        Just f = lookup

assoc looks for x in dict, which is a list of ([char], [Integer] -> [Integer]) tuples.

The problem is that he wants me to move the . command into the dictionary (among other things). Now, if I put it in its own function, whatever it returns is going to be in an IO monad, which is decidedly not a list of integers. Since un-monad-ing an IO Monad requires more than a few trips to the voodoo priestess, my solution to this problem was to rewrite the way eval handles dictionary functions, so that instead of returning a modified stack, they would all take the list of tokens and just call eval using the modified stack, like this:

Code: Select all

...|isJust lookup = f xs stack...dot z (x:xs) = do putStrLn (show x)                   eval z xs

Which seems like an awful idea, but works wonderfully, except that later down the line I need to implement the ability to define new words. To do this, I have to pass the dictionary into eval, so I can modify it in basically the same way I'm dealing with the stack. Like this:

Code: Select all

eval []     stack dictionary = return (stack, dictionary)eval (x:xs) stack dictionary  | isJust lookup   = f xs stack dictionary  | x =~ "^[0-9]+$" = eval xs ((read x :: Integer) : stack) dictionary | otherwise = do putStrLn ("Unknown word '" ++ x ++ "'") return (stack, dictionary) where lookup = assoc x dict Just f = lookup BUT, all the functions I made before that call eval now have to take the dictionary as a parameter. Since they're all in the dictionary, it creates an infinite type, which is bad. Now, obviously I'm doing something extremely stupid, I'm pretty sure it has to do with my solution to the first problem, but I don't see how I can get around it. edit: Aha! Solved this problem. Reorganized the dictionary functions so that they return a ([Integer], [Integer]) pair. The first list is sent off to be printed, the second one is a new stack. To solve the adding commands to the dictionary problem, I put those commands in a second dictionary which is handled in a completely different way from the normal one. I feel like a god damn genius, which I think is the point of functional languages. The Great Hippo wrote:I am starting to regret having used 'goat-fucker' in this context. Wendell890 Posts: 26 Joined: Tue Apr 26, 2011 11:40 am UTC ### Re: The "IT DOESN'T WORK!" thread I'm trying to work with two Perl modules, Image::Compare and Video::Capture::V4l::Imager, and they are unable to be located, however CPAN has an easy time figuring out that they're there and they're up to date. How can I fix this? Here's the direct error message for Image::Compare, Code: Select all Can't locate Video/Capture/V4l.pm in @INC (@INC contains: /sw/lib/perl5/5.10.0/darwin-thread-multi-2level /sw/lib/perl5/5.10.0 /sw/lib/perl5 /sw/lib/perl5/darwin /Library/Perl/Updates/5.10.0/darwin-thread-multi-2level /Library/Perl/Updates/5.10.0 /System/Library/Perl/5.10.0/darwin-thread-multi-2level /System/Library/Perl/5.10.0 /Library/Perl/5.10.0/darwin-thread-multi-2level /Library/Perl/5.10.0 /Network/Library/Perl/5.10.0/darwin-thread-multi-2level /Network/Library/Perl/5.10.0 /Network/Library/Perl /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level /System/Library/Perl/Extras/5.10.0 .) And the direct error message for Video::Capture::V4l::Imager, Code: Select all Can't locate Image/Compare.pm in @INC (@INC contains: /sw/lib/perl5/5.10.0/darwin-thread-multi-2level /sw/lib/perl5/5.10.0 /sw/lib/perl5 /sw/lib/perl5/darwin /Library/Perl/Updates/5.10.0/darwin-thread-multi-2level /Library/Perl/Updates/5.10.0 /System/Library/Perl/5.10.0/darwin-thread-multi-2level /System/Library/Perl/5.10.0 /Library/Perl/5.10.0/darwin-thread-multi-2level /Library/Perl/5.10.0 /Network/Library/Perl/5.10.0/darwin-thread-multi-2level /Network/Library/Perl/5.10.0 /Network/Library/Perl /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level /System/Library/Perl/Extras/5.10.0 .) headprogrammingczar Posts: 3072 Joined: Mon Oct 22, 2007 5:28 pm UTC Location: Beaming you up ### Re: The "IT DOESN'T WORK!" thread I assume this is on Linux. Can you open a terminal and run "locate V4l.pm" and "locate Compare.pm", then paste the output? <quintopia> You're not crazy. you're the goddamn headprogrammingspock! <Weeks> You're the goddamn headprogrammingspock! <Cheese> I love you Wendell890 Posts: 26 Joined: Tue Apr 26, 2011 11:40 am UTC ### Re: The "IT DOESN'T WORK!" thread headprogrammingczar wrote:I assume this is on Linux. Can you open a terminal and run "locate V4l.pm" and "locate Compare.pm", then paste the output? Code: Select all WARNING: The locate database (/var/db/locate.database) does not exist.To create the database, run the following command: sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plistPlease be aware that the database can take some time to generate; oncethe database has been created, this message will no longer appear. I've run that command twice, but to no avail. headprogrammingczar Posts: 3072 Joined: Mon Oct 22, 2007 5:28 pm UTC Location: Beaming you up ### Re: The "IT DOESN'T WORK!" thread Oh, this is on a mac. I haven't ever used one, so I don't know where stuff usually goes. <quintopia> You're not crazy. you're the goddamn headprogrammingspock! <Weeks> You're the goddamn headprogrammingspock! <Cheese> I love you squareroot Posts: 548 Joined: Tue Jan 12, 2010 1:04 am UTC Contact: ### Re: The "IT DOESN'T WORK!" thread Would like to try making mods of the Source SDK; it looks fun, and like an interesting way to practice my C++. I'm having difficulty getting things set up, though; So I installed the CDT in Eclipse, and got Cygwin to provide make/g++/gdb. I downloaded the Source SDK. I can run and compile general C++ programs just fine. How do I include the Source header files, though? At first I just tried #include <cbase.h>... couldn't find the file. #include<C:\...\cbase.h>? No. I made sure that the directory (C:\...\server\) was in my includes, and then tried it with #include <cbase.h> again. It's being passed to g++ with the -I option, for sure. But it still can't find the file. Can anyone help me here? I'm feeling very reluctant to use Visual Studio, I'm thinking I *should* be able to get this working in Eclipse after all. EDIT: I swear, there's some kind of magical force that likes to play tricks on people by making them find solutions immediately after asking for help. About 120 seconds was all it took... and I don't think I event changed anything. Ah well, it works now. <signature content="" style="tag:html;" overused meta /> Good fucking job Will Yu, you found me - __ - sourmìlk If I can't complain, can I at least express my fear? Posts: 6393 Joined: Mon Dec 22, 2008 10:53 pm UTC Location: permanently in the wrong Contact: ### Re: The "IT DOESN'T WORK!" thread I just realized how much I use this thread, and now I feel kind of bad, especially when people like HeadProgrammingSpockCzar have done nothing but help. Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it. the pie Posts: 29 Joined: Tue Mar 17, 2009 7:55 am UTC Location: Oz-tray-lee-ar, brisbane ### Re: The "IT DOESN'T WORK!" thread Hi all! I hope there are some of you out there that are familiar with MS SQL, because I've googled the bajesus outta my problem and can't seem to find out how to solve it. So we have a little web based system that is attached to an MS SQL database. On one of our pages I have to display a list for a user to browse. The problem is that this list not only drags column.A, column.B and column.C from the existing tables in the database, but it has to grab those columns from tables that will be added by users later on. So how do I do this? obviously I can't do SELECT * FROM * But if I do SELECT * FROM table.1, table.2 etc Then any new tables added later on by a user will not show up in that list. Can anyone please give me some pointers? google is failing me phlip Restorer of Worlds Posts: 7573 Joined: Sat Sep 23, 2006 3:56 am UTC Location: Australia Contact: ### Re: The "IT DOESN'T WORK!" thread Wait, so is this thing you're making some sort of admin tool for other developers who are making their own apps in their databases? Or is this a real app that's creating new tables on the fly for its users? That is, are the tables you searching for ones that the users have made themselves for other reasons, or is the table-creation part of the workings of your program? Because in the latter case you're almost definitely Doing It Wrong. I can't think of a situation where that would be a good idea. In the former case, you aren't going to be able to pull all the data out in one query, but you can get the list of tables from the "sys.tables" table, and then get their structure from other sys.* tables (I don't know the exact details, I've only loosely dabbled in MSSQL). The information you find will let you construct a SELECT query for each table, and pull the data out that way. Most DBMSes I've seen have some method of introspection like this... MSSQL has the sys.* tables, Firebird has the RDB$* tables, MySQL has the INFORMATION_SCHEMA database, and also commands like "show tables", "describe table" and friends. They're useful for making dev tools, but should rarely be touched in normal operation in a production system.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

the pie
Posts: 29
Joined: Tue Mar 17, 2009 7:55 am UTC
Location: Oz-tray-lee-ar, brisbane

### Re: The "IT DOESN'T WORK!" thread

phlip wrote:Wait, so is this thing you're making some sort of admin tool for other developers who are making their own apps in their databases? Or is this a real app that's creating new tables on the fly for its users?

That is, are the tables you searching for ones that the users have made themselves for other reasons, or is the table-creation part of the workings of your program?

Well I would have designed it differently too, but this is what the rest of the guys thought would be a good idea.

Basically it's a website that tracks events. What is in the database at the moment is three different events (concert, seminar, carnival) They want specific users (admins I guess) to be able to add new event types (e.g festivals or markets) which means they will use a web-form that creates a new table in the database for that event.

Thanks for your reply! I'm going to lookup info on sys.tables, but from all my research its looking very difficult.

I guess I could always ask the Database guy to add a table that contains only general information about each event of every type. Then my job would be simple

phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

### Re: The "IT DOESN'T WORK!" thread

the pie wrote:Basically it's a website that tracks events. What is in the database at the moment is three different events (concert, seminar, carnival) They want specific users (admins I guess) to be able to add new event types (e.g festivals or markets) which means they will use a web-form that creates a new table in the database for that event.

Yeah, that really shouldn't be new tables. That should be the same "events" table, which has a column for "type", an FK to some "event_types" table. Multiple tables for that is just bad, and that way madness lies.

But if you're being strongarmed into it, I guess you have to do what you have to do... but seriously. Bad Idea.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

the pie
Posts: 29
Joined: Tue Mar 17, 2009 7:55 am UTC
Location: Oz-tray-lee-ar, brisbane

### Re: The "IT DOESN'T WORK!" thread

phlip wrote:Yeah, that really shouldn't be new tables. That should be the same "events" table, which has a column for "type", an FK to some "event_types" table. Multiple tables for that is just bad, and that way madness lies.

But if you're being strongarmed into it, I guess you have to do what you have to do... but seriously. Bad Idea.

Haha tell me about it. I believe I am already far down the path to madness with this project and I could see as soon as I sunk my teeth into it that most problems arise from the way he designed the database.

bittyx
Posts: 194
Joined: Tue Sep 25, 2007 9:10 pm UTC

### Re: The "IT DOESN'T WORK!" thread

Wow, that's pretty awful database design. I'm currently working on an event-management website as well, and one of the requirements is that an event can belong to multiple categories, so I've organized them as tags. Basically, you've got an events table which has just the event info (event_id, event_name, event_date, etc.), an event_types table (which has event_type_id, event_type_name) and a third table used for connecting the first two, event_type_tag, which has two foreign keys - event_id and event_type_id. This way, each event can be of multiple types, and the data is properly normalized (your friend who designed the db should *really* look into database normalization, it's a pretty important concept).

I sincerely suggest you change the database design to a better one, but if that isn't possible, you can do as phlip suggested - use the meta-info provided by each DBMS (which isn't very reliable, in case you ever have to switch to another DBMS) or make another table, like event_type_tables with (event_type_table_id, event_type_table_name), and then when a new table type is created (e.g. "birthday_party"), you'd add it to the event_type_tables if it isn't already there. Then you can do something like "SELECT event_type_table_name FROM event_type_tables;" which will give you a list of all the relevant tables. You can then use insert this into the FROM clause for the actual query.

Steax
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

### Re: The "IT DOESN'T WORK!" thread

The path of horrible normalization is the path of death. Trust me.

There can (and probably will) be corner cases which completely breaks for certain situations. I can't think of them from the top of my head, but allowing people to create new arbitrary tables and such is... dangerous. Probably the kind of thing you want to avoid. If you're still in the design process, it should not take you long to restructure and renormalize the database. Honestly, if you can, fight for it. Much better doing it now than after something breaks.

(What I'm saying is extremely really redundantly redundant, given others have pointed out the dark side in your environment, but it's also really, really, really important. Chances are you will get weird and wonderfully obscure problems as things go on. If your friend needs an easy introduction into database normalization, I suggest this article on database schemes for tagging systems. It's not exactly what you're doing, but it's a good way to get started.)
In Minecraft, I use the username Rirez.

cphite
Posts: 1370
Joined: Wed Mar 30, 2011 5:27 pm UTC

### Re: The "IT DOESN'T WORK!" thread

the pie wrote:
phlip wrote:Wait, so is this thing you're making some sort of admin tool for other developers who are making their own apps in their databases? Or is this a real app that's creating new tables on the fly for its users?

That is, are the tables you searching for ones that the users have made themselves for other reasons, or is the table-creation part of the workings of your program?

Well I would have designed it differently too, but this is what the rest of the guys thought would be a good idea.

Basically it's a website that tracks events. What is in the database at the moment is three different events (concert, seminar, carnival) They want specific users (admins I guess) to be able to add new event types (e.g festivals or markets) which means they will use a web-form that creates a new table in the database for that event.

Thanks for your reply! I'm going to lookup info on sys.tables, but from all my research its looking very difficult.

I guess I could always ask the Database guy to add a table that contains only general information about each event of every type. Then my job would be simple

Dude... no...

Part of your job as a developer is - as painful as it might be sometimes - to tell the folks asking for crap like this that it's crap.

This is beyond bad design. It's horribly inefficient, creates tons of extra work for you and everyone else involved, is introducing all sorts of potential problems...

A very simple solution would be two tables. One holds the events types (EventID, EventDescription, whatever else is unique to that event type) and the second holds the actual events (EventID, whatever else is unique to that event) - this would allow you to have as many events types as you want, as many events per type as you want, would be FAR easier in terms of inserting new events and reporting on them.

When an admin (or whoever) wanted to create a new type of event, they'd be creating a new row in the Event Types table. When someone wanted to create a new event, they'd be entering a new row in the Events table (with the correct Event Type as part of the row) - the coding for this sort of thing would be kids stuff. FAR easier than what you're suggesting.

Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

### Re: The "IT DOESN'T WORK!" thread

One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

Steax
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

### Re: The "IT DOESN'T WORK!" thread

For custom properties, create a third table called Event Properties with the fields EventID, PropertyID, and Value. Instead of inserting property values in an Events table, you insert them there, and link to their ID.

Or... If you wanted to be even more normalized, make a fourth table called Properties, which is just a list of allowed properties. You can add in more customization here, like what kind of validation to use, or limit them to certain Event Types. PropertyID then points to this table.

It might look like a bit more work, but it's only at the beginning.
In Minecraft, I use the username Rirez.