Here is a simplified version of my Code:
data Bookmark = Bookmark {
url :: String
, label :: String
} deriving (Show)
genBookmark :: String -> String -> IO Bookmark
genBookmark u l = return ( Bookmark { url = u, label = l } )
But the Strings (url, label) are in the data base, so I have to deal with IO String. There must me a very easy solution, but I don’t see it (and an extensive web search didn’t get me anywhere.)
Basically I want to change my code to:
genBookmark :: IO String -> IO String -> IO Bookmark
Here is another version illustrating the problem:
genBookmark2 :: String -> String -> Bookmark
genBookmark2 u = return ( Bookmark { url = u, label = newlabel } )
where newlabel = getLine
With the error “Expected type: String, Actual type: IO String”.
====== EDIT =======
Here is the “real code” that had the error (with the solution that I got from the answer):
getSkosConceptRight :: String -> IO SkosConcept
getSkosConceptRight catName = do
suConcepts <- getSubConcepts catName
concept <- getMainConcept catName
return ( concept { subConcepts = suConcepts })
getSkosConceptWrong :: String -> IO SkosConcept
getSkosConceptWrong catName = return ( concept { subConcepts = suConcepts })
where suConcepts = getSubConcepts catName
concept = getMainConcept catName
getMainConcept :: ShortUrl -> IO SkosConcept
getSubConcepts :: ShortUrl -> IO [SkosConcept]
You’ve left out the part of your code that gets the string from the db. Either way, you need to “unpack” the
Stringfrom yourIO String.In general, you should get rid of the
IOas early as possible. So you should really try for a type ofString -> String -> BookmarkHere’s a simple example:
Now, from
mainwhich is inIO, we make our “db” calls to get our strings, unpack the values, and send to ourgenBookmarkfunction.The reason why your
genBookmark2is failing, is because you are trying to usegetLine, whose type isIO Stringin a pure function; hence theexpected String, got IO Stringerror message. Also, the type mentions twoStrings as inputs, but you only take one.