Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Haskell question

Back to Forum: Tech Off
  • User profile image
    trifonius

    Can somebody explain to me why the following works:


    add2 = foldr (\ x xs -> x + 2 : xs) []

     

    but this does not:

    appendTest = foldr(\ x xs -> x ++ "Test" : xs) []

     

    I get a type mismatch between [[[Char]]] and [[Char]] when I call

    appendTest ["one", "two"]

     

    I need to do it this way:

     

    appendTest = foldr(\ x xs -> (\ x -> x++ "Test") : xs) []

     

    I am not sure why though, especially since the Int example works. Sure, String is [Char], but I can't see why the first version of appendTest expects a [[[Char]]] instead of a [[Char]].

     

    Thanks a lot!

  • User profile image
    W3bbo

    Have you tried reformulating as separate functions rather than "trying to be smart on the first go" with the embedded lambda expression? When I'm doing Haskell it helps to identify problem areas.

  • User profile image
    A N Satrawala

    it may be due to + binding with higher precedence compared to : in comparison with ++

    placing brackets like should work (x ++ "Test")

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.