I have defined 2 types in Haskell – Trip and Tour. Trip consists of direction, distance and price. The Tour is a list of Trips.
type Trip = (String, Integer, Float)
type Tour = [Trip]
trip1 :: Trip
trip1 = ("NY", 50, 100)
trip2 :: Trip
trip2 = ("Paris", 150, 100)
trip3 :: Trip
trip3 = ("London", 60, 100)
...
...
tripN :: Trip
tripN = ("Rome", 90, 100)
tour :: Tour
tour = [trip1, trip2, trip3,..., tripN]
The trips may have duplicates. What I want to accomplish here is to have function that takes one Tour, from destination, to destination and a Trip and the function replaces all the Trip sequences between the from and to destinations with the Trip, starting right after the from, and get the changed Tour.
Here is an example with the tour defined above:
shortenTour :: Tour -> String -> String -> Trip -> Tour
shortenTour tour "NY" "London" ("NY-London", 260, 200)
so the beginning of the new Tour should be something like this:
[("NY", 50, 100), ("NY-London", 260, 200), ... , ("Rome", 90, 100)]
If this sequence “NY” – .. – “London” is met anywhere else in the Tour it should be changed with the new Trip.
If there are two or more trips with the same from destination (ex. “NY”) before meeting the to destination the first from should be taken. Ex: (the distance and price don’t get in use so I am replacing them with _ )
shortenTour [("DC", _, _), ("NY", _, _), ("NY", _, _), ("Sofia", _, _), ("London", _, _)] "NY" "London" ("NY-London", 260, 200)
should return
[("DC", _, _), ("NY", _, _), ("NY-London", 260, 200)]
All the solutions that came up to me are long and bit ugly but I believe there is a smart and short (one or two rows of code) way to accomplish this by using list comprehensions but still cannot figure it out. So any help will be highly appreciated.
My Haskell is very rough these days, but what you’re looking for is:
from— you can get this usingtakeWhile;to, but containing noother
to— you can get this usingtailswith a filter.Then you can join the prefix, your shortcut, and the (tail of the) suffix, to get what you want.