1

I'm trying to define map in ghci recursively. What I've come up with so far is the following:

let mymap f (x:xs) = if null xs then [] else f x : map f xs

What I'd like to do now is to simplify it a bit and hardcode the list inside the code, i.e., write a map function which takes a function as argument and does what the real map does but only to a specific list e.g., [1, 2, 3, 4, 5]. Is such a thing possible?

2
  • Sure. It's just mymap f = [f 1, f 2, f 3, f 4, f 5]. This is pointless, however. Commented Jul 7, 2014 at 15:38
  • @TomEllis I'd do that as mymap f = map f [1..5]. Commented Jul 8, 2014 at 6:53

1 Answer 1

6

First of all, your map function isn't entirely correct. If I were to input mymap (+1) [1], I would expect to get [2] back, but instead I'd get []. If I tried mymap (+1) [], my program would crash on a pattern match failure, since you haven't defined that case. Instead, consider defining your mymap as

mymap :: (a -> b) -> [a] -> [b]
mymap f [] = []
mymap f (x:xs) = f x : mymap f xs

If you want to do it inline with an if statement then you'd have to do

mymap f xs = if null xs then [] else f (head xs) : mymap f (tail xs)

These do essentially the same thing, but the first is a bit easier to read in my opinion.


If you want to use mymap to define a function that maps only over a specific list, you could do so pretty easily as

mapOnMyList :: (Int -> b) -> [b]
mapOnMyList f = mymap f [1, 2, 3, 4, 5]

Or in point-free form

mapOnMyList = (`mymap` [1, 2, 3, 4, 5])

using mymap as an infix operator. This is equivalent to flip mymap [1, 2, 3, 4, 5], but the operator form is usually preferred since flip is not necessarily free to execute.


You can also do this using list comprehensions:

mymap f xs = [f x | x <- xs]

Or if you want to hard code the list

mapOnMyList f = [f x | x <- [1, 2, 3, 4, 5]]
Sign up to request clarification or add additional context in comments.

16 Comments

Personally, I find flip much more readable than infix operators, but that's just taste, I guess.
Actually I'd like to have my initial inline definition (or yours which is more correct) and hard code the list inside the code but without touching the rest of the code.
@Mephy I agree that it's more readable, but flip is not inlined, so if your usage is in an inner loop then you could suffer a performance penalty. GHC may be able to optimize this away, but the source doesn't give any indication that it does.
@SotirisKal I'm not sure I understand what you're wanting to do. Do you want the list inside the definition of mymap without having to write an additional function?
@genisage You can get at least one more flip in there: let yourMap = flip (.) ((.) (:)) (flip ((flip id) [] (flip foldr)) [1..10])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.