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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:29:56+00:00 2026-06-11T16:29:56+00:00

I’m trying to grab a random item from a string list and save that

  • 0

I’m trying to grab a random item from a string list and save that into another string list but I can’t get my code to work.

    import System.Random
    import Control.Applicative ( (<$>) )

    food = ["meatballs and potoes","veggisoup","lasagna","pasta bolognese","steak and fries","salad","roasted chicken"]
    
    
    randomFood xs = do
              if (length xs - 1 ) > 0 then 
                 [list] <- (fmap (xs!!) $ randomRIO (0, length xs -1))
                 else
                 putStrLn (show([list])

I’m getting parse error on input ‘<-‘ but I’m sure there are more issues then that. There is also the issue that the list may contain the same dishes two days in a row which is not what I want and I guess I can remove duplicates but that also would remove the number of items in the list which I want to stay the same as the number in the list.

Anyone have a good idea how I could solve this? I have been searching for a day now and I can’t find something useful for me but that’s just because I’m looking in the wrong places. Any suggestion on how I can do this or where I can find the info will be greatly 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-11T16:29:57+00:00Added an answer on June 11, 2026 at 4:29 pm

    The reason it didn’t work is that you needed another do after your if...then. (After a then you need an expression, not a pattern <- expression.)

    randomFood :: String -> IO ()  -- type signature: take a String and do some IO.
    randomFood xs = do
              if length xs > 1 then do
                 [list] <- (fmap (xs!!) $ randomRIO (0, length xs -1))
                 else
                 putStrLn (show([list])
    

    But that still doesn’t compile, because you don’t actually do anything with your list.
    At the end of every do block, you need an expression to return.
    I think you meant to still print some stuff if the length of xs is too short, and you probably meant to print the selected food if there was more than one to choose from.

    Better would be:

    randomFood :: String -> IO () 
    randomFood xs | length xs <= 1 = putStrLn $ show xs
    randomFood xs | otherwise = do
                 item <- (xs!!) <$> randomRIO (0, length xs -1)
                 putStrLn $ show(item)
    

    This | boolean test = syntax is better for conditional answers based on input.

    I changed [list] to item because you’re selecting a single item randomly, not a list of items.
    Haskell is quite happy to let you put [list], because any string that’s got one character in it matches [list].
    For example, "h" = [list] if list='h', because “h” is short for ['h']. Any longer string will give you Pattern match failure. In particular, all the food you’ve specified has more than one character, so with this definition randomFood would never work! item will match anything returned by your randomRIO expression, so that’s fine.

    You imported <$> then didn’t use it, but it’s a nice operator, so I’ve replaced fmap f iothing with f <$> iothing.

    I finally realised I’m doing the wrong thing with short lists; if I do randomFood ["lump of cheese"] I’ll get ["lump of cheese"], which is inconsistent with randomFood ["lump of cheese"] which will give me "lump of cheese".
    I think we should separate the short list from the empty list, which enables us to do more pattern matching and less boolean stuff:

    randomFood :: String -> IO () 
    randomFood []        = putStrLn "--No food listed, sorry.--"
    randomFood [oneitem] = putStrLn . show $ oneitem 
    randomFood xs = do
             item <- (xs!!) <$> randomRIO (0, length xs -1)
             putStrLn . show $ item
    

    This gives three different definitions for randomFood depending on what the input looks like.

    Here I’ve also replaced putStrLn (show (item)) with putStrLn . show $ item – compose the functions show and putStrLn and apply ($) that to the item.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

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.