Haskell uses foldl as recursion

I am trying to get a function that uses foldl to go through a list of tuples and create a string from it. I am trying to create a similar function that already works using recursion.

Here is the code I'm trying to compile:

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

So, in the very last line of the provided code, I'm trying to make foldl use (++) as an operator to concatenate the lines in the list. I use citeBook (x) as the base code, since x will be the first tuple taken from the list. Note that citeBook (x) returns a string. Then continue curled up with the xs list.

Here are the errors I get. I think my parameter types for foldl are not matching what is expected, but everything seems fine to me.

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

I appreciate any feedback. Thanks!

+4
2

foldl (++), String -> String -> String. , , xs, [(String, String, Integer)], [String].

bibliography_fold

bibliography_fold :: [(String, String, Integer)] -> String
bibliography_fold [] = ""
bibliography_fold (x:xs) = foldl (++) (citeBook x) (map citeBook xs)

bibliography_fold :: [(String, String, Integer)] -> String
bibliography_fold xs = foldl (++) "" (map citeBook xs)

noob Haskell, .

, (citeBook x), citeBook(x), , citeBook (x) foldl ( , ). , , , .

+4

, :

bibliography_fold :: [(String, String, Integer)] -> String
bibliography_fold = foldr ((++) . citeBook) ""

, , . GHCi :info, , . foldr, ++, citeBook, (++) . citeBook, , , . foldr.

+3

All Articles