Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:29:52+00:00 2026-06-07T03:29:52+00:00

I have defined 2 types in Haskell – Trip and Tour . Trip consists

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:29:53+00:00Added an answer on June 7, 2026 at 3:29 am

    My Haskell is very rough these days, but what you’re looking for is:

    • the shortest prefix ending in from — you can get this using
      takeWhile;
    • the longest suffix starting with to, but containing no
      other to — you can get this using tails with a filter.

    Then you can join the prefix, your shortcut, and the (tail of the) suffix, to get what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the defined inductive types: Inductive InL (A:Type) (y:A) : list A ->
If I have defined the following types: type category = Noun | Verb |
I have 3 types, (TCPClient, string, class) Note that the class is defined at
I have defined a custom list template type for SharePoint . I install it
I have defined some types: type box = Box of int type table =
I have defined two module types and two modules module type FOO = sig
I have defined some complex data types, for instance: type drawer = { ...
In my application I have a class which has properties of user-defined types like
I have defined a table type PL/SQL variable and added some data there. create
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.