Hi,
In F#, you typically don't use 'output' parameters. In F#, function returns all its outputs as the result of the function. This is because in F#, most of the things are immutable, which means that you don't change value assigned to some symbol later in the program. You can use tuples to return multiple values (boolean flag and the answer) like this:

1
2
3
4
5
6
 
let eat animal =
  match animal with
  | "goat" -> (true, "cabage")
  | "wolf" -> (true, "goat")
  | _ -> (false, "") // unknown animals don't eat anything

In fact, you can make the example simpler by using option type, which can either have value "None" or can carry value (for example Some(123)):

1
2
3
4
5
6
 
let eat animal =
  match animal with
  | "goat" -> Some("cabage")
  | "wolf" -> Some("goat")
  | _ -> None // we don't know what to return

Then you can use pattern matching to process the result:

1
2
3
match eat("goat") with
| Some(food) -> printfn "goat eats %s" food
| None -> printfn "goat is on diet!"

Regarding your second question, you can't write the code like this. You'd have to store the facts in some data structure such as list of tuples and then write functions that work with this data structure (such as finding who eats what):

1
2
3
4
5
 
let data = [ ("goat", "cabage"); ("wolf", "goat") ]

// find what goat eats:
let (who, what) = List.find (fun (who, what) -> who = "goat") data

If you're new to functional programming, you'd maybe find the following webcast useful: [link:tomasp.net].

By on 5/27/2009 5:52 PM ()

thank you for your detailed responce. I worked long time on Prolog and I see that here methods little different. Any way I want to learn all aspects to use in the future. In my case the last code with anonimous function is better because it allows tp match by first variable or by second. In pther cases other code also can be good/

What I expect on the next step. I want to solve tasl about sailor who can put in his bpat only 2 items. In Prolog I can write set of predicates and using back tracking I can solve puzle. Here at this time I see deterministic way. However I think if I proper;y setup match I can solve problem functionally. At least until I wrote the text I found some answers.

Thank you.

By on 5/27/2009 7:18 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