Although a nicer way to write this code in F# would be

1
let loopFn k = Seq.exists (fun x -> bool1 x && bool2 x) { k..20 }
By on 11/13/2007 10:36 PM ()

Greg,

Thanks for your responses. It works for the example that was provided, but in the real example that I had, there are some intermediate steps between the evaluations of bool1 k and bool2 k, so it becomes too cumbersome to evaluate them at the same line. Essentially we have an function where deep in the function, if a certain condition is satisfied, one can exit the function. I was not able to determine how if one is performing a Seq.iter calculation, whether one can stop the calculation in the middle. It seems that there needs to be a break command.

What I still do not understand is why this code is not correct.

1
2
3
4
5
6
7
let rec loopFn k  = match k with
                    | 20 -> 0
                    | j -> if bool1(k) then
                               // Do some intermediate processing
                                if bool2(k) then
                                     1
                             loopFn (k+1)  

Thanks

Chris

By on 11/14/2007 7:10 AM ()

That's not a problem.

1
2
3
4
5
6
7
let loopFn k = 
  let p x = 
    if bool1 x then 
      // Do some intermediate processing
      bool2 x
    else false
  Seq.exists p { k..20 }

It is not possible to break from a Seq.iter calculation unless you throw an exception or something, but it is probably better to use a library function that does support breaking (e.g., exists, tryfind) than to do that.

By on 11/14/2007 5:23 PM ()

Hello.

The code is not correct because when using an "if" you have to necessarily use an "else", unless you don't return anything on the "if" statement.

Not sute if i understood exactly wath your problem is about the "break", but if it's just stoping a function when "something" happens, you can do it raising exceptions per example.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
exception Invalid_data;;

let rec ler vector conta n =
 
 if conta <= n then (
 
  vector.(conta) <- read_float(); 
  
  if ((vector.(conta) < -200.0) || (vector.(conta) > 200.0)) then raise Invalid_data
  else
   ler vector (conta+1) n
   )
;;

Hope i helped you with this :)

Regards.

PS: how do i put the sintax highligh for F# when posting a reply? :P

By on 11/14/2007 7:46 AM ()

Robert already gave the explanation. You'll probably understand it once you see the corrected version:

1
2
3
4
5
6
7
8
let rec loopFn k = match k with
                   | 20 -> 0
                   | j ->
                       if bool1(k) then
                           // Do some intermediate processing
                           if bool2(k) then 1
                           else loopFn (k + 1)
                       else loopFn (k + 1) 

Best regards,
Stephan

By on 11/14/2007 7:26 AM ()

How about:

1
2
3
4
5
6
7
8
let rec loopFn k  = 
    match k with
    | 20 -> 0
    | j -> 
        if bool1(k) && bool2(k) then
          1
        else
          loopFn (k+1)

The basic problem is that as "if then else" is an expression so when you are returning a value from an "if then else" you must provide an else clause.

By on 11/13/2007 10:25 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