Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute F# Sharp Online

//ORIGINAL CODE
let firstList = [5;15;20;25;30]
let secondList = [50;100;150;200;250;300]


//PROBLEM ONE
let max num1 num2 =
    if num1 > num2 then num1
    else num2

let rec problemOne list =
    match list with
    |[] -> 0
    |head::tail -> max head (problemOne tail)//conspatter ::.
    
let largestSum = problemOne firstList

printfn "\nPROBLEM ONE\nThe maxiumum integer in the list %A is: %d\n" firstList largestSum



//PROBLEM TWO
let iterator = 2

let rec problemTwo list intPassed = 
    match list with
    |[] -> 0
    |head::tail -> 
        if intPassed = 0 then head
        else problemTwo tail (intPassed-1)
    
let sumAtIndex = problemTwo firstList iterator

printfn "\nPROBLEM TWO\nThe integer at index %d in list %A is: %d\n" iterator firstList sumAtIndex


//PROBLEM THREE
//Second function to reorder the list after it has been doubled so it is in the proper order
let emptyList = []

let rec problemThree (list1 : int list) (list2 : int list) =
    match list1 with
    |head::tail -> problemThree tail ((head*2)::list2)
    |[]-> list2       

let rec problemThreeReOrder (list1 : int list) (list2 : int list) =
    match list1 with
    |head::tail -> problemThreeReOrder tail (head::list2)
    |[] -> list2

    
let doubledList = problemThree firstList emptyList
let reOrderedDoubledList = problemThreeReOrder doubledList emptyList
printfn "\nPROBLEM THREE \nThe original list is: %A\nThe doubled list is: %A\n" firstList reOrderedDoubledList

//PROBLEM FOUR
let rec problemFourFunction (intPassed : int) (listPassed : int list) : int =
    match listPassed with
    |[] -> -1
    |head::tail ->
        if intPassed = head then 1
        else problemFourFunction intPassed tail
        
let intResult = problemFourFunction 4 firstList
printfn "\nPROBLEM FOUR\n"
printfn "The searched for integer was %d and the list searched was %A\n" 4 firstList
printfn "If 1 the result was found, if -1 the result wasn't found: --> %d\n" intResult

//PROBLEM FIVE

let emptyTupleList = []
//(list1 : int list) (list2 : int list) (list3 : int list)
let rec problemFive list1 list2 list3 =
    match list1, list2 with
    |(list1Head::list1Tail),(list2Head::list2Tail) -> problemFive (list1Tail) (list2Tail) ((list1Head,list2Head)::(list3))
    |[]-> list3  
    
let tupleList = problemFive firstList secondList emptyTupleList

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.