简体   繁体   English

如何在交互式窗口中使用af#函数?

[英]How can i use a f# function in the interactive window?

How can I execute F# function in the interactive window? 如何在交互式窗口中执行F#函数?

If I create a .fsx file and type following code: 如果我创建一个.fsx文件并键入以下代码:

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. 然后全部选中,然后按Alt + Enter。 Nothing happens. 什么都没发生。 When I set the cursor to the last line and press Alt+Enter I get following error: 当我将光标置于最后一行并按Alt + Enter时,出现以下错误:

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

What am I doing wrong? 我究竟做错了什么?

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 ;;. 他指的是:如果在交互式窗口中手动键入一些代码,则需要以双分号;;结尾。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM