tags:

views:

87

answers:

3

is there a way to iterate list over the list through List.map?

I know List.map takes single function and list and produce a list that the function applies to all elements. But what if i have a list of function to apply a list and produce list of the list ?

+5  A: 

Your question is not very clear, however as far as I understand it, you have a list of functions and a list of values. If you want to apply all functions to all elements then you can write this:

(* // To get one nested list (of results of all functions) for each element *)
List.map (fun element ->
  List.map (fun f -> f element) functions) inputs

(* // To get one nested list (of results for all elements) for each function *)
List.map (fun f ->
  List.map (fun element -> f element) inputs) functions

In case this is not what you wanted, could you try clarifying the question a little bit (perhaps some concrete example would help)?

Tomas Petricek
i think you got my idea, a function takes a list of function and a list of value and apply each function to all value. The result is the result of each function for all values. Ex) [(...);(...);(...)] list of function and [1;2;3;4] list of value then result is [[true, true, false false]; [false, true, true, true]; [...]; [...]]
REALFREE
I thought this was an Ocaml question not an F# one? I don't think OCaml has a |> operator.
sashang
@sashang: Thanks for the correction - I wasn't sure whether OCaml has that operator (but I thought it is so useful, so it must have it too - a mistake!). The rest of the answer should be correct.
Tomas Petricek
Ocaml Batteries Included provides the |> operator. Despite it not being in the standard library, it's becoming part of common usage.
Thelema
A: 

You can try this :

let rec fmap fct_list list = match fct_list with
    [] -> //you do nothing or raise sth
    head::tail -> List.map head list :: fmap tail list;;
Dimitri
i already knew how to do this with recursive function.. but the trick is i cannot use recursive function directly but through List.map
REALFREE
Note that // isn't a legitimate way to comment code in OCaml
sashang
I don't think it is possible only with List.map. Maybe you will have to use another functions with it like List.iter, or List.fold_left or fold_right,etc
Dimitri
@sashang, it has been two years since i did not program in Ocaml
Dimitri
A: 

Are you allowed to use List.map2? Because then this is simple:

let lista = [(fun x -> x + 1); (fun x -> x + 2); (fun x -> x + 3)];;
let listb = [1; 1; 1];;
let listc = List.map2 (fun a b -> (a b)) lista listb;;

The output would be [2; 3; 4]

Edit: wait, I think I read your problem wrong. You want to get a list of lists, where each list contains a list of a function applied to the initial list? In other words, for the lista and listb above, you'd get:

[[2;2;2];[3;3;3];[4;4;4]]

Is this correct?

Niki Yoshiuchi
I didn't know there is list.map2 but i should use multiple list.map and nested it to loop over the list but still lost
REALFREE
This solution only applies each function to one element, and there must be one function for each element.
Thelema
Yeah, that's what my edit was about. The problem was worded poorly and can be interpreted several different ways. I realized that my initial interpretation is wrong, but I'm still not entirely clear what is actually being asked.
Niki Yoshiuchi