나는 foldl 튜플 목록을 통해 이동하여 문자열을 만드는 데 사용하는 함수를 얻으려고하고 있어요. 이미 재귀를 사용하여 작동하는 비슷한 함수를 만들려고합니다. 여기 Haskell 재귀 foldl처럼 사용
내가 컴파일하기 위해 노력하고있어 코드입니다 : 제공된 코드의 맨 마지막 줄에 따라서citeBook :: (String, String, Integer) -> String
citeBook (name, titl, date) = (titl ++ " (" ++ name ++ ", " ++ show date ++ ")\n")
--Recursion function
-- function must be called with putStr in order for newlines to work
bibliography_rec :: [(String, String, Integer)] -> String
bibliography_rec [] = ""
bibliography_rec xs = (citeBook(head xs) ++ bibliography_rec (tail xs))
--foldl function
bibliography_fold :: [(String, String, Integer)] -> String
bibliography_fold [] = ""
bibliography_fold (x:xs) = foldl (++) citeBook(x) xs --ERROR HERE
, 나는 foldl 사용을 위해 노력하고 있어요 (++) 연산자와 같이 목록에있는 문자열을 조합하십시오. x가 목록에서 가져온 첫 번째 튜플 이 될 것이므로 기본 사례로 citeBook (x)을 사용하고 있습니다. citeBook (x)은 문자열을 반환합니다. 그런 다음 목록 xs와 함께 접기를 계속하십시오.
여기에 오류가 있습니다. 나는 foldl 내 매개 변수 유형이 아닌 예상 무슨와 일치 생각하지만, 모든
hw1.hs:28:34:
Couldn't match type `[a0]'
with `(String, String, Integer) -> String'
Expected type: ((String, String, Integer) -> String)
-> [a0] -> (String, String, Integer) -> String
Actual type: [a0] -> [a0] -> [a0]
In the first argument of `foldl', namely `(++)'
In the expression: foldl (++) citeBook (x) xs
In an equation for `bibliography_fold':
bibliography_fold (x : xs) = foldl (++) citeBook (x) xs
hw1.hs:28:48:
Couldn't match expected type `[[a0]]'
with actual type `(String, String, Integer)'
In the third argument of `foldl', namely `(x)'
In the expression: foldl (++) citeBook (x) xs
In an equation for `bibliography_fold':
bibliography_fold (x : xs) = foldl (++) citeBook (x) xs
hw1.hs:28:51:
Couldn't match expected type `(String, String, Integer)'
with actual type `[(String, String, Integer)]'
In the fourth argument of `foldl', namely `xs'
In the expression: foldl (++) citeBook (x) xs
In an equation for `bibliography_fold':
bibliography_fold (x : xs) = foldl (++) citeBook (x) xs
내가 어떤 모든 의견을 보내 주셔서 감사합니다 .. 나에게 좋아 보인다. 감사!
'bibliography_rec'는'concatMap citeBook'이 아니십니까? – bheklilr
예, 답변으로 게시하십시오. – amnn
bheklilr, 방금 해보았지만 작동하지만 명시 적으로 재귀 함수를 만들려고했습니다.내 문제는 내가 bibliography_rec 같은 목록을 인쇄하려고하지만 재귀 대신 foldl 사용 –