I'm trying to learn the idiomatic Haskell way to shuffle a list and take the first value off the head. I'm not even sure where in my program I should be doing this. So far, I've moved the logic to here:
-- {-# LANGUAGE UnicodeSyntax #-}
module RPS
( toss
) where
import System.Random.Shuffle
import Data.List
toss :: IO ()
toss =
-- This doesn't actually work
-- but I'd like to shuffle the list returned by
-- getTosses and pull the first element out
putStrLn "Biff tossed" ++ head shuffleM getTosses
getScissors :: [String]
getScissors =
scissors where scissors = replicate 10 "✂️"
getPaper :: [String]
getPaper =
paper where paper = replicate 10 "????"
getRocks :: [String]
getRocks =
rocks where rocks = replicate 10 "????"
getPossibilities :: [String] -> [String] -> [String] -> [String]
getPossibilities rocks paper scissors =
rocks ++ paper ++ scissors
getTosses :: [String]
getTosses =
getPossibilities getRocks getPaper getScissors
While running this module in GHCi, I get
src/RPS.hs:11:36: error:
• Couldn't match expected type ‘[[String] -> [a1]]’
with actual type ‘[a0] -> m0 [a0]’
• Probable cause: ‘shuffleM’ is applied to too few arguments
In the first argument of ‘head’, namely ‘shuffleM’
In the second argument of ‘(++)’, namely ‘head shuffleM getTosses’
In the expression:
The odd thing is, I can run shuffleM getTosses in GHCi just fine... it's only when I try to actually do that in a source file and then load that, that this fails. I've read that the shuffle monad only works in the context of IO, so I can't just shuffle a list in one of my other functions. I'm not sure if there's a better way.
putStrLntake only one argument. If you use++, makes sure to warp the argument. So, write it asputStrLn (...)orputStrLn $ .... Second, just likeputStrLn,headalso take only one argument. - wisnhead :: [a] -> a. The String is a List of Char. If you take its head, it will be a Char. - wisnputStrLn ("Biff tossed" ++ head (shuffleM getTosses))does give me a type error like you mention. What is the proper way to print a Char? Do I need to useshow? - Jim Wharton