在哈斯克爾中等於(=)Vs左箭頭(< - )符號
工作程式碼:
import System main = do [file1, file2] <- getArgs --copy file contents str <- readFile file1 writeFile file2 str
崩潰程式碼:
import System main = do [file1, file2] = getArgs str = readFile file1 writeFile file2 str
當我嘗試,它丟擲一個錯誤:
a.hs:6:18: parse error on input ‘=’
那麼<-from =
要了解真正的區別,你必須瞭解僧侶在他們的答案中描述的monads,以及褻瀆的描述.
對於IO monad的具體情況,與您的getArgs示例一樣,粗略但有用的直覺可以如下進行:
> x<- 操作執行IO操作,獲取其結果,並將其繫結到x
> let x = action定義x等於action,但不執行任何東西.稍後,您可以使用y<-x表示y-- 動作.
來自允許關閉的命令式語言的程式員可能會用Javascript進行粗略的並行比較:
var action = function() { print(3); return 5; } // roughly equivalent to x <- action print('test 1') var x = action()// output:3 // x is 5 // roughly equivalent to let y = action print('test 2') var y = action// output: nothing // y is a function // roughly equivalent to z <- y print('test 3') var z = y()// output:3 // z is 5
再次:這個比較只關注於IO.對於其他monads,您需要檢查>> =實際上是什麼,並考慮對do的否定.
http://stackoverflow.com/questions/28624408/equal-vs-left-arrow-symbols-in-haskell