Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Can anyone explain to me why my Haskell function gives rise to a type-definition error? Originally, I wrote the following function to subtract one from the first n elements in a list: dec_first :...
#1: Initial revision
How to use function composition for applying a function to first elements of a list?
Can anyone explain to me why my Haskell function gives rise to a type-definition error? Originally, I wrote the following function to subtract one from the first `n` elements in a list: ```haskell dec_first :: Int -> [Int] -> [Int] dec_first 0 l = l dec_first n (x:xs) = (x-1):dec_first (n-1) xs ``` However, as a challenge, I wanted to write this function using available standard functions, using function compositions. This is how I ended up writing the function (for the subtraction part) ```haskell dec_first :: Int -> [Int] -> [Int] dec_first = map (subtract 1) . take ``` but this function does not compile due to a type mismatch. In my attempt to fix the error, I also tried the following variant: ```haskell dec_first :: Int -> [Int] -> [Int] dec_first n = map (subtract 1) . take n ``` This function actually does work as expected. However, if I specify all parameters again, things breaks again: ```haskell dec_first :: Int -> [Int] -> [Int] dec_first n l = map (subtract 1) . take n l ``` Can anyone help me understand why this function only works with one variable, but not in the other settings?