Haskell, unexpected output

Discussion in 'Tech Discussion' started by Zaart, May 30, 2020.

Tags:
  1. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    Hi guys,
    i don't get why in the following haskell code:

    test :: [[Char]] -> [Char]
    test [a] = a
    test _ = "yo"

    when i send:

    > ["ay", "oy", "ey"]

    i always get "yo" as an output.

    i tried looking on stackoverflow but didn't find my answer Q-Q

    i am quite new and i don't get why it enter in the one with '_'.
     
  2. Jeebus

    Jeebus Well-Known Member

    Joined:
    Jun 20, 2017
    Messages:
    904
    Likes Received:
    780
    Reading List:
    Link
    Haskell isn't a super-common language. It might be better to look for a forum dedicated to Haskell.

    I don't know Haskell, so I won't be much help. If anyone can help, it might be useful to include some extra information. What output do you expect, and is what you posted just a snippet? If it's just a snippet, it might be helpful to post the code in its entirety.
     
  3. eulerman

    eulerman Well-Known Member

    Joined:
    Aug 19, 2016
    Messages:
    192
    Likes Received:
    160
    Reading List:
    Link
    I know this is old already but underscore(_) is basically "whatever" in haskell. So you are saying in that line 'if you reached here, then return "yo"'
    I'm not sure what you wanted to do here. If you are still hanging in it, I could help you
     
    Zaart likes this.
  4. Zaart

    Zaart Deer Master Race

    Joined:
    Oct 15, 2016
    Messages:
    1,320
    Likes Received:
    2,011
    Reading List:
    Link
    Thank you very much for the offer =P
    But i did manage to finish this ^^
    As usual it was me making a dumb mistake =P
    But still thanks you very much for the offer, that was quite kind