View Full Version : Strike....
GnuVince
05-28-2002, 11:57 PM
The map function with pattern-matching
let rec mapp f = function
[] -> []
| h::t -> (f h)::(mapp f t);;
Strike
05-29-2002, 12:51 AM
Cool, here's my fixed map function.
# let null l = (l = []) ;;
val null : 'a list -> bool = <fun>
# let rec map func arg_list =
if null arg_list then []
else (func (List.hd arg_list))::(map func (List.tl arg_list));;
val map : ('a -> 'b) -> 'a list -> 'b list = <fun>
# map (fun x -> x * x) [1; 2; 3; 4];;
- : int list = [1; 4; 9; 16]
By the way, that "ledit" trick ROCKS. Paren matching, shifts lines to fit in the term ... great stuff.
Strike
05-29-2002, 01:39 AM
haha, some of this stuff is so cool:
# mapp (fun x -> (( ** ) (float_of_int x)) 2.0) [1; 2; 3; 4; 5];;
- : float list = [1; 4; 9; 16; 25]
# mapp int_of_float (mapp (fun x -> (( ** ) (float_of_int x)) 2.0) [1; 2; 3; 4; 5]);;
- : int list = [1; 4; 9; 16; 25]
GnuVince
05-29-2002, 08:10 AM
;)
By the way, I love your new avatar :)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.