Author Topic: (Challenge) mapcar  (Read 4158 times)

0 Members and 1 Guest are viewing this topic.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: (Challenge) mapcar
« Reply #15 on: May 01, 2009, 02:38:13 PM »
I started my journey into functional programming a couple of weeks ago and started trolling the LISP groups to help revive some long dormant grey cells.  I stumbled across this challenge early this week and was thrilled to see all the multi-language replies, especially Herman's, since F# is the FP language that I'm starting with.  I'm glad to know that there's at least one familiar person that I can turn to :-)

Here's my F# one liner submission.  It's the same as Herman's; it just uses currying and forward piping to clean things up a bit.
Code: [Select]
[[0..9];[10..19];[20..29]] |> List.map (List.map (fun n -> n*3));;

Which returns
Code: [Select]
[[0; 3; 6; 9; 12; 15; 18; 21; 24; 27];
   [30; 33; 36; 39; 42; 45; 48; 51; 54; 57];
   [60; 63; 66; 69; 72; 75; 78; 81; 84; 87]]

Although in real code I'd name it and allow any function to be passed in.
Code: [Select]
let nestedMap f inputList =
  inputList |> List.map (List.map f)

Anyway, great post, great replies.  I sure hope to see more.
Bobby C. Jones