ฉันไม่สามารถใช้ฟังก์ชันลำดับสูงได้ ฉันไม่เห็นว่าจะต้องทำอย่างไร ฉันยังใหม่กับ haskell มาก มันจะต้องมีการเรียกซ้ำด้วย
split :: [Int] -> ([Int],[Int])
split xs =
ฉันได้รับสิ่งนี้เพื่อเริ่มต้นด้วย ฉันไม่รู้ด้วยซ้ำว่าจะเริ่มต้นปัญหานี้จากตรงไหน
ตัวอย่าง:
split []
([],[])
split [1]
([1],[])
split [1,2,3,4,5,6,7,8,9,10]
([1,3,5,7,9],[2,4,6,8,10])
ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมมาก
แก้ไข: ตำแหน่งคู่และคี่
So
แยก [3,6,8,9,10] จะเป็น ([3,8,10],[6,9])
โอเค ฉันคิดเรื่องนี้ขึ้นมา มันไม่สวย แต่ดูเหมือนว่าจะทำงานได้ดี
split :: [Int] -> ([Int],[Int])
split [] = ([],[])
split [xs] = ([xs],[])
split xs = (oddlist xs, evenlist xs)
oddlist :: [Int] -> ([Int])
oddlist xs | length xs <= 2 = [head(xs)]
| otherwise = [head(xs)] ++ oddlist(tail(tail(xs)))
evenlist :: [Int] -> ([Int])
evenlist xs | length xs <= 3 = [head(tail(xs))]
| otherwise = [head(tail(xs))] ++ evenlist(tail(tail(xs)))
split [1,3,2,5,8]
~›([1,3,5], [2,8])
- person Tom Lokhorst   schedule 14.09.2010