Reputation: 865
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!
Upvotes: 0
Views: 191
Reputation: 11218
Few points to note :
Here is the code using random-fu library
import Data.Random
import Control.Applicative
food :: [String]
food = ["meatballs and potoes","veggisoup","lasagna","pasta bolognese","steak and fries","salad","roasted chicken"]
randomFood :: [String] -> RVar (Maybe String)
randomFood [] = return Nothing
randomFood xs = Just <$> randomElement xs
main :: IO ()
main = (sample $ randomFood food) >>= print
This is like choosing one element from a list randomly.
> main
Just "steak and fries"
> main
Just "meatballs and potoes"
If you want to output just a random permutation of the above list, you can use shuffle
like
main = (sample $ shuffle food) >>= print
Example
> main
["meatballs and potoes","lasagna","steak and fries","roasted chicken","salad","pasta bolognese","veggisoup"]
> main
["roasted chicken","veggisoup","pasta bolognese","lasagna","steak and fries","meatballs and potoes","salad"]
Upvotes: 2
Reputation: 32465
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
.
Upvotes: 5