🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Adventures in F# Part 1.5

Published March 05, 2008
Advertisement
Holy crap, a comment!

In other news, I dabbled a little more with F# over the past days. The BigInt stuff seems to be missing a lot, and the intellisense on stuff seems wonky. For list/List at least, it tends to oscillate between three visible types; only one really containing the useful (if sometimes terribly named) bits.

I found a few more pattern matching examples ( (n::_) for something starting a list for example) which make it a little niftier than sugar for if/else blocks. Still a disappointment. The style does though lend itself very, very nicely towards algorithm implementation. I got half through an implementation of the quadratic sieve before I realized that BigInt kinda sucked.

So I went on to a traditional interview question that I've always seemed to have trouble with: "Reverse a string in place."

let rec reverse a =  match a with  | [] -> []  | _ -> (reverse (List.tl a)) @ [List.hd a]


Still fairly newbish with the language, but it took 1 shot and about a minute. No BS pointer arithmetic; straight-forward and easy to remember. Now if only this tl/hd/car/cdr bull could go the way of "let's see how much we can do in one line of code!" perl.

God I hate that stuff.
Previous Entry Firewalls
Next Entry Tangent 1.5
0 likes 2 comments

Comments

Daerax
your code with more sugar and done with safer matching

let rec rev a  =
    match a with 
        | [] -> a
        | x::xs ->  rev xs @ [x]
         


then u can '_ -> failwith "?"'.

March 06, 2008 11:19 AM
Telastyn
Okay, but doesn't the usage of List.hd, etc cause the parameter of the function be a List via inference? There would be a compile-time error if not a list is passed rather than using x::xs/_ -> fail which would be runtime? I will double check tonight.

[edit: The cons style does the same effect, only via matching. Points for Daerax]
March 06, 2008 03:29 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement