PDA

View Full Version : Update a Haskell List in Runtime


kosta
11-01-2004, 01:32 PM
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="a"

and a function:
add::add::String->[String]
add x = [list1] ++ [x]

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...
Could some one help me ?!

weedweaver
12-21-2004, 02:44 PM
i dont know whether this is the answer you are looking for but i have read it right u just want to add one element at a time to a list:

addToList :: [String] -> String -> [String]
addToList x string = x ++ [string]

it takes a list as an input along with whatever element u want to add and then produces the concatenated output. to run type:

Main> addToList ["a","b","c"] "d"
["a","b","c","d"]

this is probably over simple and i have misread what it is you need to do. you could then write another function which pases a specific list to that function alon with whatever u want to add to the list e.g.

run :: [String]
run = addToList ["a","b","c"] "d"

if u want to add more than one element at a time change:

addToList :: [String] -> String -> [String]
addToList x string = x ++ [string]

to

addToList :: [String] -> [String] -> [String]
addToList x y = x ++ y

then run it by typing:

Main> addToList ["a","b","c"] ["d","e"]
["a","b","c","d","e"]

hope this helps maybe a little.