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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:43:19+00:00 2026-05-16T10:43:19+00:00

I am writing a program in Haskell here it is the code module Main

  • 0

I am writing a program in Haskell
here it is the code

module Main
where
import IO
import Maybe
import Control.Monad.Reader
--il mio environment consiste in una lista di tuple/coppie chiave-valore
data Environment = Env {variables::[(String,String)]}deriving (Show)

fromEnvToPair :: Environment-> [(String,String)]
fromEnvToPair (Env e)= e

estrai' x d
|length x==0=[]
|otherwise=estrai x d
estrai (x:xs) d
| (x:xs)=="" =[]
| x== d=[]
| otherwise = x:(estrai  xs d)
--estrae da una stringa tutti i caratteri saino a d
conta'  x d n 
| length x==0 = 0
|otherwise = conta x d n 
conta (x:xs) d n
| x== d=n
| otherwise = (conta  xs d (n+1))
primo (a,b,c)=a
secondo (a,b,c)=b
terzo (a,b,c)=c

estraifrom x d n
|n>=(length x) =[]
| x!!n==d = []
|otherwise = x!!n:(estraifrom x d (n+1))

readerContent :: Reader Environment Environment
readerContent =do
content <- ask
return ( content)

-- resolve a template into a string
resolve :: [Char]-> Reader Environment (String)
resolve key= do
varValue <- asks (lookupVar key)
return $ maybe "" id varValue

maketuple x =(k,v,l) where
k= (estrai' x ':')--usare estrai'

v=estraifrom x ';' (conta' x ':' 1)
l= (length k)+(length v)+2 --è l'offset dovuto al; e al :
makecontext x
| length x==0 = []
| (elem ':' x)&&(elem ';' x)==False = []
|otherwise= (k,v):makecontext (drop l x) where
    t= maketuple x
    k= primo t
    v= secondo t
    l= terzo t



doRead filename = do
    bracket(openFile filename ReadMode) hClose(\h -> do 
        contents <- hGetContents h 
        return contents
        let cont=makecontext contents
        putStrLn (take 100 contents)
        return (contents))
--          putStrLn (snd (cont!!1)))
--          putStrLn (take 100 contents))


-- estrae i caratteri di una stringa dall'inizio fino al carattere di controllo
-- aggiungere parametri to the environment

-- estrae i caratteri di una stringa dall'inizio fino al carattere di controllo
-- aggiungere parametri to the environment



-- lookup a variable from the environment
lookupVar :: [Char] -> Environment -> Maybe String
lookupVar name env = lookup name (variables env)
lookup'  x t=[v| (k,v)<-t,k==x]





fromJust' :: Maybe a -> a
fromJust' (Just x) = x
fromJust' Nothing  = error "fromJust: Nothing"

main = do

file<- doRead "context.txt"-- leggo il contesto
let env= Env( makecontext file) -- lo converto in Environment
let c1= fromEnvToPair(runReader readerContent env)
putStrLn(fromJust'(lookupVar "user" env))
--putStrLn ((lookup' "user" (fromEnvToPair env))!!0)-- read the environment
--putStrLn ("user"++ (fst (c1!!1)))
putStrLn ("finito")
--putStrLn("contesto" ++ (snd(context!!1)))

What I want to do is reading a file formating the content and puting it in Environment, well it read the file and does all the other stuff only if in doRead there is the line
putStrLn (take 100 contents)
otherwise I can not take anithing, somebody knows why?
I do not want to leave that line if I do not know why
thanks in advance
thanks in advance

  • 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-16T10:43:20+00:00Added an answer on May 16, 2026 at 10:43 am

    Using one of the Haskell parser libraries can make this kind of thing much less painful and error-prone. Here’s an example of how to do this with Attoparsec:

    module Main where
    
    import Control.Applicative
    import qualified Data.Map as M
    import Data.Attoparsec (maybeResult)
    import qualified Data.Attoparsec.Char8 as A
    import qualified Data.ByteString.Char8 as B
    
    type Environment = M.Map String String
    
    spaces = A.many $ A.char ' '
    
    upTo delimiter = B.unpack <$> A.takeWhile (A.notInClass $ delimiter : " ")
                              <* (spaces >> A.char delimiter >> spaces)
    
    entry = (,) <$> upTo ':' <*> upTo ';'
    
    environment :: A.Parser Environment
    environment = M.fromList <$> A.sepBy entry A.endOfLine
    
    parseEnvironment :: B.ByteString -> Maybe Environment
    parseEnvironment = maybeResult . flip A.feed B.empty . A.parse environment
    

    If we have a file context.txt:

    user: somebody;
    home: somewhere;
    x: 1;
    y: 2;
    z: 3;
    

    We can test the parser as follows:

    *Main> Just env <- parseEnvironment <$> B.readFile "context.txt"
    *Main> print $ M.lookup "user" env
    Just "somebody"
    *Main> print env
    fromList [("home","somewhere"),("user","somebody"),("x","1"),("y","2"),("z","3")]
    

    Note that I’m using a Map to represent the environment, as camcann suggested in a comment on your previous Reader monad question.

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To toggle play/pause you will need to record the position… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer Canvas has a getHeight() and getWidth() method you can use… May 16, 2026 at 6:48 pm
  • Editorial Team
    Editorial Team added an answer this migth help you code-pretiffy May 16, 2026 at 6:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm writing an audio program in Haskell using Portaudio. I have a function that
I am writing a program to leak memory( main memory ) to test how
For a tool I'm writing ( http://hackage.haskell.org/package/explore ) I need a way to read
I am writing a program that needs to take text input, and modify individual
I'm writing a program and I want the user to be able to specify
I'm writing a program for a school project that is supposed to emulate the
I'm writing a program in Ruby that downloads a file from an RSS feed
I'm writing a program in objective-c but when I build and go the window
I'm currently writing a program that has debug output strewn throughout it. This is
I had a friend ask me about writing a program that would allow data

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.