0
votes

How can I execute F# function in the interactive window?

If I create a .fsx file and type following code:

open System

let parseTitle (line : string) =
    let startIndex = line.LastIndexOf('(')+1
    let endIndex = line.LastIndexOf(')')
    line.Substring(startIndex,endIndex-startIndex-1)

let testTitle = "The Possessed (The Devils) (Fyodor Dostoyevsky)"

parseTitle testTitle

Then select it all and press Alt+Enter. Nothing happens. When I set the cursor to the last line and press Alt+Enter I get following error:

error FS0039: The value or constructor 'parseTitle' is not defined

What am I doing wrong?

2
Have you tried right-click > Send to Interactive also? It works for me. To send a single line to FSI (without selecting it) use Alt+'. - Daniel
Yes, I've only been sending it to the interactive window by doing Alt+' actually! Does that only work with a single line? With selecting "Send to Interactive" it works! Thanks - lukebuehler
Yep. Alt+Enter sends the selected text to FSI. Alt+' sends the current line. - Daniel
For what it's worth, double-check your key bindings. Sometimes VS extensions re-map the default key bindings. Also, the default key-bindings differ based on which "profile" you select on first-run. - pblasucci

2 Answers

1
votes

To use code from some given file, you only need to highlight it, right click, and then select "Send To Interactive". This will send the entire selection to the interactive window, as opposed to just one line. Enigmativity's answer is incorrect, as you don't need to modify your file in order to send a snippet to the interactive window. What he was referring to was this: if you type some code by hand in the interactive window, you need to end your statements with a double semi-colon ;;.

0
votes

You need to key ;; at the end of each line (or block) to execute the code.

open System;;

let parseTitle (line : string) =
    let startIndex = line.LastIndexOf('(')+1
    let endIndex = line.LastIndexOf(')')
    line.Substring(startIndex,endIndex-startIndex-1);;

let testTitle = "The Possessed (The Devils) (Fyodor Dostoyevsky)";;

parseTitle testTitle;;

I then get:

> 
val parseTitle : string -> string

> 
val testTitle : string = "The Possessed (The Devils) (Fyodor Dostoyevsky)"

>
val it : string = "Fyodor Dostoyevsk"
>