0

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?

4
  • 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+'. Commented Sep 20, 2011 at 3:14
  • 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 Commented Sep 20, 2011 at 3:22
  • 2
    Yep. Alt+Enter sends the selected text to FSI. Alt+' sends the current line. Commented Sep 20, 2011 at 3:26
  • 1
    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. Commented Sep 20, 2011 at 14:09

2 Answers 2

1

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 ;;.

Sign up to request clarification or add additional context in comments.

Comments

0

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"
> 

1 Comment

you actually should only need the ;; at the end of the last block, also, using the Visual studio hotkeys the ;; should be there automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.