myDrop n xs = if n <= 0 || null xs then xs else myDrop (n - 1) (tail xs)
をmyDrop.hsとして保存して、ghci上でロードする。
Prelude> :load myDrop.hs [1 of 1] Compiling Main ( myDrop.hs, interpreted ) Ok, modules loaded: Main. *Main> myDrop 7 [] []
ここまではよい。
ところがmyDrop.hs内で、
myDrop n xs = if n <= 0 || null xs then xs else myDrop (n - 1) (tail xs) main = do print (myDrop 2 "foobar") print (myDrop 4 [1,2]) print (myDrop 0 [1,2]) print (myDrop 7 []) print (myDrop (-2) "foo")
という風に実行しようとするとエラーが出る。print(myDrop 7 [])がいけないらしい。
Ambiguous type variable `a0' in the constraint: (Show a0) arising from a use of `print' Probable fix: add a type signature that fixes these type variable(s) In a stmt of a 'do' expression: print (myDrop 7 [])
printでは空の配列?は表示できないのか。なぜかはよく分からない。