February 2, 2024
Haskellbrain: Week 3 of 48 in 24
Well, I’m quite late with this week’s challenges. Again, I was only going for the silver medal of doing any three languages, but I didn’t have a lot of time to spare for the challenges last week, and I also spent more time than I should have tweaking my haskell solution in particular.
This week’s exercise is fizzbuzz, but slightly different.
JavaScript First up, here’s my javascript solution.
export const convert = (num) => { let s = [[3, "Pling"], [5,"Plang"], [7,"Plong"]] .map(([x,y]) => num % x === 0 ? y : "") .join(""); return s === "" ? num.toString() : s; }; The basic idea is to take a list of pairs (a divisor and a string) and map that pair to the string if $n$ is divisible by the divisor, or map to the empty string otherwise. We then join those strings together. The final wrinkle is to check if the string we have left over is is still empty, and if it is, return the number as a string, rather than the resultant string.
Read more