부모 함수에서 인스턴스화 된 구조가 있고 그 부모 함수의 함수를 호출하여 해당 인스턴스화 된 데이터를 수정하려고합니다. 고안된 예가 있습니다 :동일한 작업을 수행하기위한 대안은 무엇입니까?
import Data.List
data MyLists = MyLists {
myInts :: [Int],
myBools :: [Bool]
} deriving (Show)
addIntToList :: Int -> MyLists -> MyLists
addIntToList x main_lists =
main_lists { myInts = Data.List.insert x my_ints }
-- might make a call to another child function that modifies main_list here, and so on (i.e., this is the vertical problem I see with this structuring)
where
my_ints = myInts main_lists
main :: IO()
main = do
let my_main_lists = MyLists [1,2,3] [False, True, False]
let my_new_main_lists = addIntToList 4 my_main_lists
print my_new_main_lists
let my_new_new_main_lists = addBoolToList True my_new_main_lists
print my_new_new_main_lists
-- and so on (this is the lateral problem I see with this code structuring)
이 코드를 구조화하거나 이와 유사한 작업을 수행하는 다른 방법은 무엇입니까? 더 간결한 방법이 있습니까?
자식 함수에 대한 긴 함수 호출 체인을 만들면 특히 냄새가 심합니다 (코드 냄새). 그들 모두는 새로운 MyLists
을 돌려 주거나 그냥 아무 것도하지 않고 main_list
을 돌려 줄 필요가 있습니다. 부모님은 MyList
과 다른 반환 값 (예 : -> (Bool, MyList)
)을 처리해야 할 수도 있습니다.
그래서 MyList 매개 변수와 반환 값을 필요로하는 함수 호출의 트리 구조를 상상할 수 있습니다. 그것은 최적으로 보이지 않습니다.
다음은 내가 말하고자하는 구체적인 예입니다. https://github.com/mokehehe/monao (haskell의 슈퍼 마리오 클론) 코드를 찾아보십시오. state.monad는 전혀 사용되지 않으며, 코드 전체에 걸쳐 있어야하는 상위 레벨 구조가 있음을 알 수 있습니다 (예 : Main.hs의 GameGame).
예상되는 출력은 무엇입니까? 정렬 할 int 목록이 있습니까? – epsilonhalbe
예상대로 출력됩니다 ('MyLists {myInts = [1,2,3,4], myBools = [False, True, False]}'). 나는이 코드를 구조화하는 더 좋은 방법을 찾고있다. – joshj
'addBoolToList True my_main_lists'에서, 그것은'my_new_main_lists'가되어서는 안됩니까? –