I believe you can use takeWhile after map, before min.

Of course, takeWhile if you read the haskell source is done with recursion. I believe pattern matching with terminating condition in recursion is the standard metaphor for 'break/continue' in FP.

The beauty though is it is expressed in a declarative what that is easier to read.

By on 11/14/2008 7:39 AM ()

I don't think inserting takeWhile between map and min does the trick.

Also, the seq<_> interface is not directly amenable to recursion (at least not without introducing horrible inefficiencies), only the IEnumerator interface is. This is probably why there's no way to dissect sequences via pattern matching.

By on 11/14/2008 8:04 AM ()

Sorry, I wasn't thinking when I wrote that last post. takeWhile *does* do the trick, of course.

By on 11/14/2008 12:43 PM ()

For this kind of loop you'll have to directly use the IEnumerator interface in F#:

1
2
3
4
5
6
7
    let mutable min = System.Double.MaxValue
    use iter = seq.GetEnumerator()    
    while iter.MoveNext() && min > 0.0 do
        let x = iter.Current
        let fx = f1 x
        if fx < min then
            min <- fx

In general, the lack of break, continue and return in F# will make your imperative algorithms uglier and slower. But given the other advantages of F# you'll learn to live with that ;-)

By on 11/13/2008 4:01 AM ()

In F# you can use Seq.unfold, which allows you to create a sequence and terminate it by returning None. See the docs for more details:

[link:research.microsoft.com]

By on 11/13/2008 12:55 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