0

I'm trying to create a function myMap that applies x to a list of functions using map.

Example:

myMap [f1, f2, ..., fn] x 
==> [f1(x), f2(x), ..., fn(x)]

I believe I need to write an anonymous function to complete this task, but am having trouble comprehending how they work.

My Attempt:

fun myMap [x] y = [fn => x ] => x;
chris
  • 4,988
  • 20
  • 36

1 Answers1

2

I'm not sure what you were trying to do in your attempt, since it's not valid sml, but you can apply list of function to a single value like this

- fun myMap x fns = map (fn f => f x) fns;
val myMap = fn : 'a -> ('a -> 'b) list -> 'b list

You can try it out

- myMap 1 [(fn x => x+1), (fn x => x+2), (fn x=> x+3)];
val it = [2,3,4] : int list

You can see that syntax for anonymous functions is fn arg => body.

zaquest
  • 2,030
  • 17
  • 25