Why does getArgs
gets evaluated after the method argument of fmap
?
main::IO()
main=do
fpath<-fmap head getArgs
putStrLn fpath
I get the error :
Exception: Prelude.head: empty list
It seems it applies head on something that has not been computed yet.
I first assumed that it might be another rule about lazyness that i am not aware of being new to Haskell so i tried with :
a<-fmap head getLine
# no problem
a<-fmap head (readFile [filename])
# again no problem
So why is getArgs
special that gets evaluated after ?
If head
breaks on empty list, that means that getArgs
did already evaluate, because the value []
was produced and matched by head
.
Most probably you ran your program from ghci
, which can produce such an effect. Since head
is unsafe, you should check whether there's at least one argument on the list present.