Difference between revisions of "Cookbook/Interactivity"

From HaskellWiki
Jump to navigation Jump to search
m (Fixed broken links)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
== Input and output ==
  +
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 6: Line 8:
 
|-
 
|-
 
| printing a string
 
| printing a string
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStr putStr]
+
| [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3AputStr putStr]
 
|<haskell>
 
|<haskell>
 
Prelude> putStr "Foo"
 
Prelude> putStr "Foo"
Line 12: Line 14:
 
</haskell>
 
</haskell>
 
|-
 
|-
| printing a string in a new line
+
| printing a string with a newline character
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AputStrLn putStrLn]
+
| [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3AputStrLn putStrLn]
 
|<haskell>
 
|<haskell>
 
Prelude> putStrLn "Foo"
 
Prelude> putStrLn "Foo"
 
Foo
 
Foo
  +
Prelude>
 
</haskell>
 
</haskell>
 
|-
 
|-
 
| reading a string
 
| reading a string
| [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AgetLine getLine]
+
| [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3AgetLine getLine]
 
|<haskell>
 
|<haskell>
 
Prelude> getLine
 
Prelude> getLine
Line 27: Line 30:
 
|}
 
|}
   
  +
== Printing a string ==
 
Strings can be output in a number of different ways.
 
<haskell>
 
Prelude> putStr "Foo"
 
FooPrelude>
 
</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:
 
<haskell>
 
Prelude> putStr "Foo\n"
 
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"
 
Foo
 
</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.'''
 
<haskell>
 
Prelude> print "Foo"
 
"Foo"
 
</haskell>
 
   
 
== Parsing command line arguments ==
 
== Parsing command line arguments ==
   
  +
Command line argument processing is provided by System.Environment library.
TODO
 
  +
  +
The following small program (tac) concatenates and prints the contents of files in reverse (or reads from stdin with no arguments).
  +
  +
<pre>
  +
--
  +
import System.Environment
  +
import System.Exit
  +
  +
main = getArgs >>= parse >>= putStrLn . tac
  +
  +
tac = unlines . reverse . lines
  +
  +
parse ["-h"] = usage >> exit
  +
parse ["-v"] = version >> exit
  +
parse [] = getContents
  +
parse fs = concat `fmap` mapM readFile fs
  +
  +
usage = putStrLn "usage: tac [-vh] [file..]"
  +
version = putStrLn "Haskell tac 0.1"
  +
exit = exitWith ExitSuccess
  +
die = exitWith (ExitFailure 1)
  +
</pre>
  +
  +
Some example uses:
  +
  +
<pre>
  +
$ ./tac -h
  +
usage: tac [-vh] [file..]
  +
</pre>
  +
  +
== The environment ==
  +
  +
Many programs need access to computer environment variables. On POSIX systems, access to all variables is by
  +
  +
<pre>
  +
import System.Environment
  +
</pre>
  +
  +
NOTE: example uses to be added

Latest revision as of 21:07, 6 January 2019

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

Command line argument processing is provided by System.Environment library.

The following small program (tac) concatenates and prints the contents of files in reverse (or reads from stdin with no arguments).

-- 
import System.Environment
import System.Exit

main = getArgs >>= parse >>= putStrLn . tac

tac = unlines . reverse . lines

parse ["-h"] = usage >> exit
parse ["-v"] = version >> exit
parse []     = getContents
parse fs     = concat `fmap` mapM readFile fs

usage   = putStrLn "usage: tac [-vh] [file..]"
version = putStrLn "Haskell tac 0.1"
exit    = exitWith ExitSuccess
die     = exitWith (ExitFailure 1)

Some example uses:

$ ./tac -h
usage: tac [-vh] [file..]

The environment

Many programs need access to computer environment variables. On POSIX systems, access to all variables is by

import System.Environment

NOTE: example uses to be added