:PROPERTIES:
:ID: ce613120-bb95-4488-82dc-6f2fcc144884
:mtime: 20231023024015
:ctime: 20231023024009
:END:
#+title: list map
#+filetags: :public:project:
* Definition
In the context of [[id:2dbb5e8a-32bf-49aa-ba92-9a98fe41c2fc][Functional Programming]], the *list map* function
applies a function to each element of a list
* Uses of List Map
List map is very useful.
You can use list map to implement
[[id:bb1b3c8e-37fc-4121-b826-a221fd44dd79][matrix transpose.]]
#+BEGIN_SRC ocaml
let rec transpose = function
| [] :: _ -> []
| rows -> (List.map List.hd rows) :: (transpose (List.map List.tl rows))
#+END_SRC
* List Map in [[id:8952459c-5076-4e68-8a68-5f658209f39e][Ocaml]]
#+BEGIN_SRC ocaml
let rec map f = function
| [] -> []
| x :: xs -> (f x) :: map f xs
#+END_SRC
#+RESULTS:
: <fun>
#+BEGIN_SRC ocaml :exports both
map ((^) "hello ") ["John" ; "James"]
#+END_SRC
#+RESULTS:
| hello John | hello James |
* List Map in Haskell