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 6590897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:21:26+00:00 2026-05-25T17:21:26+00:00

My code doesn’t compile. It seems that the problem lies in the main function

  • 0

My code doesn’t compile. It seems that the problem lies in the main function with lookup.
I am trying to parse an file that look like this :

auiauiaui@tsrntsr.epep 584756 “srtrtte”
jepeto@pinokio.disney 464456454 “cendrillon”

If you name the file lol and the code prog then calling prog lol 1 2 will return 464456454.

EDIT :

Thanks,

See I have a file named filetest like this :
field1-1 field1-2 field1-3
field2-1 field2-2 field2-3
field3-1 field3-2 field3-3
field4-1 field4-2 field4-3
field5-1 field5-2 field5-3

Now my piece of code is suppose to grab a field given the line and the rank.
mycode filetest give me the number of lines in the file (so that i can use a script for each lines).

Here some example :
mycode filetest 1 2 -> field1-2
mycode filetest 2 2 -> field2-2
mycode filetest 3 4 -> field3-4
mycode filetest 3 2 -> field3-2
mycode filetest 2 5 -> field2-5
mycode filetest 1 4 -> field1-4

Now each field contain a String.

What i do is grab the file as a string then grab the line wanted (with lines) and then grab the field (with words).

I corrected my errors (thanks to the #haskell and #haskell-fr channel and you). But my code doesn’t work as expected at all.

Any help shall be appreciated. Here is the néw code :

import System.Environment
import System.IO

fieldArg :: Int -> [String] -> String
fieldArg x a = if length a > 2
                then if x < 4 then a !! x else "field number is too high"
                else "field number is too low"

lineArg :: Int -> [String] -> String
lineArg x a
    | x > length a = "line number is too big"
    | x < 1        = "line number is too low"
    | otherwise    = a !! x

extrcta :: Int -> Int -> String -> String
extrcta x n = fieldArg x . words . lineArg n . lines


argList :: String -> [((Int, String), String -> String)]
argList n = let n1 = read n
            in [((1, n), extrcta 1 n1)
               ,((2, n), extrcta 2 n1)
               ,((3, n), extrcta 3 n1)
               ]


main :: IO ()
main = do
        args <- getArgs
        case args of
                   (path: opt1: opt2: _) -> case (read opt1, opt2) `lookup` argList opt2 of
                                                                                       Nothing -> putStrLn $ "Wrong argument " ++ opt1 ++ " or " ++ opt2
                                                                                       Just act -> withFile path ReadMode (\handle -> do
                                                                                                                                       contents <- hGetContents handle
                                                                                                                                       putStrLn $ act contents)
                   (path: _) -> withFile path ReadMode (\handle -> do
                                                                    contents <- hGetContents handle
                                                                    (print (length $ lines contents)))
                   _ -> putStrLn "Wrong number of argument"

EDIT2 : Thanks

  • 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-25T17:21:27+00:00Added an answer on May 25, 2026 at 5:21 pm

    The problem is in the 4th line of main: opt1 and opt2 are strings but argList returns a list with integer pairs. To fix this you could change argList to:

    argList :: String -> [((String, String), String -> String)] 
    argList n = 
        let n1 = read n
        in [(("1", n), extrcta 1 n1)
           ,(("2", n), extrcta 2 n1)
           ,(("3", n), extrcta 3 n1)
           ]   
    

    Now it compiles (but still does not seem to work as intended; don’t understand what your algorithm is supposed to do).

    UPDATE corresponding to the updated question: That’s too complicated. Just drop that argList thing and do something simple as:

    getRow :: Int -> String -> String
    getRow n = (!!n) . lines
    
    getField :: Int -> String -> String
    getField n = (!!n) . words
    
    getValue :: String -> String -> String -> String
    getValue row field src = getField (read field) $ getRow (read row) src
    
    main :: IO () 
    main = do
        args <- getArgs         
        case args of                    
            (path: opt1: opt2: _) -> do
                src <- readFile path
                putStrLn $ getValue opt1 opt2 src
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code doesn't compile for obvious reasons, namely that Foo is trying to
I know why the following code doesn't compile: public class Main { public static
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
Edit: Of course my real code doesn't look exactly like this. I tried to
I have a colleague who insists that his code doesn't need comments, it's "self
this code doesn't compile. I'm wondering what I am doing wrong: private static Importable
The following code doesn't work as I intuitively expect it to: function MyObject(input) {
The following code doesn't compile ( error CS0123: No overload for 'System.Convert.ToString(object)' matches delegate
Hi my code doesn't work, Im trying to group my blogentries by year and

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.