I am learning Haskell and I am having a hard time performing the following task.
I have a script that gives me a directory. I would like to call that script, get the directory and then use the "cd" in Turtle to change the directory. Below is the code that I currently wrote but it fail to compile.
It says lineToText targetDirLine expects Line and not Maybe Line . I do not understand why fold (inshell "getSomeDir" empty) Fold.head produces Maybe (Maybe Line).
Even after fixing this error I get other issue using cd.
#!/usr/bin/env stack
-- stack --resolver lts-10.2 script
{-# LANGUAGE OverloadedStrings #-}
import qualified Control.Foldl as Fold
import Turtle
getDirAndCd :: MonadIO io => io ()
getDirAndCd = cd fp
where (Just targetDirLine) = fold (inshell "getSomeDir" empty) Fold.head
fp = fromText (lineToText targetDirLine)
What am I missing?
If you inspect the type of the
foldexpression with GHCi:you'll see that it's not a
Maybe Line, it's anio (Maybe Line). When you write, in yourwhereclause:it tries to match the
Maybeon the left with theioon the right, and soio (Maybe Line)unifies withMaybe (Maybe Line), and that's why you get the type error you do.You'll want to process the
foldexpression in a<-line in thedo-block:This will result in another error,
Could not deduce (MonadFail io). The issue here is thatJust targetDirLine <- ...represents a pattern that could fail, iffoldreturns aNothing. If you add aMonadFail ioconstraint, it type checks: