I'm doing some exercises and I'm stuck on 3.4. They ask me to define a function that, given a list, returns the inverse list. I CANNOT USE THE REVERSE FUNCTION . To complete this task I have to use the functions that I am asked for in 3.1 and 3.3 whose code I am going to attach now.
import Data.List
--3.1
posinv::[t] -> [(Integer,t)]
posinv s
|null s = []
|otherwise = zip (reverse [1..(genericLength s)]) s
--Ejemplo de lo que hace: posinv [19,56,15] = [(3,19),(2,56),(1,15)]
--3.2
lista_indice::Integer -> [(Integer,t)]->[(Integer,t)]
lista_indice i s = [(x,t) | (x,t) <- s, x == i]
--Ejemplo de lo que hace: lista_indice 2 [(3,19),(2,56),(1,15)] = [(2,56)]
--3.3
lista_indices:: Integer -> [(Integer,t)] -> [[(Integer,t)]]
lista_indices n s = [lista_indice x s | x <- [1..n] ]
--Ejemplo de lo que hace: lista_indices 3 [(3,19),(2,56),(1,15)] = [[(1,15)],[(2,56)],[(3,19)]]
--3.4
inversa::[t]-> [t]
inversa s = concat [y | y <- (x,y) <- (lista_indices (genericLength s) (posinv s)) ]
--Lo que debería de dar: inversa [19,56,15] = [15,56,19]
Well, since they ask me yes or yes to use functions 3.1 and 3.3 to complete 3.4, (lista_indices (genericLength s) (posinv s))
with this here I get what function 3.3 does, that is, [[(1,15)],[(2,56) ],[(3,19)]]. If you look closely, it's almost a result, only that it should concatenate [[15],[56][19]]
, only I don't know how to do this.
It's simple with pattern mapping :