Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell: writing the result of a computation to file
    text
    copied!<p>I have a function which creates a tuple after computation, but I would like to write it to file. </p> <p>I know how to write to a file using <code>writeFile</code>, but do not know how to combine the computation and monads IO together in the type signature.</p> <p>This is my code.</p> <pre><code>invest :: ([Char]-&gt;Int-&gt;Int-&gt;([Char], Int) ) -&gt; [Char]-&gt;Int-&gt;Int-&gt;([Char], Int) invest myinvest x y = myinvest x y myinvest :: [Char]-&gt;Int-&gt;Int-&gt;([Char], Int) myinvest w x y | y &gt; 0 = (w, x + y) | otherwise = error "Invest amount must greater than zero" where </code></pre> <p>I have a function which computes the maximum value from a list, but I want this function to receive input from a file, and then perform the computation of maximum value.</p> <pre><code>maximuminvest :: (Ord a) =&gt; [a] -&gt; a maximuminvest [] = error "Empty Invest Amount List" maximuminvest [x] = x maximuminvest (x:xs) | x &gt; maxTail = x | otherwise = maxTail where maxTail = maximuminvest xs </code></pre> <p>Please help. Thanks. </p> <p>[Edit]</p> <ol start="3"> <li>My new question is at below. </li> </ol> <p>The first and second question can be solve through function composition but when i try it say type mismatch.</p> <p>I have check it but i couldn't find any errors.</p> <pre><code>invest :: ( [Char]-&gt;Int-&gt;Int-&gt;([Char], Int) ) -&gt; [Char]-&gt;Int-&gt;Int-&gt; ([Char], Int) invest theinvest x y = theinvest x y theinvest :: [Char]-&gt;Int-&gt;Int-&gt; ([Char], Int) theinvest w x y | y &gt; 0 = (w, x + y) | otherwise = error "Invest amount must greater than zero" savefile :: ([Char], Int) -&gt; IO() savefile (x, y) = do let name = fst (x, y) let temp = snd(x, y) let amount = show temp writeFile "C:\\Invest.txt" (name ++ " " ++ amount) test = savefile . theinvest "asd" 1234 234 </code></pre> <p>The error message is</p> <pre><code> ERROR - Type error in application * Expression : savefile . invest theinvest "234" 234 234 Term : invest theinvest "234" 234 234 Type : ([Char],Int) * Does not match : a -&gt; b </code></pre> <p>Please help. My return type is <code>([Char],Int)</code>. Why it complaint as <code>a -&gt; b</code> ? Thanks</p> <p>I solve this using command like savefile (invest theinvest "asd" 12 12) but why the operator doesn;t works ? </p> <ol start="4"> <li><p>My fourth question is i have something like this ["peter","1000","michell","2000","kelly","3000"] and i would like to convert to [("peter",1000),("michell", 2000),("kelly",3000)]</p></li> <li><p>The reading of file content is ok but i want to filter the string and get the number only. For instance, a file that has "peter 100\nasd 200"</p></li> </ol> <p>I would like to drop the alphabet and remain the integer here. I just want the [100, 200] to be the argument the function.</p> <p>Please help. </p> <p>Thanks. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload