Haskell error Not in Scope: data constructor 'Medicines'

Asked

Viewed 195 times

1

Good afternoon, how do I get out of that mistake? I’m trying to include an element in a list of tuples.

    type Nome             = String
    type Quantidade       = Int
    type HorarioProximo   = Int
    type HoraAtual        = Int
    type Horario          = [Int]
    type Medicamento      = (Nome,Quantidade)
    type Medicamentos     = [Medicamento]
    type Prescricao       = (Nome,Horario,HorarioProximo)
    type PlanoMedicamento = [Prescricao] 

    adicionarMedicamento :: Medicamento -> Medicamentos -> Medicamentos 
    adicionarMedicamento (a,b) [] = Medicamentos++[(a,b)]
    adicionarMedicamento (a,b) [(c,d):(e,f)] | (a == c)  = (a,b+d)
                                         | otherwise = (c:d):(adicionarMedicamento (a,b) [(e,f)])  

Someone please?

1 answer

1

In his job adicionarMedicamento :: Medicamento -> Medicamentos -> Medicamentos on the line adicionarMedicamento (a,b) [] = Medicamentos++[(a,b)] you refers to the second function parameter, initially, as [], however, following the addition of the medicinal product (a,b) received the list of Medicines, you refers to the second function parameter, as Medicamentos and this causes the error:

    Data constructor not in scope: Medicamentos :: [(Nome, Quantidade)]
   |
12 | adicionarMedicamento (a,b) [] = Medicamentos++[(a,b)]
   |                                 ^^^^^^^^^^^^

For the compiler knows the second function parameter as [], to fix use the same parameter name as initially set.

Reading your code, I understand that you want to add a medicine to the list of medicines if that medicine is not on the list. However, if the medicine is already on the list, you want to increase the amount of the medicine. If based on this, I propose the following code:

type Nome             = String
type Quantidade       = Int
type HorarioProximo   = Int
type HoraAtual        = Int
type Horario          = [Int]
type Medicamento      = (Nome,Quantidade)
type Medicamentos     = [Medicamento]
type Prescricao       = (Nome,Horario,HorarioProximo)
type PlanoMedicamento = [Prescricao] 

adicionarMedicamento :: Medicamento -> Medicamentos -> Medicamentos 
adicionarMedicamento (a,b) c |elem (fst (a,b)) [fst c | c <- c] == False = c++[(a,b)]
                             |otherwise = aumentarDose (a,b) c

aumentarDose :: Medicamento -> Medicamentos -> Medicamentos
aumentarDose m ms |fst (head ms) == fst m = [(fst m, snd m + snd (head ms))]++tail ms
                  |otherwise = [head ms]++(aumentarDose m (tail ms))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.