3 ms·
i'd like to see it written without a loop, using map or filter...
by Tycho 14y ago
i'd like to see it written without a loop, using map or filter...
- kroger 14y agoYou can se a solution in Python here: http://news.ycombinator.com/item?id=4828828 http://news.ycombinator.com/item?id=4828828
- tinco 14y agoAlright, time to fire up ghci then.. it doesn't work with map or filter, but you get the idea: notes = concat $ repeat ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"] majorSteps = [1,1,0,1,1,1,0] scale key steps = key : skip notesFromKey majorSteps where (_:notesFromKey) = dropWhile ((/=) key) notes skip list skips = fst $ foldl next ([], list) skips where next (m, xs) s = (m ++ [xs !! s], drop (s + 1) xs) edit: cleaned it up a lot. This is Haskell by the way. The loop is replaced by a 'fold' in the skip function that reduces the list by skipping through it taking the skip distances from the skips argument.
- mifrai 14y agoSimilar concept but a little simpler and avoids manually written recursion. notes = cycle ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"] majorSteps = [2, 2, 1, 2, 2, 2] :: [Int] scale note = map (dropWhile (/= note) notes !!) . scanl (+) 0 I think it's less clear, but if you want to avoid the rescan via (!!), then scale note = map head . scanl (flip drop) (dropWhile (/= note) notes) Both result in λ> scale "d" majorSteps ["d","e","f#","g","a","b","c#"]