Once again I'm hitting roadblocks with this code... I posted my earlier code which was much different earlier, so now that its changing, so are the problems! so I have a function
convertToHTML :: String -> String
convertToHTML [] = [] --prevents calling head on empty line
convertToHTML x
| doubleHash x == True = "<h3>" ++ drop 2 x ++ "</h3>"
| head x == '#' = "<h1>" ++ tail x ++ "</h1>"
| x == "---" = "<hr/>"
| otherwise = x
Now, basically what is happening is, my helper function doubleHash x which is supposed to read a line, and if that line begins with ## slap a h3 tag on the whole line and remove the ##. So the first guard, I believe does just that. So, I'm thinking there is a problem with the doubleHash function. So here is the doubleHash helper function
doubleHash ('#' : '#' : []) = True
doubleHash _ = False
so using cons, just saying ## would return true. Not sure what is wrong here, but when convertToHTML gets called to run doubleHash x, it doesnt apply the notion of doubleHash == True, so slap a H3 tag on this line! instead it goes right to head x and applies the H1 tag to both lines... ex: text
--> #This should be a H1 tagged line
--> ##This should be a H3 tagged line
however both are getting slapped with H1 tags.
'#' : '#' : []
is equivalent to "##"
. In other words, you're checking if the entire string is equal to "##"
. You probably want to check if the string starts with "##"
. In Data.List
there's the convenient function isPrefixOf
which tests for just that.
> isPrefixOf "##" "## a string"
True
> isPrefixOf "##" "# another string"
False
Your doubleHash
will return True only if the whole string is "##".
This one will do whatever you need:
doubleHash ('#' : '#' : _) = True
doubleHash _ = False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.