Difference between revisions of "Cum scriu bucla while din functia principala, main ?"

From HaskellWiki
Jump to navigation Jump to search
 
m
Line 33: Line 33:
 
 
   
  +
{--
{--Reading file "input.hs":
 
 
runhugs myinput.hs
 
runhugs myinput.hs
 
--}
 
--}

Revision as of 22:54, 5 January 2008

Atunci cand programul principaL PRELUCREAZA UN FLUX DE DATE iar functia principala s-ar fi scris ca o bucla while intr-un limbaj imperativ aveti nevoie de acest while. Este scris in do-notatie si se foloseste de obicei impreuna cu operatii de IO.

while test actiune
  = do rezultat <- test 
       if rezultat then do actiune
                           while test actiune
                   else return ()

Exemplu:

 
module Main where
import IO
-- pentru isEOF

-- isEOF :: IO Bool

while test actiune
  = do rezultat <- test 
       if rezultat then do actiune
                           while test actiune
                   else return ()


main= while  (do b <- isEOF
                 return (not b) ) 
             (do s <- getChar
                 print $ ord s  )  
             

{--
runhugs  myinput.hs
--}