Difference between revisions of "Cookbook/Interactivity"

From HaskellWiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Reading a string =
+
== Input and output ==
Strings can be read as input using [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AgetLine getLine].
 
<haskell>
 
Prelude> getLine
 
Foo bar baz
 
"Foo bar baz"
 
</haskell>
 
   
  +
{| class="wikitable"
= Printing a string =
 
  +
|-
Strings can be output in a number of different ways.
 
  +
! Problem
<haskell>
 
  +
! Solution
  +
! Examples
  +
|-
  +
| printing a string
  +
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStr putStr]
 
|<haskell>
 
Prelude> putStr "Foo"
 
Prelude> putStr "Foo"
 
FooPrelude>
 
FooPrelude>
 
</haskell>
 
</haskell>
  +
|-
As you can see, [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStr putStr] does not include the newline character `\n'. We can either use putStr like this:
 
  +
| printing a string with a newline character
<haskell>
 
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStrLn putStrLn]
Prelude> putStr "Foo\n"
 
 
|<haskell>
Foo
 
</haskell>
 
Or use [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStrLn putStrLn], which is already in the Standard Prelude
 
<haskell>
 
 
Prelude> putStrLn "Foo"
 
Prelude> putStrLn "Foo"
 
Foo
 
Foo
  +
Prelude>
 
</haskell>
 
</haskell>
  +
|-
We can also use [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aprint print] to print a string, '''including the quotation marks.'''
 
 
| reading a string
<haskell>
 
 
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AgetLine getLine]
Prelude> print "Foo"
 
 
|<haskell>
"Foo"
 
 
Prelude> getLine
  +
Foo bar baz --> "Foo bar baz"
 
</haskell>
 
</haskell>
  +
|}
  +
  +
   
= Parsing command line arguments =
+
== Parsing command line arguments ==
   
 
TODO
 
TODO

Revision as of 09:08, 2 August 2009

Input and output

Problem Solution Examples
printing a string putStr
Prelude> putStr "Foo"
FooPrelude>
printing a string with a newline character putStrLn
Prelude> putStrLn "Foo"
Foo
Prelude>
reading a string getLine
Prelude> getLine
Foo bar baz          --> "Foo bar baz"


Parsing command line arguments

TODO