Monday, February 06, 2012

F# code snippets - part 11

This snippet demonstrates the TAKE function available in most functional programming languages


//////////////////////////


// eleven.fs

//

// A program in F# which demonstrates

// how to extract first n elements from a list

// 

//

// Written by Praseed Pai K.T.

// http://praseedp.blogspot.com

//

//

//

// fsc eleven.fs

// eleven.exe

//





//-----------function take extracts the first i elements

//-----------from the list...



let rec TAKE i lst =

match i , lst with

| i,[]-> []

| i,x::xs-> if (i > 0) then x::(TAKE (i-1) xs) else [];







//---------------extract a list from another list



let newlst = TAKE 3 [1;4;9;16;25;36]





//--------------- print the extracted list..



printfn "%A" newlst



0 comments: