There are several things wrong with your code. The specific error that you're getting is because your declaration is malformed; you want to do either "let readInteger l ?curVal ..." or "let readInteger (l, ?curVal) ...", but your current placement of the comma won't work.

However, once you try these alternatives you'll see that neither of them will work either. Optional parameters shouldn't be tupled, so the second possibility won't work. However, even in the first case, optional parameters can only be used with members of types, not with let-bound functions, so that also won't work.

There are at least a few other issues; 10 is an integer literal, not an int64 literal, so you want to use 10L instead. Also, in the case where the list contains exactly one character, "rest" is an empty list, so calling rest.Head will throw. Instead, I think it would make more sense to make the recursive call no matter what, but to have the empty list case return currVal instead of 0L.

Here's how I'd write the function:

1
2
3
4
let readInteger s =
  s
  |> Seq.takeWhile System.Char.IsDigit
  |> Seq.fold (fun n c -> n*10L + (int64 c - int64 '0')) 0L
By on 5/2/2010 1:53 PM ()

The specific error that you're getting is because your declaration is malformed; you want to do either "let readInteger l ?curVal ..." or "let readInteger (l, ?curVal) ...", but your current placement of the comma won't work.

Thanks Man!!! I completely misunderstood the format of parameter passing.

"let readInteger l curVal" works well for me except that curVal isn't optional anymore, but that isn't a significant issue for me.

However, once you try these alternatives you'll see that neither of them will work either. Optional parameters shouldn't be tupled, so the second possibility won't work. However, even in the first case, optional parameters can only be used with members of types, not with let-bound functions, so that also won't work.

kvb is that Man!!! I just observed what you described tonight. Thanks for the tip!!!

10 is an integer literal, not an int64 literal, so you want to use 10L instead.

Ooops!!! Thanks for pointing this out!

Also, in the case where the list contains exactly one character, "rest" is an empty list, so calling rest.Head will throw.

Very Good Point!!! I have put in a hack to protect my function. I simply put in another ugly if statement to check to make sure "rest" isn't empty before I dereference it.

Instead, I think it would make more sense to make the recursive call no matter what, but to have the empty list case return currVal instead of 0L.

It probably would but my tiny brain never figured that out, so I used your other ideas and moved on. I ran a few "unit" like tests and the function seems to be behaving as expected.

Here's how I'd write the function:

1
2
3
4
let readInteger s =
  s
  |> Seq.takeWhile System.Char.IsDigit
  |> Seq.fold (fun n c -> n*10L + (int64 c - int64 '0')) 0L

That is breathtakingly beautiful!!! I seemed to have forgotten how "fold" works. It's use certainly makes your solution much more aesthetic than mine!

KVB is the Man!!!

By on 5/3/2010 7:57 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper