If you're not allowed to use map
because this is homework, then you're probably not allowed to use foldr
either. And using foldr
to multiply every element in a list by 10 would be a rather silly way to do it (except as a learning exercise). As a general rule:
- If you want to do something to every element in a list, and get a list as the result, then you probably want some sort of map function.
- If you want to do something to every element in a list, and get a single value as the result, then you probably want one of the fold functions.
So let's write our own function. The function will take a list of numbers, and return a list. If you want to work with Int
s, then:
myFunction :: [Int] -> [Int]
(Better yet, instead of hard-coding in 10, you could let that be an input parameter. I'll leave that as an exercise for you.)
We could use recursion to implement this function. When using recursion, start by asking yourself "what is the base (simplest) case". Well, that would be an empty list. If we get a list, there's nothing to do, so we just return the empty list.
myFunction [] = []
Now, what if our list is non-empty? Well, we can multiply the first element by 10, and concatenate that with the result of running myFunction
on the rest of the list.
myFunction (x:xs) = x*10 : myFunction xs