简体   繁体   中英

The parameter order of function for ocaml

Suppose I have a map function like below:

let rec map f xs = match xs with
[] -> []
| hd :: tl -> f hd :: (map f tl)

I got some errors when I change the signature of map to let rec map xs f , Could anyone pinpoint which knowledge I am lacking and explains why?

There's no problem if you change the definition and the recursive call .

# let rec map xs f = match xs with
  [] -> []
  | hd :: tl -> f hd :: (map tl f);;
val map : 'a list -> ('a -> 'b) -> 'b list = <fun>
# map [3; 5; 7] ((+) 1);;
- : int list = [4; 6; 8]

You should be able to switch the xs with the f

the only thing you have to make sure to change is the the order on line 3 where you have (map f tl) to (map tl f)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM