Wednesday, May 14, 2008

missing ( before formal parameters


I ran across this little error the other day when I ran some javascript that was working fine in IE but Firefox just didn't like.
It was pretty easy, but vague, to track down what Firefox was complaining about and it was just another difference between JScript and Javascript ugh.
Here's a little example of what was causing this issue.

<script type="text/javascript">
// just create some dummy variables for namespaces
var some = {};
some.namespace = {};

(function($) {
function $.testFunc() { /*some code here*/ }
})(some.namespace);
</script>

The problem is when creating the testFunc function. JScript allows the '.' in the declaration and Javascript does not. Here is a quick and simple fix for this that will work in both IE and Firefox (and Safari).

<script type="text/javascript">
// just create some dummy variables for namespaces
var some = {};
some.namespace = {};

(function($) {
$.testFunc = function() { /*some code here*/ }
})(some.namespace);
</script>

Thursday, February 28, 2008

Some Fun Generating 'the power of' With F#

So for my first bit of fun with F# I figured it'd be fun to play with the BigNum (used for crazy big numbers) I figured I write up some quick functions that simple generate the power of some number, resulting in some crazy big numbers :)

(*all of my examples include '#light')

My first function doesn't use the BigNum but it's very simplistic.

let rec pwrof x y =
     if y = 1 then
         x
     else
         x * pwrof x (y - 1)

So for everyone new to F# 'pwrof' is a recursive function that takes 2 ints. 'rec' lets the compiler know that the function is recursive and the 2 parameters get resolved down to being ints at compile time. This came pretty naturally for me coming from a C# world, so lets try to mix it up a bit.

let rec pwrof x y =
     match y with
     | 1 -> x
     | _ -> x * pwrof x (y - 1)

This function does the exact same thing, but instead of using a familiar if/else statement we're using pattern matching (these are widely used in F#). It's like a switch statement. If y matches 1 then we return x, otherwise if y matches anything else, '_', then we recurse.

These were kinda fun, but I wanted to calculate crazy big power of calculations like 5000^5000. So here's an example that resembles my first function

let bpwrof x y =
    let x = Microsoft.FSharp.Math.BigNum.of_int x
    let pwr = x
    let rec pwrof x y =
        if y = 1 then
            x
        else
            pwrof (x * pwr) (y - 1)
    pwrof x y

And here's an example that resembles my second function

let bpwrof x y =
    let x = Microsoft.FSharp.Math.BigNum.of_int x
    let pwr = x
    let rec pwrof x y =
        match y with
        | 1 -> x
        | _ -> pwrof (x * pwr) (y - 1)
    pwrof x y

These are tail recursive so you can create some really, REALLY big numbers. If you call one of the last 2 functions you can calculate say 5000 to the 9000th power. It ends up taking a lot more time to print the result to the screen than it does to do the calculation(its pages and pages of number madness). I even tried 5000 to the 90,000th pwr, that's a big number.

 

Oh well, that's all for now...

Sunday, February 17, 2008

F# again

I had blogged about F# a while back and though I had tinkered around with it a bit, I got a little busy, and a little lazy. I've decided I'm gonna stick with it this time and learn it :) which means more upcoming blogs about F# (I haven't blogged in a while either so this will get me motivated)

I'm not one that enjoys scouring the web for information on things so I usually just go buy a book on whatever it is that I'm interested in. I decided the other day to head down to the local Borders and there was one copy left of Robert Pickering's F# book, titled 'Foundations of F#'. I'm currently only a few chapters in but it seems like a pretty good book thus far. It hasn't required a background in functional programming which is good and also seems to be a good read for the novice to expert programmers.

Well that's all for now, maybe next time I'll have some F# knowledge to share :)