edwinb / TypeDD-Samples

Sample code from "Type Driven Development with Idris"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Listing 6.8 `toFormat` '%' case

blouerat opened this issue · comments

Hi Edwin,

first of all, thank you for writing such a brilliant book, it's a pleasure to read!

Secondly, I have a question regarding the implementation of toFormat (listing 6.8). It currently goes like this (Chapter6/Printf.idr):

toFormat : (xs : List Char) -> Format
toFormat [] = End
toFormat ('%' :: 'd' :: chars) = Number (toFormat chars)
toFormat ('%' :: 's' :: chars) = Str (toFormat chars)
toFormat ('%' :: chars) = Lit "%" (toFormat chars)
toFormat (c :: chars) = case toFormat chars of
                             Lit lit chars' => Lit (strCons c lit) chars'
                             fmt => Lit (strCons c "") fmt

I don't understand the reason behind the toFormat ('%' :: chars) case. Shouldn't we simply get rid of it and let '%' fall into the final case where it will, if needed, nicely be appended inside a Lit?

For example, given the input "foo%bar", with the implementation in the book, you get:

> toFormat . unpack $ "foo%bar"
Lit "foo%" (Lit "bar" End) : Format

Whereas, when removing the toFormat ('%' :: chars) case, you get:

> toFormat . unpack $ "foo%bar"
Lit "foo%bar" End : Format

I think the latter makes more sense but maybe I am missing something. Happy to open a PR if needed.

Cheers!
Bastien