ภาษา haskelllanguage:เปลี่ยน(n,m)พวธาตุอยู่ในรายชื่ออยู่ในรายชื่อ

0

คำถาม

คน 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
haskell
2021-11-23 23:01:59
2

คำตอบที่ดีที่สุด

2

ฉันคิดว่าคุณมีคำตอบของคุณและสามารถทำให้ความคืบหน้าแต่ฉันคิดว่ามันอาจจะเป็นที่น่าสนใจที่จะแสดงให้คุณเป็นตัวเลือก implementation. มีบางอย่าเป็นสัญลักษณ์แห่งในของคุณที่มีอยู่แล้วขึ้นรหัสของ traversing รายชื่อต้องเฉพาะเรื่องจุดและแล้วกำลังเปลี่ยนแปลงได้ นี่เป็นไอเดียสำหรับว่าคุณอาจจะดำเนินนั่นแค่ครั้ง:

onIndex :: Int -> (a -> a) -> ([a] -> [a])
onIndex n f = if n < 0 then id else loop n where
    loop _ [] = []
    loop 0 (x:xs) = f x:xs
    loop n (x:xs) = x:loop (n-1) xs

มากกว่าต้องกินธาตุออกและแทนที่นั่นธาตุนี้ต้องเป็นเผด็จการซะหน่อทำการแก้ไขฟังก์ชันและจะใช้กับมันฟังก์ชันต้องธาตุที่เหมาะสมตำแหน่งแล้ว เราสามารถใช้นี่เพื่อแทนที่การอีลีเมนต์กับค่า foo ใช้ const foo เป็นการแก้ไขฟังก์ชัน. แต่,และนี่คือกุญแจสำคัญสิ่งที่เราสามารถใช้ข้อแก้ไขงานที่เรียกตัวมันเอง onIndex!

replaceAtMatrix :: (Int, Int) -> a -> [[a]] -> [[a]]
replaceAtMatrix (m, n) = onIndex m . onIndex n . const
2021-11-24 01:43:23
1

ไม่ใช้ length. เรื่องนี้จะเกิดข้อผิดพลาดขึ้นไม่จำกัดรายชื่อ:คุณไม่ได้ต้องการ:ในกรณีที่ number > length somelistในที่สุดมันจะมาถึงจุดจบของรายการ:

replaceAtList :: Int -> a -> [a] -> [a] 
replaceAtList _ _ [] = []
replaceAtList n _ xs
    | n < 0 = xs
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 (m, _) _ xs
    | m < 0 = xs
replaceAtMatrix (0,num2) a (x:xs) = replaceAtList num2 a x : xs
replaceAtMatrix (num1,num2) a (x:xs) = x : replaceAtMatrix (num1-1,num2) a xs
2021-11-23 23:19:22

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่