Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:17:43+00:00 2026-05-23T06:17:43+00:00

hello I am very beginer of haskell I am making GUI program that open

  • 0

hello I am very beginer of haskell

I am making GUI program that

  1. open file selection dialog

  2. take word

  3. search the word in selected txt file

  4. print number of found to label

but I stuck with error which I can’t solve it

I paste error and code here

could somebody please help me?

thank you

the full code is 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

the error is 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:17:44+00:00Added an answer on May 23, 2026 at 6:17 am

    fileChooserGetFilename can’t always return a filename (the user might click on “cancel” for example). For that reason its return type is Maybe FilePath, not FilePath. So if a file was chosen, it returns a Just containing the FilePath. If no file was chosen it returns Nothing.

    However readFile takes a FilePath as an argument, not a Maybe FilePath (calling readFile with Nothing makes no sense).

    So what you need to do is you need to pattern match on targetFile. If it is Nothing, you need to handle that somehow (you could print an error message, or just keep asking the user for a file until he picks one), and if it’s a Just, you take the FilePath it contains and feed that to readFile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compile a very basic hello world level Cuda program under Linux.
Hello i have structures declared in the same Header file that need eachother. struct
this is my very first android program, it's a modified hello world. I'm running
I have a String that looks like: Hello my is Joeseph. It is very
I just installed Ubuntu 8.04 and Eclipse. I made a very simple Hello World
Hello I am compiling a program with make but I get the error of
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello I am working with a simulator that uses rcS scripts to boot, this
i'm creating a very simple (hello World quality) web application using spring mvc 3.0.
I am having a hard time consuming a very simple (Hello World) WCF web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.