1
votes

UPDATE:

To be fair, my example was a simplification of a problem I'm facing, I've tried implementing the solution, but it doesn't seem to work...

followConnection :: Connection->Crib->Stecker->Offsets->Maybe Stecker
followConnection w x y z
| steckerAdd ((enigmaEncode (getSndTripleEl w) (SimpleEnigma rotor1 rotor2 rotor3 reflectorB) (calculateOffset z (getFirstTripleEl w))), (getThirdTripleEl w)) y == Nothing = Nothing
| steckerAdd ((enigmaEncode (getSndTripleEl w) (SimpleEnigma rotor1 rotor2 rotor3 reflectorB) (calculateOffset z (getFirstTripleEl w))), (getThirdTripleEl w)) y == Just (y) = y

With

 steckerAdd ((enigmaEncode (getSndTripleEl w) (SimpleEnigma rotor1 rotor2 rotor3 reflectorB) (calculateOffset z (getFirstTripleEl w))), (getThirdTripleEl w)) y == Just (y) = y

Giving

Couldn't match type [(Char, Char)]' withMaybe Stecker' Expected type: Maybe Stecker Actual type: Stecker

I have a function (myFunction) which returns "Maybe Int" as an output

I would like to code something similar to:

myOtherFunction :: Int -> Maybe Int
myOtherFunction x
| myFunction x == Nothing = 1
| myFunction x == 1 = 2
| otherwise = 3

However, Haskell doesn't seem to like me comparing a "Maybe Int" value to an int...

I also tried "casting" it to an Int by making it:

| fromMaybe(myFunction) x == 1 = 2

Where the function is:

fromMaybe :: Maybe a -> a
fromMaybe (Just x)=x

Ideas?

4
Why not myFunction x == Just 1?user824425

4 Answers

5
votes

I'm going to assume that your myFunction has this type:

myFunction :: Int -> Maybe Int

I'm also going to assume that you wrote the type of myOtherFunction incorrectly, since it produces an Int result and not a Maybe Int:

myOtherFunction :: Int -> Int

The way you do this is pattern matching, which you write using a case statement:

myOtherFunction x = case (myFunction x) of
    Nothing -> 1
    Just 1  -> 2
    _       -> 3
1
votes

What you should use instead is pattern matching:

myOtherFunction :: Maybe Int -> Int
myOtherFunction Nothing = 1
myOtherFunction (Just 1) = 2
myOtherFunction _ = 3

The reason why yours doesn't work is because you can't compare an Int directly to a Maybe Int. This is true in any language, not just Haskell. Comparing two different types usually doesn't work.


If you really wanted to use guards, you could do

myOtherFunction :: Int -> Maybe Int
myOtherFunction x
    | myFunction x == Nothing = 1
    | myFunction x == Just 1 = 2
    | otherwise = 3
0
votes

You can do something like this:

myOtherFunction :: Int -> Maybe Int
myOtherFunction x
 | myFunction x == Nothing = 1
 | myFunction x == Just 1 = 2
 | otherwise = 3

Here's an Ideone of this kind of thing.

0
votes

The 4th line of your updated example ends with = y. But it is clear from followConnection's type signature that y is a Stecker, and not a Maybe Steker.

Try changing the end of that 4th line to = Just y. This is most likely what the error is referring to, since it expects a Maybe Stecker to be returned, but it is getting a Stecker.