Louie
Louie

Reputation: 21

Making palindromes in Haskell

I am trying to write a function that takes in a string and makes a palindrome out of it.

For example, ace becomes aceeca.

Upvotes: 1

Views: 2060

Answers (2)

Sergiu Starciuc
Sergiu Starciuc

Reputation: 574

palindrome [] = []
palindrome (x:xs) = append (x:(palindrome xs)) x
  where append (x:xs) y = x:(append xs y)
        append [] y = [y]

Upvotes: 0

Avilo
Avilo

Reputation: 1204

Pretty easy, just concatenate the reversed string onto itself.

palindrome s = s ++ reverse s

++ is the list concatenation function.

Let me point you toward Real World Haskell. It's a good book for learning the language if you're just getting started.

Upvotes: 7

Related Questions