Jump to content
  • Advertisement
  • entries
  • comments
  • views

Adventures in F# Part 1.5

Sign in to follow this  


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.
Sign in to follow this  


Recommended Comments

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 "?"'.

Share this comment

Link to comment
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]

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!