TWiki> INFBase Web>AnswersPage>YearOne (revision 9)EditAttach

Quotas

If you can't get into your account, and have tried on several machines, there is a strong chance that you are over your quota. Get a lab demonstrator to log you via the alternative console login method and remove some files.

C

Almost every day at InfBase we get questions about programming in C. On this page you can find answers to some of the more common questions, along with longer articles about trickier things. This is intended to be in no way an exhaustive guide; think of it as more of a handbook to basic tasks.

A very small handbook.

Pointers
A (comparatively) short guide to pointers, addresses, and references.
Booleans
A (tragically) brief tour of boolean operators and types.
Arguments
Getting things out of function arguments.
Magic
Interesting C hacks.
-- TrevorFountain - 05 Dec 2008

Java Exceptions

Okay, so a common problem that people come up against in java when they are starting out is the urge to recall a method from inside an exception thrown in that method. For instance one might be tempted by something like this:-

public void method(int par){
    try{
        doSomethingThatBreaks(par);
    }

    catch(Exception e){
      method(par+1);
    }
}

The intuition is that if the parameter is different then it should be possible to do this, the problem is that java exceptions dont work in this way. Put simply you can't do this and the code will fail. A better approach to this type of task would be to try something like this:-


public void method(int par){
    if(method_prime(par)==0){
          method_prime(par+1);
    }
}


public int method_prime(int par){
    try{
        doSomethingThatBreaks(par);
        return 1;    
    }

    catch(Exception e){
      return 0;
    }
}

If you generate different types of exception, you can do the following:

try {
   ...
} catch (ExceptionTypeA name) {
   ...
} catch (ExceptionTypeB name) {
   ...  
}  

However, if you want to try one statement after having tried and failed another statement of the same exception type, you should nest the try-catch block inside the catch block like the following:

try {
   ...     
} catch (ExceptionTypeA name) {
  try {
     ...   
  } catch (ExceptionTypeA name) {
     ...
  }  
}  

You can of course nest try-catch inside catch as many times as you need.

-- PhilMGraham - 28 Oct 2008

Regular Expressions in Python

There were some questions regarding REGEX in Python. People who have difficulties with it might want to look at the following tutorial: http://www.amk.ca/python/howto/regex/ . It's really worth consulting when you are not sure about how to construct/decipher certain patterns.

-- KisuhAhn - 06 Nov 2008

Haskell and List difference

Someone asked about how to find the difference between the elements of two integer lists. ( Although it might be that recursion wasn't permitted!)

The function is of the form

subtractLists :: [Int] -> [Int] -> [Int]

We can take the first element from each list by matching to (m:ms) and (n:ns). The difference between elements m and n ("m-n") can be joined onto a new list, with the remaining elements generated by another call to subtractLists:

subtractLists (m:ms) (n:ns) = m-n : subtractLists ms ns

subtractLists _ [] = []

subtractLists [] _ = []

-- MikeSmith - 20 Oct 2009

Edit | Attach | Print version | History: r13 | r11 < r10 < r9 < r8 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r9 - 20 Oct 2009 - 12:41:55 - MikeSmith
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
This Wiki uses Cookies