Coming from a Matlab and R background where the development process is very interactive (select, run selection, fix, select, run selection, fix, etc), I'm trying to figure out how F# handles this style of development, which seems pretty important in scientific applications. Here are few things that just immediately come to mind to somebody new to F#:
Selecting multiple lines gives different results than one line at a time.
let add x y = x + y add 4.1 2.3
Selecting both lines results in
float -> float -> float
whereas selecting the first line results inint -> int -> int
. More generally, matlab/R users are used to results printing out after each statement, not at the end.Shadow copying can become burdensome.
let file = open2GBfile('file.txt') process file
If you run this interactively over and over again开发者_开发技巧, the 2GB file is shadow copied and you will quickly run out of memory. Making file mutable doesn't seem like the appropriate solution, since the final run of the program will never change it.
Given these issues, is it impossible for a fsi.exe
based system to support matlab/R style interactive development?
[Edit: I am guessing about 2. Do objects get marked for deletion as soon as they are shadowed?]
I wouldn't expect F# to be a drop-in replacement for Matlab/R, because unlike them, F# is a general purpose programming language. Not everything you need for a specific type of work will be in the standard libraries. But that doesn't mean that the "interactive development" you describe is impossible, it may just require some effort up-front to build the library functions you depend on.
For #1, as was mentioned earlier, adding type annotations is unfortunately necessary in some cases, but also the inline
keyword and "hat-types" can give you duck-typing.
For #2, I'm not clear on what your open
and process
functions do, versus what you want them to do. For example, the open
function could:
- Read the entire file at once, return the data as an array/list/etc, and then close the file
- Return a
FileStream
object, which you're callingprocess
on but forget to close. - Return a sequence expression so you can lazily iterate over the file contents
- Memoize the result of one of the above, so that subsequent calls just return the cached result
- One of the gazillion other ways to create an abstraction over file access.
Some of these are better suited for your task than others. Compared to Matlab & R, a general purpose language like F# gives you more ways to shoot yourself in the foot. But that's because it gives you more ways to do everything.
To #1
In FSI, you'll have to type ;;
at the end of each statement and get the results directly:
> 1 + 2;;
val it : int = 3
Generally an F#-Codefile should be seen as a collection of individual functions that you have to call and evaluate interactively and not as a series of steps that produce values to be shown.
To #2:
This seems to be a problem of the code itself: Make file
a function, so the reading/copying is only done when and where really needed (otherwise the let binding would be evaluated in the beginning).
精彩评论