| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Does anyone has a short segment of code using a map iterator. Cannot find an exemple. Thank you. |
|
#2
| |||
| |||
| On Nov 12, 3:22 pm, Sandor Lengyel <sleng...@chello.hu> wrote: > Does anyone has a short segment of code using a map iterator. > ... List.map? If so val addedones = List.map (fn(x) => x+1) [0,1,2,3,4] |
|
#3
| |||
| |||
| On 2007-11-12, Sandor Lengyel <slengyel@chello.hu> wrote: > > Does anyone has a short segment of code using a map iterator. > > Cannot find an exemple. map is probably the simplest of the higher order functions to get your head around - all it does is apply the function given as its first parameter to each element in the second, and returns a new list containing the results of those applications. If the function given to map if of type "a -> b", then the list passed to map must be of type "a list", and you will get a list of type "b list" in response. These types can of course be the same, but in the example below I have made them different for clarity. The function "take6_and_stringify" is of type "int -> string" and so we pass map a list of ints and get back a list of strings: - fun take6_and_stringify n = makestring (n - 6); > val take6_and_stringify = fn : int -> string - val source = [0,1,2,3,4,5,6]; > val source = [0, 1, 2, 3, 4, 5, 6] : int list - val dest = map take6_and_stringify source; > val dest = ["~6", "~5", "~4", "~3", "~2", "~1", "0"] : string list -- Andrew Smallshaw andrews@sdf.lonestar.org |
|
#4
| |||
| |||
| On 13/11/2007 00:22:01, Sandor Lengyel wrote: > > > Does anyone has a short segment of code using a map iterator. > > Cannot find an exemple. > > > > Thank you. > > > > Unlike the other posters, I'm going to assume you were asking about ocaml's Map module, and in case you never got an answer, and still care, here: module StrMap = Map.Make(String);; let x = StrMap.add "hello" 10 StrMap.empty in let y = StrMap.add "world" 20 x in StrMap.iter (fun k v -> Printf.printf "Map of %s => %d\n\r" k v) y;; Produces: bash-3.1$ ocaml maptest.ml Map of hello => 10 Map of world => 20 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.