Map iterator.

This is a discussion on Map iterator. within the ml forums in Programming Languages category; Does anyone has a short segment of code using a map iterator. Cannot find an exemple. Thank you....

Go Back   Application Development Forum > Programming Languages > ml

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 11-12-2007, 06:22 PM
Sandor Lengyel
Guest
 
Default Map iterator.



Does anyone has a short segment of code using a map iterator.

Cannot find an exemple.



Thank you.




Reply With Quote
  #2  
Old 11-14-2007, 07:45 PM
David B. Benson
Guest
 
Default Re: Map iterator.

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]




Reply With Quote
  #3  
Old 11-14-2007, 07:47 PM
Andrew Smallshaw
Guest
 
Default Re: Map iterator.

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


Reply With Quote
  #4  
Old 04-03-2008, 08:33 PM
Nikki
Guest
 
Default Re: Map iterator.

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

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:12 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.