Today I thought it'd be fun to take a look at a few common & simple "interview questions" in Haskell. These sorts of questions are often used to establish whether someone has programming and problem solving skills, and I thought it might be useful for folks to see how they play out in Haskell since our beloved language's solutions tend to follow a different paradigm than most other languages do. I'll withhold any judgement on whether these questions are in any way helpful in determining programming skill whatsoever 馃槄; please don't @ me about it. Palindromes Let's start off nice and easy with the standard "is it a palindrome" question! The task is to write a function which determines whether a given string is a palindrome (i.e. whether it reads the same in both reverse and forwards) isPalindrome :: String -> Bool isPalindrome str = str == reverse str >>> isPalindrome "racecar" True >>> isPalindrome "hello world!" False That'll do it! Not much to say about this one, it's nice that our definition roughly matches an English sentence describing the problem "does a given string equal itself in reverse". I'll leave it as an exercise for the reader to expand it to handle differences in capitalization however you like. Fizz Buzz Next up is the infamous Fizz Buzz! For the 3 of you who are unfamiliar, for each number from 1 to 100 we need to print out "Fizz" if it's divisible by 3, "Buzz" if it's divisible by 5, and "Fizz Buzz" if it's divisible by both 3 AND 5! Otherwise we print the number itself. Let's see it! import Data.Foldable fizzle :: Int -> String fizzle n | n `mod` 3 == 0 && n `mod` 5 == 0 = "Fizz Buzz!" | n `mod` 3 == 0 = "Fizz!" | n `mod` 5 == 0 = "Buzz!" | otherwise = show n main :: IO () main = do for_ [1..100] (putStrLn . fizzle) >>> main 1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16 -- ...you get the idea I write a helper function "fizzle" here which converts a number into its appropriate string so I can keep the "printing" logic separat...
First seen: 2025-05-23 03:28
Last seen: 2025-05-23 07:28