我应该避免使用交互模式吗?

Should I avoid using interactive mode?

Haskell 这里是新手。我用ghci实现一个宝贝quicksort算法[1]如下:

Prelude> quicksort (firstx:xs) = quicksort[x|x<-xs, x<firstx] ++ [firstx] ++ quicksort[x|x<-xs, not(x<firstx)]
Prelude> quicksort [] = []

命令quicksort [1,2,3]然后报错:

*** Exception: :8:1-17: Non-exhaustive patterns in function quicksort

但是,在 quicksort.hs 文件和 运行 $ ghci quicksort.hs 中键入完全相同的内容不会产生这样的错误。

问题

有什么区别?避免使用交互模式是经验法则吗?

参考

[1] 雷克斯佩奇。 Haskell

中的两节简短课程

在文件中定义多行函数然后加载 :l <module> 或重新加载 :r 通常更容易,但有时我会直接在 GHCi 中声明多行函数,以 :{:}:

:{
quicksort (firstx:xs) = quicksort[x|x<-xs, x<firstx] ++ [firstx] ++ quicksort[x|x<-xs, not(x<firstx)]
quicksort [] = []
:}
如果您在 GHCi 中忘记了任何命令,

:h 很有用。

编辑:忘记解决问题的第二部分。

Is it a rule of thumb that one should avoid using interactive mode?

交互模式是一个很棒的工具,我在 ReasonML 等其他没有交互模式的生态系统中怀念它。没有理由避免它。如果您想保留一些代码,请记住复制您的代码。