I've been following Haskell tutorials online, and I think it's really cool that the head, tail, init
, and last
are all built-in. I thought as a test, it'd be really cool if I could write the Longest Common Subsequence algorithm in one line. My code is as follows:
lcs x y = if length x == 0 || length y == 0 then 0 else if (last x) == (last y) then (lcs (init x) (init y))+1 else (lcs (init x) y + lcs x (init y))
I've applied parenthesis liberally, but when I call lcs "abc" "abd"
I get 6
, which obviously isn't right.