tis 2002-05-14 klockan 06.37 skrev anatoli:
> Brian Huffman <[EMAIL PROTECTED]> wrote:
> > Here is a printf-style function that I hacked up this morning; it uses type

> > classes but it doesn't need functional dependencies:
> [snip]
>
> It's very nice and even extendable, though `class Printf String'
> is unfortunately not Haskell 98. But the bigger question is, how
> to support Posix-style positional arguments? They are essential for
> i18n.
>
> For instance,
>
> > printf "%1$s %2$s" "foo" "bar" -- ==> "foo bar"
> > printf "%2$s %1$s" "foo" "bar" -- ==> "bar foo"
>
> Naturally, such format strings cannot be pre-processed by the
> compiler since they are typically loaded from some message database
> at run time.

I agree that i18n needs positional arguments.
What's wrong with simply doing like this:

printf "I have %. %. %.."    ["trained", show 1, "Jedi"]
printf "%2. %3. %1. I have." ["trained", show 1, "Jedi"]

with printf would look something like this:

printf ('%':'%':rest) xs     = '%' : printf rest xs
printf ('%':'.':rest) (x:xs) = x ++ printf rest xs
printf ('%':d:rest)   xs     | isDigit d =
  let (ds, rest') = span isDigit rest
      index = read (d:ds)
  in if null rest' || head rest' /= '.' || index > length xs then
       '%':printf (d:ds:rest') xs
     else
       xs!!(index - 1) ++ printf (tail rest') xs
printf (r:rest) xs = r:printf rest xs 
printf [] _ = []

Note that there are no errors if the format string is wrong in any way, it's
just unchanged. Also, behaviour with both positional and normal
formatters is not considered.

Feel free to use this code snippet however you like.

Regards,

        Martin

--
Martin Norbäck          [EMAIL PROTECTED]
Kapplandsgatan 40       +46 (0)708 26 33 60
S-414 78  GÖTEBORG      http://www.dtek.chalmers.se/~d95mback/
SWEDEN                  OpenPGP ID: 3FA8580B

Attachment: signature.asc
Description: PGP signature

Reply via email to