Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does haskell stop when trying to add a string to the end of this string?

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

like image 344
Dean Avatar asked Dec 02 '22 09:12

Dean


1 Answers

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"
like image 200
sepp2k Avatar answered Dec 04 '22 03:12

sepp2k