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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:12:58+00:00 2026-06-14T05:12:58+00:00

I’m trying to place a bunch of words into a hash table based on

  • 0

I’m trying to place a bunch of words into a hash table based on length. The words are stored in

data Entry = Entry {word :: String, length :: Int} deriving Show

Now, I’ve got all the words stored in “entries”, which is a list of Entry. Then, my hash table is defined as follows:

type Hash = [Run]
type Run = [Entry]

Now I’m trying to figure out how to get the entries into the hash table. The following is my current attempt

maxL = maximum [length e | e <- entries]
runs = [r | r <- [e | e <- entries, length e == i]] where i = [1..maxL]

Compiler’s obviously telling me that Int can’t be compared to [Int], but I don’t know how to say

e | e <- entries, e has length i

Any help is much appreciated!

Cheers

  • 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-14T05:12:59+00:00Added an answer on June 14, 2026 at 5:12 am

    Your code is almost OK:

    maxL = maximum [length e | e <- entries]
    runs = [r | r <- [e | e <- entries, length e == i]] where i = [1..maxL]
    

    except that where doesn’t work that way. It’s not a synonym for foreach; but for let:

    runs = let i = [1..maxL] 
           in [r | r <- [e | e <- entries, length e == i]] 
    

    So, length e is an integer, but i is [1..maxL] which is a list of integers. You intended for i to take on the values in [1..maxL] one-by-one, and that’s done by <- binding in list comprehension:

    runs = [ [r | r <- [e | e <- entries, length e == i]] | i <- [1..maxL]]
    

    Now, [r | r <- xs] is the same as just xs, so it becomes

    runs = [ [e | e <- entries, length e == i] | i <- [1..maxL]]
    

    With “standard” functions, this is written as

    import Data.List (sortBy)
    import Data.Ord  (comparing)
    
    runs = group $ sortBy (comparing length) entries
    

    It is also better algorithmically. Although, it won’t have empty runs for non-existent lengths, so the two aren’t strictly equivalent. But that can be fixed with another O(n) pass over the results, with

    -- mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
    
    runs' = snd $ mapAccumL 
                   (\a@ ~((k,g):t) i-> if null a || i<k then (a,[]) else (t,g))
                   [ (length $ head g, g) | g<- runs]
                   [ 1..maxL] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but

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.