I am trying to add a string to the end of a string in Haskell.
albumStr = ""
main = do
let albumStr = albumStr ++ "nothing"
print albumStr
Whenever i run this it just gets stuck on " in the console and i have to terminate it.
Why? and how do i add a string to another string in this way?
Edit: How do i add multiple strings to the end of a current string without overwritting it.
Thanks
Unlike ML, Haskell does not have a rec
keyword to mark recursive definitions. Instead all definitions can be recursive, meaning that in every variable definition, the defined variable is already in scope during its definition.
So in let albumStr = albumStr ++ "nothing"
, the albumStr
on the right of the =
refers to the one defined on the left of the =
- not the one defined in line 1. Therefore the definition is infinitely recursive and loops forever.
If you want to define a variable based on another one, you have to give it a different name:
let localAlbumStr = albumStr ++ "nothing"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With