Difference between revisions of "Cookbook/Interactivity"

From HaskellWiki
Jump to navigation Jump to search
Line 4: Line 4:
 
! Solution
 
! Solution
 
! Examples
 
! Examples
  +
|-
  +
| printing a string
  +
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStr putStr]
  +
|<haskell>
  +
Prelude> putStr "Foo"
  +
FooPrelude>
  +
</haskell>
  +
|-
  +
| printing a string in a new line
  +
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStrLn putStrLn]
  +
|<haskell>
  +
Prelude> putStrLn "Foo"
  +
Foo
  +
</haskell>
 
|-
 
|-
 
| reading a string
 
| reading a string

Revision as of 09:04, 2 August 2009

Problem Solution Examples
printing a string putStr
Prelude> putStr "Foo"
FooPrelude>
printing a string in a new line putStrLn
Prelude> putStrLn "Foo"
Foo
reading a string getLine
Prelude> getLine
Foo bar baz          --> "Foo bar baz"

Printing a string

Strings can be output in a number of different ways.

Prelude> putStr "Foo"
FooPrelude>

As you can see, putStr does not include the newline character `\n'. We can either use putStr like this:

Prelude> putStr "Foo\n"
Foo

Or use putStrLn, which is already in the Standard Prelude

Prelude> putStrLn "Foo"
Foo

We can also use print to print a string, including the quotation marks.

Prelude> print "Foo"
"Foo"

Parsing command line arguments

TODO