What is the best way to map all items in a list except the last item in a list?
Let's say we have a list let l = [1,2,3,4] and we want to get [2,3,4,4] .
I have a solution, but it doesn't seem like a “functional” way to do this (in ghci):
let l = [1,2,3,4] let len = toIntegral $ length l -- to avoid a type mismatch Integer <-> Int let l1 = zip l [1..] let l2 = map (\(x,y) -> if y < len then (x + 1,y) else (x,y)) l1 let l3 = map (fst) l2
Not very nice ... I hope there is a better way! Since I'm new to functional programming, I don't know where to start looking for it.
mort source share