bender86
05-11-2010, 15:07
Ho un problema con un programma in Haskell. Devo implementare una funzione che, data una lista di coppie [ ( String, [String] ) ] mi generi una funzione IO (String -> String) che ritorna un elemento scelto a caso dalla lista in ingresso.
In breve: da [("prima", ["A", "B"]), ("seconda", ["C"])] potrebbe generare due funzioni
f1 "prima" = "A"
f1 "seconda" = "C"
e
f2 "prima" = "B"
f2 "seconda" = "C"
Questa è la definizione della funzione:stateOfMind :: BotBrain -> IO (Phrase -> Phrase)
stateOfMind brain =
do
r <- randomIO :: IO Float
return (rulesApply (zip ps (pick r pps)))
where
(ps, pps) = unzip brain
Il modulo viene caricato e compilato correttamente, quindi mi genera la funzione che mi serve (corretta o meno, non è questo il punto).
Questo è quello che risponde l'interprete:> :type stateOfMind
stateOfMind :: BotBrain -> IO (Phrase -> Phrase)
> let mind = stateOfMind brain
> :type mind
mind :: IO (Phrase -> Phrase)
Il mio problema è che non riesco a capire come chiamare la funzione mind.> :type phrase
phrase :: Phrase
> :type phrase2
phrase :: IO Phrase
> mind phrase
<interactive>:1:0:
Couldn't match expected type `IO Phrase -> t'
against inferred type `IO (Phrase -> Phrase)'
In the expression: mind phrase
In the definition of `it': it = mind phrase
> mind phrase2
<interactive>:1:0:
Couldn't match expected type `Phrase -> t'
against inferred type `IO (Phrase -> Phrase)'
In the expression: mind phrase2
In the definition of `it': it = mind phrase2
In breve: da [("prima", ["A", "B"]), ("seconda", ["C"])] potrebbe generare due funzioni
f1 "prima" = "A"
f1 "seconda" = "C"
e
f2 "prima" = "B"
f2 "seconda" = "C"
Questa è la definizione della funzione:stateOfMind :: BotBrain -> IO (Phrase -> Phrase)
stateOfMind brain =
do
r <- randomIO :: IO Float
return (rulesApply (zip ps (pick r pps)))
where
(ps, pps) = unzip brain
Il modulo viene caricato e compilato correttamente, quindi mi genera la funzione che mi serve (corretta o meno, non è questo il punto).
Questo è quello che risponde l'interprete:> :type stateOfMind
stateOfMind :: BotBrain -> IO (Phrase -> Phrase)
> let mind = stateOfMind brain
> :type mind
mind :: IO (Phrase -> Phrase)
Il mio problema è che non riesco a capire come chiamare la funzione mind.> :type phrase
phrase :: Phrase
> :type phrase2
phrase :: IO Phrase
> mind phrase
<interactive>:1:0:
Couldn't match expected type `IO Phrase -> t'
against inferred type `IO (Phrase -> Phrase)'
In the expression: mind phrase
In the definition of `it': it = mind phrase
> mind phrase2
<interactive>:1:0:
Couldn't match expected type `Phrase -> t'
against inferred type `IO (Phrase -> Phrase)'
In the expression: mind phrase2
In the definition of `it': it = mind phrase2