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 7432593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:28:56+00:00 2026-05-29T09:28:56+00:00

I am trying to find the shortest and longest word in a given string.

  • 0

I am trying to find the shortest and longest word in a given string. If the entered string is “house tap mobile telephone” then the longest word should be telephone and shortest will be tap. I have already written the following functions. How do i find the index of the strings and find the shortest and longest word out of it?

---------Converting string into a list of strings and finding length of each word--------

stringConvert :: String -> [Int]
stringConvert x = map (length) (words x)

----------Find the longest word-----------

findLongestWord :: String -> Int
findLongestWord x = maximum(stringConvert x)

----------Find the shortest word-----------

findShortestWord :: String -> Int
findShortestWord x = minimum(stringConvert x)
  • 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-05-29T09:28:57+00:00Added an answer on May 29, 2026 at 9:28 am

    Since you are looking for a certain word, the signatures of your functions should be

    findLongestWord :: String -> String
    findShortestWord :: String -> String
    

    The stringConvert function you have implemented has the right idea, but it is a bit problematic because the result doesn’t have the information which word is associated with which length. It might be better to separate splitting the string into a word list from the length calculation, and there is a handy function called comparing that actually removes the need for the stringConvert function altogether.

    import Data.List (maximumBy)
    import Data.Ord (comparing)
    
    findLongestWord :: String -> String
    findLongestWord s = maximumBy (comparing length) (words s)
    

    maximumBy is similar to maximum but it takes as the first parameter a function that should be used to compare two items. comparing is a higher-order function that can be used to convert a function of type a -> b (where bis some type that can be compared, i.e. Ord b => a -> b if you are already familiar with type constraints) into a comparison function between two a‘s (i.e. a function of type a -> a -> Ordering).

    The shortest word can be found in a similar manner.

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

Sidebar

Related Questions

I'm still relatively new to regex. I'm trying to find the shortest string of
Trying to find a standard. The CmdLet will process data - multiple input, defined
Trying to find out if a provided Type is of a given generic type
I'm trying to write a piece of code that find shortest path in an
I am trying find lat & long from user given address, for that i
Background: I am trying to write a shortest possible lambda expression to find the
I'm trying to find out an efficient way of finding the shortest path between
I trying find a simple python-based algorithmic ranking system. Here's the scenario: There will
I am learning regular expressions and I am trying find this string day1otlk_XXXX.gif where
I am trying to find the all-pairs shortest paths matrix for a 5x5 matrix,

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.