Haskell Maybe type -> type -
i running problem :
couldn't match expected type ‘int’ actual type ‘maybe int’ can somehow convert ‘maybe int’ ‘int’??
if index == nothing let index = 0 putstrln(fancyprint2 $ kaasasolevlist !! index) else let index = index putstrln(fancyprint2 $ kaasasolevlist !! index) i tried this, gives me :
exception: <<loop>>
you have several different options around this
first using if statement, small modification (avoid doing though)
if index == nothing let index' = 0 putstrln $ fancyprint2 $ kaasasolevlist !! index' else let (just index') = index putstrln $ fancyprint2 $ kaasasolevlist !! index' i'm writing index' here, because haskell doesn't allow overwrite existing variables, let hide them. it's better practice label "modified" versions of variable "prime" symbol (') @ end. way can access original if need be.
second can use case expression turns code into
case index of -> putstrln $ fancyprint2 $ kaasasolevlist !! nothing -> putstrln $ fancyprint2 $ kaasasolevlist !! 0 or if clean bit using where clause:
case index of -> putindex nothing -> putindex 0 putindex x = putstrln $ fancyprint2 $ kaasasolevlist !! x lastly there's frommaybe lets this:
import data.maybe (frommaybe) -- ... let index' = frommaybe 0 index putstrln $ fancyprint2 $ kaasasolevlist !! index' you can use guards. seeing don't know code-snippet comes from, don't know if it's reasonable use guards.
you can read more guards, case expressions , pattern matching here
Comments
Post a Comment