Ich möchte Text.Printf Funktion printf mit Array aufrufen, aber ich kann keinen Weg finden. Hier sind zwei nicht funktionierende Versionen (eigentlich gleiche Idee).Haskell printf Argumente als 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 Fehler sind:
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
(Warum. Ich DSL Schreiben und printf-Funktion zur Verfügung stellen möchten)
Gerade für Informationen gibt es einen Abgrund der Differenz zwischen einer verknüpften Liste (die '[a]' ist) und Array. –
Möchten Sie wirklich alle Argumente auf printf zwingen, um den gleichen Typ zu haben? – augustss
Nein, alle Argumente sind Instanzen von PrintfArg und haben nicht denselben Typ. –