In F# you use the keyword "and" to show that functions are mutually recusive. You will also need to add the keyword "rec" before the first function definition to show that it is recusive.

1
2
3
4
5
#light
let rec foo() =
    bar()
and bar() =
    foo()

I actually find its rare that you need to do this, generally you can factor you code so each function appears in the order that its used. Also if a function foo need to use a function bar it maybe better to define it as an inner function of foo. If foo is recurise bar can also call foo.

1
2
3
4
5
#light
let rec foo() =
    let bar() = ()
    bar()
    bar()

Cheers,
Rob

By on 7/11/2007 8:04 AM ()

Hi Rob,

Thanks for your reply. I am not so concered about mutally recursive functions but rather just the order that definitions are found in. Perhaps a concrete example?

Let us say, for instance, that I am writing a program that sorts lists but uses my own comparison function

so

1
2
3
4
5
#light 

let myListSort someList = List.sort myCompare someList

let myCompare listEl1 listEl2 = ...

Now it would seem that the previous code will not compile, even though it is the most natural way of writing it. Instead I would need to write

1
2
3
4
#light 

let myCompare listEl1 listEl2 = ... 
let myListSort someList = List.sort myCompare someList

While it is true that I coudl use an embedded/inner function in this case, or just re-order the function definitions (as above) in some instances both of these options just make the code messy and hard to read. So I am wondering how I can get around this. In the C/C++ world function prototypes perfom this function. I assume that F# must have a way of doing it too?

Thanks,

Matt

By on 7/11/2007 3:36 PM ()

Hi Matt,

I pretty certain there is no way to switch the order of function definitions like that, it's just the way ML style languages do things, OCaml and SML have the same constraints. Perphas its just because I've been ML hacking for a while now the second way of writing actually looks more natural to me. Although in most cases you'd probably something like:

1
2
#light 
let myListSort = List.sort (fun e1 e2 -> ...) 

This myListSort is still a function that takes a list and returns a custom sorted version, because this is the function returned by applying "List.sort" to a custom compare function. In my opioin this means the reader of the code concentracts there efforts on understanding the important bit of the function mySortedList - the custom compare implementation.

Cheers,
Rob

By on 7/12/2007 12:47 AM ()

Hi Rob,

Thanks for your reply. Having looked over a few of the sample applications I must say I have never seen anything along the lines of a function prototype so I probably should have picked up on it straight away! Anyway, there is nothing like talking to a real person!

I must say though, (and itt has been a long time but...) I seem to remember that Haskell does a multi pass compilation of some sort that avoids these sorts of problems. No need for ordered definitions and no need for function prototypes? Maybe that is why I thought it was possible... :( Maybe I am just making things up!

Matt

By on 7/12/2007 6:33 AM ()

Hi Matt,

No worries, I actually like talking about F# so feel free to pose any more questions.

I don't know enought about Haskell to say how this could be handled in it. I know Haskell has influnce the design of F# a bit, more the in the sorts of functions that are available in the base library.

Cheers,
Rob

By on 7/13/2007 2:35 AM ()
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