Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

Replace individual list elements in Haskell?

I have a list of elements and I wish to update them:

from this: ["Off","Off","Off","Off"]

to this: ["Off","Off","On","Off"]

As I am somewhat new to Haskell, I have been using (x:xs)!!y to extract and update individual components using the function:

replace y z [] = []
replace y z (x:xs)
  | x==y           = z:replace y z xs
  | otherwise      = x:replace y z xs

and then entering the following in ghci: (replace "Off" "On" ["Off",'Off","Off","Off"]) !! 2

I get the following: "On"

I seem to be able to extract and convert elements of a list but I can't seem to get a list up with the single element converted.

Any help regarding this matter would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typically, you modify elements of a list by splitting the list, replacing an element, and joining it back together.

To split a list at an index, we have:

 splitAt :: Int -> [a] -> ([a], [a]) 

which you can use to break up a list, like so:

 > splitAt 2 ["Off","Off","Off","Off"] 
 (["Off","Off"],["Off","Off"])

now you just need to pop the head element of the snd component of the list. This is easily done with pattern matching:

 > let (x,_:ys) = splitAt 2 ["Off","Off","Off","Off"]
 > x
 ["Off","Off"]
 > ys
 ["Off"]

you can now join the list back together, with an "On":

 > x ++ "On" : ys
 ["Off","Off","On","Off"]

I'll leave it to you to put those pieces together into a single function.


As a style note, I'd suggest using a new custom data type, instead of String for your toggles:

 data Toggle = On | Off deriving Show

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...