Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive functions for sequences in F#

Rather trivial question, but a quick google search didn't give me the answer.

What is a standard way to write a recursive functions for sequences? For lists you would do pattern matching with empty list and head+tail pattern, what is the equivalent for sequences?

like image 969
Grzenio Avatar asked Jul 12 '12 12:07

Grzenio


1 Answers

There is no standard way to do so since you rarely write recursive functions for sequences.

You should look at a variety of high-order functions in Seq module. They are often more than adequate, so you don't have to write recursive functions by yourself.

To generate sequences recursively, sequence expression is a simple and intuitive way to go:

let rec allFiles dir =
    seq { yield! Directory.GetFiles dir
          for d in Directory.GetDirectories dir do
            yield! allFiles d }

If you have to break down a sequence and manipulate it recursively, you are doing it wrong. You should either manipulate List or LazyList from F# PowerPack, and convert results back to sequences.

like image 125
pad Avatar answered Sep 28 '22 16:09

pad