testFormat.curry 2.18 KB
Newer Older
Michael Hanus 's avatar
Michael Hanus committed
1 2 3
{-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=foreigncode #-}

------------------------------------------------------------------------------
Michael Hanus 's avatar
Michael Hanus committed
4 5
--- This program contains some examples for integrated code to support
--- string formatting.
Michael Hanus 's avatar
Michael Hanus committed
6
---
Michael Hanus 's avatar
Michael Hanus committed
7 8
--- The format specification follows the C specification for `printf`
--- formatting whch may be found at
Michael Hanus 's avatar
Michael Hanus committed
9 10 11 12 13 14 15 16
--- <http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>
------------------------------------------------------------------------------

import Format -- required in the pre-processed program
import Test.EasyCheck

-- Format a string and an integer:
ex1 :: String -> Int -> String
Michael Hanus 's avatar
Michael Hanus committed
17
ex1 name age = ``format "Hello %s, you are %i years old.",name,age''
Michael Hanus 's avatar
Michael Hanus committed
18

Michael Hanus 's avatar
Michael Hanus committed
19
testEx1 = (ex1 "Mike" 42) -=- "Hello Mike, you are 42 years old."
Michael Hanus 's avatar
Michael Hanus committed
20

Michael Hanus 's avatar
Michael Hanus committed
21 22 23
-- Format two integers:
ex2 :: Int -> Int -> String
ex2 n1 n2 = ``format "%+.5d and % 10.4i",n1,n2''
Michael Hanus 's avatar
Michael Hanus committed
24

Michael Hanus 's avatar
Michael Hanus committed
25
testEx2 = (ex2 12345 34) -=- "+12345 and       0034"
Michael Hanus 's avatar
Michael Hanus committed
26

Michael Hanus 's avatar
Michael Hanus committed
27
testInt5            = ``format "%.5i",42''    -=- "00042"
Michael Hanus 's avatar
Michael Hanus committed
28

Michael Hanus 's avatar
Michael Hanus committed
29 30 31 32 33 34 35 36 37
testIntSigned5      = ``format "%+.5d",42''   -=- "+00042"

testIntFixedSigned5 = ``format "%+10.5i",42'' -=- "    +00042"

-- Format a charater:
ex3 :: Char -> String
ex3 c = ``format "This is a char: %c",c''

testEx3 = (ex3 'a') -=- "This is a char: a"
Michael Hanus 's avatar
Michael Hanus committed
38 39

-- Integer and character formatting:
Michael Hanus 's avatar
Michael Hanus committed
40
testIntFixedSignedChar =  ``format "%+5d%c",42,c''  -=-  "  +42%"
Michael Hanus 's avatar
Michael Hanus committed
41
  where
Kai-Oliver Prott's avatar
Kai-Oliver Prott committed
42
   c = '%'
Michael Hanus 's avatar
Michael Hanus committed
43 44

-- Format a string with a given width and maximal length:
Michael Hanus 's avatar
Michael Hanus committed
45 46 47 48 49
ex4 :: String -> String
ex4 s = ``format "This is a string: %010.4s!",s''

testEx4 = (ex4 "Hello") -=- "This is a string:       Hell!"

Michael Hanus 's avatar
Michael Hanus committed
50 51 52 53 54 55 56
-- Format with passing expressions:
ex8 :: Int -> Int -> String
ex8 n1 n2 = ``format "The sum of %+.5d and %+5i is %+6i.\n",n1,n2,n1+n2''

test_intexp = (ex8 42 2143) -=- "The sum of +00042 and +2143 is  +2185.\n"

-- Format a float with a given width and precision:
Michael Hanus 's avatar
Michael Hanus committed
57
testFloatFixPrec3 = ``format "%+8.3f",f'' -=- "  +3.142"
Michael Hanus 's avatar
Michael Hanus committed
58 59
 where
  f = 3.14159
Kai-Oliver Prott's avatar
Kai-Oliver Prott committed
60

Michael Hanus 's avatar
Michael Hanus committed
61
-- Format a float with an exponent:
Michael Hanus 's avatar
Michael Hanus committed
62
testFloatExp = ``format "% .4E",f'' -=- " 3.1416E+02"
Michael Hanus 's avatar
Michael Hanus committed
63
 where
Michael Hanus 's avatar
Michael Hanus committed
64 65 66 67 68 69 70
  f = 314.159

-- Format the sum of two integers:
ex7 :: Int -> Int -> String
ex7 n1 n2 = ``format "The sum of %+.5d and %+5i is %+6i",n1,n2,n1+n2''

testEx7 = (ex7 1234 890) -=- "The sum of +01234 and  +890 is  +2124"