Voglio chiamare la funzione Text.Printf printf con array ma non riesco a trovare un modo. Qui ci sono due versioni non funzionanti (in realtà la stessa idea).Argomenti haskell printf come array
import Text.Printf
printfa :: (PrintfArg a) => String -> [a] -> String
printfa format args = step (printf format) args
where
step :: (PrintfType r, PrintfArg a) => r -> [a] -> r
step res (x:[]) = res x
step res (x:xs) = step (res x) xs
printfa' :: (PrintfArg a) => String -> [a] -> String
printfa' format args = foldr (\arg p -> p arg) (printf format) args
main = putStrLn $ printfa "%s %s" ["Hello", "World"]
GHC errori sono:
printfa.hs:8:23:
Couldn't match type `r' with `a1 -> r'
`r' is a rigid type variable bound by
the type signature for
step :: (PrintfType r, PrintfArg a1) => r -> [a1] -> r
at printfa.hs:8:5
The function `res' is applied to one argument,
but its type `r' has none
In the expression: res x
In an equation for `step': step res (x : []) = res x
printfa.hs:12:41:
The function `p' is applied to one argument,
but its type `String' has none
In the expression: p arg
In the first argument of `foldr', namely `(\ arg p -> p arg)'
In the expression: foldr (\ arg p -> p arg) (printf format) args
(Perché:. Sto scrivendo DSL e desidera fornire funzione printf)
Solo per informazioni, c'è un abisso di differenza tra una lista collegata (che '[a]' è) e la matrice. –
Vuoi veramente forzare tutti gli argomenti su printf ad avere lo stesso tipo? – augustss
No, tutti gli argomenti sono istanze di PrintfArg e non hanno lo stesso tipo. –