I can have many Figures on my list. Each Figure can have many Rectangles on its list. I have a problem with my function checkNewRectangleId - this function should ask user about new rectangle id until he write really new id and then it should return this id - but I have an error: couldn't match expected type IO t against inferred type Maybe figureType line (Figure id width height rectangles) <- findFigure idFigure x in my function - could you help ?
import IO
import Char
import System.Exit
import Maybe
import Data.Time.Calendar
import System.Time
checkNewRectangleId :: Int -> [FigureType] -> IO Int
checkNewRectangleId idFigure x = do
idRectangle <- getInt "Give me new rectangle id: "
(Figure id width height rectangles) <- findFigure idFigure x
if isJust (findRectangle idRectangle rectangles) then do
putStrLn ("We have yet rectangle with id " ++ show idRectangle)
checkNewRectangleId idFigure x
else return idRectangle
data FigureType = Figure Int Int Int [RectangleType] deriving(Show, Read)
data RectangleType = Rectangle Int CalendarTime deriving(Show, Read)
findFigure :: Int -> [FigureType] -> Maybe FigureType
findFigure _ [] = Nothing
findFigure n ((Figure id width height rectangles) : xs) =
if n == id then Just (Figure id width height rectangles)
else findFigure n xs
findRectangle :: Int -> [RectangleType] -> Maybe RectangleType
findRectangle _ [] = Nothing
findRectangle n ((Rectangle id date) : xs) =
if n == id then Just (Rectangle id date)
else findRectangle n xs
isInt i = not (null i) && all isDigit i
getInt :: String -> IO Int
getInt q = do
putStr q;
i <- getLine
if isInt i == False then do
putStrLn "Bad number"
getInt q
else return (read i)
Since you say idFigure is guaranteed to exist, you can use fromJust in the Data.Maybe module to convert a Maybe FigureType into a FigureType:
let (Figure id width height rectangles) = fromJust $ findFigure idFigure x
findFigure operates in the Maybe monad, but checkNewRectangleId operates in the IO monad. Haskell will not automatically translate failures (or successes) in one monad into failures (or successes) in another, because the types don't match. So, you have to ask yourself the question, what do you want to happen if findFigure fails to find anything?
Related
This is my code
module Main where
import Control.Monad (mapM)
import Text.Read (readMaybe)
import System.IO (BufferMode(..), stdout, hSetBuffering)
mouth = [('P',0),('(',1),('[',2),(')',3),('O',4)]
eyes = [(':',1),('8',2),(';',3)]
findKey :: (Eq k) => k -> [(k,v)] -> Maybe v
findKey key = foldr (\(k,v) acc -> if key == k then Just v else acc) Nothing
query :: Read a => String -> IO a
query prompt = do
putStr $ prompt ++ ": "
val <- readMaybe <$> getLine
case val of
Nothing -> do
putStrLn "Sorry that's a wrong value - please reenter"
query prompt
Just v -> return v
ngoers :: IO Int
ngoers = query "Enter the number of Concertgoers"
cgoers :: Int -> IO (Int, Double)
cgoers i = do
c <- query prompt
return (fromIntegral i,c)
where prompt = "Enter the emoticon for concertgoer " ++ show (i+1)
concertgoer :: IO [(Int, Double)]
concertgoer = do
n <- ngoers
mapM cgoers [0,1..n-1]
presentResult :: Double -> IO ()
presentResult v = putStrLn $ "The results are: " ++ show v
main :: IO ()
main = do
p <- concertgoer
presentResult $ 0
I want this output
Enter the number of Concertgoers: 4
Enter the emoticon for concertgoer 1: :(
Enter the emoticon for concertgoer 2: :)
Enter the emoticon for concertgoer 3: ;P
Enter the emoticon for concertgoer 4: ;o
The results are: 2 4 3 7
From your example I'm guessing that you match each eye and mouth to a number, and a emoticon is the sum if those... but you haven't explained nothing of this in your post. Assuming so, this is a very naive way to write It
import Control.Monad (mapM)
-- Define the data you want to use
data Eye = Normal
| Glasses
| Wink
deriving(Show, Eq)
data Mouth = P
| Sad
| Bracket
| Happy
| O
deriving(Show, Eq)
data Face = Face Eye Mouth deriving(Show, Eq)
-- Define special readers and elemToInt
readEyes :: Char -> Maybe Eye
readEyes c = case c of
':' -> Just Normal
'8' -> Just Glasses
';' -> Just Wink
_ -> Nothing
-- This is equivalent to derive Enum class and apply fromEnum. Try to do it your self ;)
eyeToInt :: Eye -> Int
eyeToInt Normal = 1
eyeToInt Glasses = 2
eyeToInt Wink = 3
readMouth :: Char -> Maybe Mouth
readMouth c = case c of
'P' -> Just P
'(' -> Just Sad
'[' -> Just Bracket
')' -> Just Happy
'O' -> Just O
_ -> Nothing
mouthToInt :: Mouth -> Int
mouthToInt P = 0
mouthToInt Sad = 1
mouthToInt Bracket = 2
mouthToInt Happy = 3
mouthToInt O = 4
readFace :: String -> Maybe Face
readFace [] = Nothing
readFace [e,m] = do
eye <- readEyes e
mouth <- readMouth m
return $ Face eye mouth
readFace _ = Nothing
faceToInt :: Face -> Int
faceToInt (Face e m) = eyeToInt e + mouthToInt m
-- The main loop is straight forward
main :: IO ()
main = do
putStrLn "Enter the number of Concertgoers"
number <- read <$> getLine -- Use safe reading better... I am using an online repl so no access to it
results <- mapM getEmoticon [1..number]
putStrLn $ "The results are: " ++ show results
where getEmoticon n = do
putStrLn $ "Enter the emoticon for concertgoer " ++ show n
face <- readFace <$> getLine
case face of
Nothing -> do
putStrLn "That's not an emotion!!"
getEmoticon n
Just f -> return $ faceToInt f
I think It is what you expect but let me know
Is there a way of finding a given "object" by one of it's property?
I tried pattern matching like i would have done in logic programming but I can't figure it out:
data Object = Object {
_prop1 :: type,
_prop2 :: color,
_prop3 :: pos
} deriving Eq
type Square = Maybe Object
type Board = [[Square]]
objectlist::Board
objectlist = [[ Just (Object type color pos), Just (Object type color pos)]
...
[ Just (Object type color pos), Just (Object type color pos)]
index_of :: (Int, Int)->Int
index_of (x,y) = fromJust $ elemIndex piece objectlist
where
piece = Piece _ _ (x,y)
Also, I think my approach to find the index is not good. I used it with a simple list but can't find how to do it with a 2 dim list.
As you stated as a comment in another answer you are looking for the index in a 2D list. Therefore I think the type of index_of should be (Int, Int) -> (Int, Int).
The function findIndex was suggested in the other answer to help you make index_of. What you need is a generic 2D version of it. Here is how you could implement findIndex2D:
import Data.List
import Data.Maybe
findIndex2D :: (a -> Bool) -> [[a]] -> Maybe (Int, Int)
findIndex2D pred xs = do
let maybeIndices = map (findIndex pred) xs
y <- findIndex isJust maybeIndices
x <- maybeIndices !! y
return (x, y)
You can use findIndex to get something of that effect.
index_of :: (Int, Int)->Int
index_of (x,y) = fromJust $ findIndex piece objectlist
where
piece (Piece _ _ pos) = pos == (x,y)
I'm new to Haskell and trying to write simple program to find maximal element and it's index from intput. I receive values to compare one by one. Maximal element I'm holding in maxi variable, it's index - in maxIdx. Here's my program:
loop = do
let maxi = 0
let maxIdx = 0
let idx = 0
let idxN = 0
replicateM 5 $ do
input_line <- getLine
let element = read input_line :: Int
if maxi < element
then do
let maxi = element
let maxIdx = idx
hPutStrLn stderr "INNER CHECK"
else
hPutStrLn stderr "OUTER CHECK"
let idx = idxN + 1
let idxN = idx
print maxIdx
loop
Even though I know elements coming are starting from bigger to smaller (5, 4, 3, 2, 1) program enters INNER CHECK all the time (it should happen only for the first element!) and maxIdx is always 0.
What am I doing wrong?
Thanks in advance.
Anyway, let's have fun.
loop = do
let maxi = 0
let maxIdx = 0
let idx = 0
let idxN = 0
replicateM 5 $ do
input_line <- getLine
let element = read input_line :: Int
if maxi < element
then do
let maxi = element
let maxIdx = idx
hPutStrLn stderr "INNER CHECK"
else
hPutStrLn stderr "OUTER CHECK"
let idx = idxN + 1
let idxN = idx
print maxIdx
loop
is not a particularly Haskelly code (and as you know is not particularly correct).
Let's make if Haskellier.
What do we do here? We've an infinite loop, which is reading a line 5 times, does something to it, and then calls itself again for no particular reason.
Let's split it:
import Control.Monad
readFiveLines :: IO [Int]
readFiveLines = replicateM 5 readLn
addIndex :: [Int] -> [(Int, Int)]
addIndex xs = zip xs [0..]
findMaxIndex :: [Int] -> Int
findMaxIndex xs = snd (maximum (addIndex xs))
loop :: ()
loop = loop
main :: IO ()
main = do xs <- readFiveLines
putStrLn (show (findMaxIndex xs))
snd returns the second element from a tuple; readLn is essentially read . getLine; zip takes two lists and returns a list of pairs; maximum finds a maximum value.
I left loop intact in its original beauty.
You can be even Haskellier if you remember that something (huge expression) can be replaced with something $ huge expression ($ simply applies its left operand to its right operand), and the functions can be combined with .: f (g x) is the same as (f . g) x, or f . g $ x (see? it's working for the left side as well!). Additionally, zip x y can be rewritten as x `zip` y
import Control.Monad
readFiveLines :: IO [Int]
readFiveLines = replicateM 5 readLn
addIndex :: [Int] -> [(Int, Int)]
addIndex = (`zip` [0..])
findMaxIndex :: [Int] -> Int
findMaxIndex = snd . maximum . addIndex
main :: IO ()
main = do xs <- readFiveLines
putStrLn . show . findMaxIndex $ xs
As for debug print, there's a package called Debug.Trace and a function traceShow which prints its first argument (formatted with show, hence the name) to stderr, and returns its second argument:
findMaxIndex :: [Int] -> Int
findMaxIndex = snd . (\xs -> traceShow xs (maximum xs)) . addIndex
That allows you to tap onto any expression and see what's coming in (and what are the values around — you can show tuples, lists, etc.)
I think alf's answer is very good, but for what it's worth, here's how I would interpret your intention.
{-# LANGUAGE FlexibleContexts #-}
module Main where
import System.IO
import Control.Monad.State
data S = S { maximum :: Int
, maximumIndex :: Int
, currentIndex :: Int }
update :: Int -> Int -> S -> S
update m mi (S _ _ ci) = S m mi ci
increment :: S -> S
increment (S m mi ci) = S m mi (ci+1)
next :: (MonadIO m, MonadState S m) => m ()
next = do
S maxi maxIdx currIdx <- get
input <- liftIO $ getLine
let element = read input :: Int
if maxi < element
then do
modify (update element currIdx)
liftIO $ hPutStrLn stderr "INNER CHECK"
else
liftIO $ hPutStrLn stderr "OUTER CHECK"
modify increment
run :: Int -> IO S
run n = execStateT (replicateM_ n next) (S 0 0 0)
main :: IO ()
main = do
S maxi maxIdx _ <- run 5
putStrLn $ "maxi: " ++ (show maxi) ++ " | maxIdx: " ++ (show maxIdx)
This uses a monad transformer to combine a stateful computation with IO. The get function retrieves the current state, and the modify function lets you change the state.
I'm trying to create a function for a silly IRC bot that will return a phrase where some of the letters are repeated a random number of times. The problem I'm having is that I can't find a way to use random numbers that ghc likes. It seems that even using this answer isn't being particularly helpful for getting my code to compile.
import System.Random
-- Write bad
baaad x y = "B" ++ (repeatA x) ++ "D " ++ (exclaim y)
-- StartHere
randomBad :: String
randomBad = do
x <- randomRIO(5,10) :: IO Int
y <- randomRIO(0,6) :: IO Int
return $ baaad x y
repeatA :: Int -> String
repeatA x = rptChr "A" x
exclaim :: Int -> String
exclaim x = rptChr "!" x
rptChr :: String -> Int -> String
rptChr x y = take y (cycle x)
Even with the trick of using a do block and passing the IO Ints to the function that way, I'm still getting compile errors that it found an IO Int when expecting Int.
randomBad is not in the IO monad.... It is type String, but you are defining it to be type IO String
Change this
randomBad :: String
to this
randomBad :: IO String
Then you should be able to use this in another IO action, like main:
main = do
theString <- randomBad
putStrLn theString
Could someone please help me with this? I'm kinda stuck and don't know why i get this error message :
not in scope type constructor or class 'Point'
--import Haste hiding (eval)
--import Haste.Graphics.Canvas
import Data.Maybe
import Expr
-- calculates all points of the graph in pixels
points :: Expr -> Double -> (Int,Int) -> [Point]
points exp sca (w,h) = [(x,realToPix(eval exp(pixToReal x))) | x<- [0..w]]
where
pixToReal :: Int -> Double
pixToReal x = sca*((fromIntegral x)-(fromIntegral w)/2)
realToPix :: Double -> Int
realToPix x = round ((x/sca) + ((fromIntegral w)/2))
-- calculates the lines that are going to be drawn between the points
linez :: Expr -> Double -> (Int,Int) -> [(Point,Point)]
linez exp sca (w,h) = zip (points exp sca (w,h)) (drop 1 (points exp sca (w,h)))
-- width and height of the window
sizeX, sizeY :: Int
sizeX = 300
sizeY = 300
--main :: IO ()
--main = do
--Just can <- getCanvasById "canvas"
--Just canElem <- elemById "canvas"
--Just func <- elemById "formula"
--Just d <- elemById "draw"
--onEvent d OnClick $ \_ (x,y) -> do
--f <- getProp func "value"
--w <- getProp canElem "width"
--h <- getProp canElem "height"
--render can (stroke (path (points (fromJust (readExpr f)) 0.04 (read w,read h))))
--return()
You have to import "Haste.Graphics.Canvas" which defines type alias for "Point".