Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Maybe Int to Int in Haskell

Tags:

haskell

I am working on the following code and wanted to find the index of the number in the box string. So i used findIndex but it returns the Maybe Int value whereas i want only Int value.

How can i convert Maybe Int to Int value or is there any way in which i can extract Int from Maybe Int. The code should print an error message if Maybe Int is nothing

box:: String
box = unlines $ ["0 | 1 | 2",
                 "---------",
                 "3 | 4 | 5",
                 "---------",
                 "6 | 7 | 8"]

moves = do
        putStrLn " Enter the number"
        number <- readLn :: IO Int
        print number
        findpostion number box

findposition number box = findIndex (==number) box
like image 408
Rog Matthews Avatar asked Jan 18 '12 04:01

Rog Matthews


People also ask

How does maybe work in Haskell?

The Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a ), or it is empty (represented as Nothing ). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error .

What is the difference between int and Integer in Haskell?

What's the difference between Integer and Int ? Integer can represent arbitrarily large integers, up to using all of the storage on your machine. Int can only represent integers in a finite range.

What does fromIntegral do in Haskell?

The workhorse for converting from integral types is fromIntegral , which will convert from any Integral type into any Num eric type (which includes Int , Integer , Rational , and Double ): fromIntegral :: (Num b, Integral a) => a -> b.

How do you change int to float in Haskell?

In Haskell, we can convert Int to Float using the function fromIntegral .


2 Answers

You can easily do this using pattern matching in your do statement:

case findposition number box of
  Just n  -> -- do whatever with n
  Nothing -> putStrLn "Invalid number!" -- you can handle the error however you want.

A good option would be to create a separate IO action to get the number:

getNumber = do putStrLn "Enter the number:"
               number <- readLn
               case findposition number box of
                 Just n  -> -- Do whatever
                 Nothing -> putStrLn "Please try again." >> getNumber

This way if the user enters an invalid number, it just asks again.

Also, as written now, your code won't work. You should have some other way of storing the numbers in box as actual numbers; right now, they're in Strings.

like image 77
Tikhon Jelvis Avatar answered Sep 21 '22 18:09

Tikhon Jelvis


Obviously, it's not possible in general: when the search doesn't succeed there is no canonical integer return value, so you get a Nothing without any such value then.

If you don't really care about the Nothing case (e.g. because you will always make sure there is one such element) you can use the fromJust function out of Data.Maybe, which you can also quickly implement yourself:

findposition number = (\(Just i)->i) . findIndex (==number)

However that's not really recommendable because you will need to make sure this doesn't break, and doing this is much easier by means of a proper pattern matching.

like image 45
leftaroundabout Avatar answered Sep 22 '22 18:09

leftaroundabout