The literal translation is:

1
Seq.find ((=) 3) {0 .. 5}
By on 10/19/2007 5:15 PM ()

I was disappointed to find no break statement, too. It's so ingrained that it took me a few minutes to figure a way around it. Settled on a while loop with a bDone mutable and several inner if blocks, rather than going the functional approach.

I imagine that OCaml users would use a try/with block and raise instead - but OCaml's exception handling is much more efficient than .NET, from what I hear.

Support for 'break' would be a nice addition. I'd also like to see repeat/until (but mostly because my nose has been in a book with Pascal examples lately ;-).

-- James

By on 10/15/2007 7:41 PM ()

I think you can use workflow sequence expressions to remove most of the need for break/continue statements or mutable state. However there still a little experimental not fully documented yet, so I can’t tell you exactly how they would replace them ;)

The below is workflow sequence to read lines from a file:

1
2
3
4
let reader =
    seq { use reader = StreamReader(File.OpenRead("test.txt"))
          while not reader.EndOfStream do
              yield reader.ReadLine() }

Also as Julien points out in his earlier post its possible to avoid mutable state using tail recursion, and while exceptions are more expensive in .NET than in OCaml and generally should be avoid for flow control, you can generally avoid using them by return an option type instead of throwing an exception.

By on 10/16/2007 12:49 AM ()

Hi,
the first one can be written using "Seq.first" function (which returns some value for the first element that matches given condition):

1
2
3
4
5
6
7
 
// Returns "Some(3)"
let test = 
  { 0 .. 5 } |> Seq.first (fun i -> if (i = 3) then Some(i) else None);;

// If you want just true/false then you can use:
let test2 = test |> Option.is_some

The second can be written using "Seq.exists" (which tests whether a sequence contains an element matching some condition):

1
2
3
4
5
6
7
 
let test = 
  { 0 .. 5 } |> Seq.exists (fun i -> i = 3)

// or you can use slightly shorter syntax:
let test = 
  { 0 .. 5 } |> Seq.exists ((=) 3)

Also note, that using List.foo and [ 0 .. x ] instead of Seq.foo and { 0 .. x } may be sometimes more efficient (especially for simple examples like this), but this indeed depends on the data type you want to use.

By on 7/30/2007 7:35 AM ()

Thanks a lot.

By on 7/31/2007 12:44 AM ()

This is a duplicate post that will hopefully be deleted soon

my apologies

By on 7/31/2007 6:09 AM ()

more alternatives

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 let f min max found = 
  let i = ref min
  let flag = ref true 
  while !i <= max && !flag
    flag := (!i <> found)
  not (!flag)

let f min max found = 
  let rec aux i =
    if i > max 
    then false 
    else 
      if i = found then true else aux (i+1)
  aux min
By on 7/31/2007 6:09 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