Re: reusing vs. interact

2000-09-25 Thread Jeffrey Straszhiem

On Mon, Sep 25, 2000 at 05:17:15PM +0200, Jose Emilio Labra Gayo wrote:

 I had an interactive program which used a function of type

  prg  :: String - String
  prg = 
{- for example -} \(c:cs) - "first char = " ++ [c]

 To run that program I used

  run :: IO ()
  run = interact prg

 Now, I want to reuse that function in a shell program which will
 execute the interactive function more than once (as an example,
 suppose that the shell program executes 'run' twice)

  main = run  run

 However, after the first run, stdin gets semi-closed, and the second
 run fails with an Illegal operation.

You can't do this directly for the reasons you state.  Here are two
options:

 1. If you know offhand what will divide the first set of input from
the next then write a function with signature:

split :: String - (String, String)

which returns the first half and the second half.  Then you do
this:

main = do (f, s) - split getContents
  putStr (prg f)
  putStr (prg s)

 2. If you do not know what will split your input, you'll have to
modify your prg function to have this signature:

prg :: String - (String, String)

Where the first part of the result is the result calculated, the
second is the remainder of the input stream.  Then main could look
like this:

main = do (rs, rm) - prg getContents
  putStr rs
  (rs', _) - prg rm
  putStr rs'

It's not as pretty, but it will get you what you want.

Note: I haven't tried any of this :)

-- 
-- Jeffrey Straszheim  |  A sufficiently advanced
-- Systems Engineer, Programmer|  regular expression is
-- http://www.shadow.net/~stimuli  |  indistinguishable from
-- stimuli AT shadow DOT net   |  magic




Re: code generation for other languages

2000-08-22 Thread Jeffrey Straszhiem

On Wed, Aug 23, 2000 at 12:26:38PM +1000, Timothy Docker wrote:
 
 I'm writing a haskell program that generates C++ code based upon
 some haskell data structures. At the moment, the code is somewhat
 ugly with lots of stuff like
 
   mutatorDef structName (name,vtype) = 
   "inline void\n" ++
   structName ++ "::" ++ (mutatorName name) ++
   "( " ++ (cppParamType vtype) ++ " v ) {\n" ++
   "" ++ (storageName name) ++ " = v;\n" ++
   "}\n\n"
 
 All those ++ operators working on raw strings bug me, and manually
 getting the indentation correct is a pain. Is there a more
 functional approach to generating source code? I thought this could
 be a common enough task that there could be a library, but a perusal
 of haskell.org didn't seem to show anything relevant.

To make matters worse, you're likely getting lousy efficiency with all
of the ++ operators.  Look through the code for showS in the Prelude
for better ways to hook strings together.  Paul Hudak talks about the
efficient use of the show functions at:

 http://www.haskell.org/tutorial/stdclasses.html

Now, with regard to the code being ugly, my suggestion would be to
check out some of the pretty printer libraries out there, and to look
through them.  They basically solve a similar problem, except they
first parse a normal program into a tree, then flatten the tree in a
standard way.  In your case you'll likely build the tree directly,
then call the final stages of the pretty printer.  There is no
shortage of pretty printer libraries for Haskell, or for FP in
general.

Hope this helps, at least a little :)

-- 
-- Jeffrey Straszheim  |  A sufficiently advanced
-- Systems Engineer, Programmer|  regular expression is
-- http://www.shadow.net/~stimuli  |  indistinguishable from
-- stimuli AT shadow DOT net   |  magic




Re: The importance and relevance of FP

2000-08-21 Thread Jeffrey Straszhiem

On Mon, Aug 21, 2000 at 10:28:41AM +0200, Ketil Malde wrote:

 Or just a mail server witout buffer overruns would be great. *Sigh*

Just this sort of thing is so often sugested as a proving ground for
all sorts of "fringe" languages.  I know it is frequently suggested on
both the Eiffel and Ada lists.  Yet it never seems to get done.

I think Haskell would be perfect for this task.  Perhaps for the beta
level project just focus on SMTP and local mail delivery, and not on
all the strange mail systems that sendmail endevors to cover.

Let's hear some thoughts on this.

-- 
-- Jeffrey Straszheim  |  A sufficiently advanced
-- Systems Engineer, Programmer|  regular expression is
-- http://www.shadow.net/~stimuli  |  indistinguishable from
-- stimuli AT shadow DOT net   |  magic