Quantcast
Channel: How to deal with a list of 'Maybe [values]' in Haskell? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

How to deal with a list of 'Maybe [values]' in Haskell?

$
0
0

I tryied to adapt some exercises of the excelent book "Land of Lisp" of Conrad Barski in order to learn Haskell. The idea is to make a simple text game engine.

Concretely I tried :

type Clau = Stringtype Descripcio = Stringtype Valors = [String]-- NOTE : Ideas of types http://learnyouahaskell.com/making-our-own-types-and-typeclassesdata Lloc = Lloc String String String deriving (Show) llocSituacio :: Lloc -> String  llocSituacio (Lloc situacio _ _ ) = situacio  llocDireccio :: Lloc -> StringllocDireccio (Lloc _ direccio _) = direcciollocPas :: Lloc -> StringllocPas ( Lloc _ _ pas) = pasnodes :: [(Clau,Descripcio)]nodes = [("living-room","you are in the living-room. a wizard is snoring loudly on the couch.")           ,("garden","you are in a beautiful garden. there is a well in front of you.")           , ("attic", "you are in the attic. there is a giant welding torch in the corner.")]edges :: [([Char], [Lloc])]edges = [ ("living-room", [(Lloc "garden"  "west" "door"), ( Lloc "attic" "upstairs" "ladder") ])        , ("attic", [(Lloc "living-room"  "east"  "door")])        , ("garden", [(Lloc "living-room" "east"  "door")])]describePath :: Lloc -> String  describePath  e = "There is " ++ llocPas e ++" going " ++ llocDireccio e ++" from here." 

At first, it seems that works well. For example:

*TextGame> describePath (Lloc "living-room"  "east"  "door")"There is door going east from here."

But when I try to apply the function to the list I got this error:

situacio = "garden"map (describePath) (lookup situacio edges)<interactive>:2:22: error:• Couldn't match expected **type ‘[Maybe Lloc]’**                  with actual **type ‘Maybe [Lloc]’**• In the second argument of ‘map’, namely ‘(lookup situacio edges)’      In the expression: map (describePath) (lookup situacio edges)      In an equation for ‘it’:          it = map (describePath) (lookup situacio edges)

The error is clear, but I do not manage to solve it. I want to parse the list of Maybe's values and print the path with the function describePath that works well:

Any ideas? Also, if somebody wants to share alternatives or feel that this code could be more in Haskell style, please feel free to talk about it.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images