Litle problem with a haskell Function...
Hello, I' a Haskell Newbie and i need some help about a function.. My problem is to update a list of values in Runtime. For example i have a initial list: list1=3D"a"=20 and a function: add::add::String->[String] add x =3D [list1] ++ [x]=20 I need to do=20 add "c"=20 and the "c" most be append to list1 to make list1=3D["a","c"] Then if i input list1 the output must be ["a","c"] But i can't do it... Could you help me ?!
Manuel Costa wrote:
I need to do add "c"
and the "c" most be append to list1 to make list1=["a","c"]
Then if i input list1 the output must be ["a","c"]
But i can't do it...
It can't be done. Your source code defines list1 to be equal to "a", and so equal to "a" it is. It can't also be equal to something else. There is a language feature that would give you something similar to what you're asking for, but I'm not going to mention it because I'm 99% sure it's not what you really want here, and it would just lead you down the wrong path. In general you need to explicitly pass around everything that you want to "change" -- for example, you could write your add function like this: add x list = list ++ [x] and then every time you call it, use the list it returns from then on instead of the list you passed to it. -- Ben
participants (2)
-
Ben Rudiak-Gould -
Manuel Costa