HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

I can has(kell) cheezburger?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
kellhascancheezburger

Problem

Edit 13 June:

I have made a new and improved version here.
Original Question

This is a very rudimentary lolcats translator (it only works on the phrase "Can I have a cheeseburger") in Haskell. This is my first ever attempt at Haskell (or any functional programming language). I am sure that my code is absolutely atrocious (the documentation was dismal) but it works.
Teh Codez

import Data.Strings
main = do
    putStrLn "Enter your text to translate"
    inputText <- getLine
    let capsText = strToUpper inputText
    let a = strReplace "HAVE" "HAS" capsText
    let b = strReplace "CAN I" "I CAN" a
    let c = strReplace "CHEESEBURGER" "CHEEZBURGER" b
    let d = strReplace " A " " " c
    putStrLn (d)
    getLine

Solution

It is good practice in Haskell to separate the functional code from the IO. In this case, you could (and therefore should) define a lolcat :: String -> String function. Be sure to put a type declaration on all functions — you didn't write one for your main.

Defining variables a, b, c, and d is overkill. I would write this as a composition of functions.

lolcat :: String -> String
lolcat = strReplace " A " " " .
         strReplace "CHEESEBURGER" "CHEEZBURGER" .
         strReplace "CAN I" "I CAN" .
         strReplace "HAVE" "HAS" .
         strToUpper

Code Snippets

lolcat :: String -> String
lolcat = strReplace " A " " " .
         strReplace "CHEESEBURGER" "CHEEZBURGER" .
         strReplace "CAN I" "I CAN" .
         strReplace "HAVE" "HAS" .
         strToUpper

Context

StackExchange Code Review Q#131448, answer score: 40

Revisions (0)

No revisions yet.