Haskell simple GUI: "can't match X vs Maybe X"

Hi, i am very starting haskell

I am making a GUI program that

  • open file selection dialog

  • take the word

  • find the word in the selected txt file

  • print number found for tag

but I am stuck with an error that I cannot solve.

Paste the error and code here

Can someone please help me?

Thank you

full code here

--GUI routine
import Graphics.UI.Gtk
import Text.Regex.Posix ((=~))
import Control.Monad (when)
--core routine
matchWord :: String -> String -> Int
matchWord file word = length . filter (== word) . concat $ file =~ "[^- \".,\n]+"
--main start
main :: IO ()
main =
      do initGUI
         win <- windowNew
         windowSetTitle win "WORD SEARCHER"
         win `onDestroy` mainQuit

         fch <- fileChooserWidgetNew FileChooserActionOpen
         containerAdd win fch 

         targetFile <- fileChooserGetFilename fch --wrong?

         ent <- entryNew
         btn <- buttonNew
         st <- labelNew $ Just "Found : 0      "

         col <- vBoxNew False 5
         containerAdd col ent
         containerAdd col btn
         containerAdd col st    

         buttonSetLabel btn "Click to search"

         btn `onClicked` do targetWord <- entryGetText ent
                            fileData <- readFile targetFile
                            found <- matchWord fileData targetWord
                            labelSetText st found
         containerAdd win col
         widgetShowAll win
         mainGUI

mistake here

gui-word-search.hs:33:49:
    Couldn't match expected type `FilePath'
       against inferred type `Maybe FilePath'
    In the first argument of `readFile', namely `targetFile'
    In a 'do' expression: fileData <- readFile targetFile
+3
source share
1 answer

fileChooserGetFilename (, "" ). Maybe FilePath, FilePath. , , Just, FilePath. , Nothing.

readFile FilePath, Maybe FilePath ( readFile Nothing ).

, , targetFile. Nothing, - ( , ), Just, FilePath readFile.

+3
source

All Articles