คน replaceAtMatrix
ฟังก์ชันควรจะอยู่ใน tuple (Int,Int)
นที่จะตัดสินใจเลือกกำหนดสิ่งที่ตำแหน่งคืนค่ากลับมาเป็นค่าเป็นค่าจำเป็นต้องเปลี่ยนไป a
ค่าที่ต้องการเปลี่ยนแปลงและรายการในรายการ [[a]]
.
ถ้า tuple เป็นค่าเกินในรายชื่อความยาวดั้งเดิม [[a]]
ต้องการที่จะกลับมาแล้ว
replaceAtList :: Int -> a -> [a] -> [a]
replaceAtList _ _ [] = []
replaceAtList 0 a (_:xs) = a:xs
replaceAtList number a (x:xs)
| number < 0 = x:xs
| number > (length (x:xs)-1) = x:xs
| otherwise = x : replaceAtList (number-1) a xs
replaceAtMatrix :: (Int,Int) -> a -> [[a]] -> [[a]]
replaceAtMatrix _ _ [] = []
replaceAtMatrix (0,num2) a (x:xs) = replaceAtList num2 a x : xs
replaceAtMatrix (num1,num2) a (x:xs)
| num1 < 0 = x:xs
| num1 > (length (x:xs)-1) = x:xs
| num2 < 0 = x:xs
| num2 > (length (x:xs)-1) = x:xs
| otherwise = x : replaceAtMatrix (num1-1,num2) a xs
มันมีการทดสอบสำหรับซึ่งฉันตรวจสอบแล้วมันผ่านส่วนใหญ่ของพวกเขาแต่นั้นคืออันที่มันจะไปในไม่จำกัดวงจรมันครบแล้ว นี่คือการทดลองตรวจสอบมันเป็นอย่างนั้น
replaceAtMatrix (-1,0) 'A' ["bcd","lds","rtz"] == ["bcd","lds","rtz"]
replaceAtMatrix (0,0) 'A' ["bcd","lds","rtz"] == ["Acd","lds","rtz"]
replaceAtMatrix (2,0) 'A' ["bcd","lds","rtz"] == ["bcd","lds","Atz"]
replaceAtMatrix (3,0) 'A' ["bcd","lds","rtz"] == ["bcd","lds","rtz"]
replaceAtMatrix (2,2) 'A' ["bcd","lds","rtz"] == ["bcd","lds","rtA"]
และนี่เป็นคนเดียวที่ไม่ได้.
แก้ไข:ฉันเพิ่งนึกออกว่ามันเป็น probaly beacuse ฉันใช้ความยาวบนมีไม่จำกัดรายชื่อ จะให้ฉันเขียนใหม่มันได้โดยไม่ใช้ความยาว?
take 5 (replaceAtMatrix (1,2) 100 [[1..],[1,11..],[],[12,13,14]] !! 1) == [1,11,100,31,41]
โดยไม่มีผู้คุมฉันได้ตัวแปรไม่ได้อยู่ในขอบเขตข้อผิดพลาด
replaceAtList :: Int -> a -> [a] -> [a]
replaceAtList _ _ [] = []
replaceAtList 0 a (_:xs) = a:xs
replaceAtList number a (x:xs) = x : replaceAtList (number-1) a xs
replaceAtMatrix :: (Int,Int) -> a -> [[a]] -> [[a]]
replaceAtMatrix _ _ [] = []
replaceAtMatrix (0,num2) a (x:xs) = replaceAtList num2 a x : xs
replaceAtMatrix (num1,num2) a (x:xs) = x : replaceAtMatrix (num1-1,num2) a xs