I recently started picking up the Haskell programming language. I've managed to learn other languages rather quickly, but I'm still having a difficult time grasping some of the basics of Haskell.
I decided to practice and wrote a program that calculates the Fibonacci Sequence (original, I know) a certain amount of times and adds the results to a list, then divides each number in the list by 7 and creates a new list of the remainders of that division. It then assigns a musical note letter based in A minor (A, B, C, D, E, F, and G), and prints out each letter. The idea was to give me new ideas for writing melodies.
The program works, but with how efficient Haskell is I feel like it's way too long for what it does. How would you guys suggest re-writing this and what are some shortcuts I can use to write better code?
Here's the program:
import Data.List import System.IO fibo a b = a:fibo b (a+b) divBy7 x = x `mod` 7 calcNums = take 16 $ map divBy7(fibo 0 1) returnNote :: Int -> String returnNote n | (n == 0) = "A" | (n == 1) = "B" | (n == 2) = "C" | (n == 3) = "D" | (n == 4) = "E" | (n == 5) = "F" | (n == 6) = "G" -- the number will never be higher than 6 due to the pisano period output = map returnNote(calcNums)
I'm just running ghci and asking it to return