(* This is a comment *) (* Sample Hello World program using F# *) let sum a b : float = a + b let mul a b : float = a * b let avg a b : float = (a + b)/2.0 printfn "%A" (sum 20.0 3.0 |> avg <| mul 2.0 3.0)
open System //functions let multiply (x,y) = x*y Console.WriteLine(multiply (5,2)) //range of numbers let k = [for x in 1..10-> x*x] Console.WriteLine(k) //point function let m (a,b,c) = a+b*c Console.WriteLine(m(2,3,4)) //recursive function let rec fact x = if x <= 1 then 1 else x*fact(x-1) Console.WriteLine (fact(5)) //mutually recursive function let rec isOdd x = if x = 0 then false elif x = 1 then true else isEven(x-1) and isEven x = if x = 0 then true elif x = 1 then false else isOdd (x-1) Console.WriteLine("_________") Console.WriteLine( isOdd(5)) //swap function let swap (a, b) = (b, a) Console.WriteLine (swap(4,5)) //Square function let dnumb = [for x in 1..5 -> x*2] Console.WriteLine (dnumb) //Values as functions let square = fun x -> x*x Console.WriteLine (square(3)) //pattern matching using a function *doesnt use match keyword let rec factorial = function | n when n < 1 -> 1 | n -> n * fact (n - 1) Console.WriteLine (fact(5)) let rec fibbonaci = function | x when x < 1 -> 1 | x -> fibbonaci (x-1) + fibbonaci (x-2) Console.WriteLine(fibbonaci(7)) //pattern matching without a function *use match and with keywords let isOdd x = (x%2 = 1) let describeNo x = match isOdd x with | true ->printfn "x is odd" | false ->printfn"x is even" describeNo 6 let rec fib n = match n with | 0 -> 0 | 1 -> 1 | _ -> fib (n - 1) + fib (n - 2) for i = 1 to 10 do printfn "Fibonacci %d: %d" i (fib i) let rec fac x = match x with | x when x < 1 -> 1 | x -> x*fac(x-1) Console.WriteLine(fac(5)) //Lists //Creating lists let list1 = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] //[] the last element should be empty let list2 = 1::2::3::4::5::6::7::8::9::10::[] Console.WriteLine (list1) Console.WriteLine(list2) //The number of elements Console.WriteLine(List.length list1) //The sum of the elements in the list Console.WriteLine(List.sum list1) //prints out the head = first element Console.WriteLine(List.head list1) //prints out the tail = the rest of the elements Console.WriteLine(List.tail [1;2;3;4]) //Create list with range let list3 = [1..2..10] Console.WriteLine(list3) //Yield let numbersNear x = [ yield x-1 yield x yield x+1 ] Console.WriteLine(numbersNear 4) //do yield can be used in 2 ways //1)using do yield let list4 = [ for a in 1 .. 10 do yield (a * a) ] Console.WriteLine(list4) //2) using -> let list5 = [for x in 1 .. 10 -> x*2 ] Console.WriteLine(list5) let list = [for a in 1 .. 5 do match a with | 3 -> yield! ["p"; "c"; "l"] | _ -> yield a.ToString () ] Console.WriteLine(list)
let list1 = [5; 10; 15; 20; 25; 30] let list2 = [50; 100; 150; 200; 250; 300] (* Problem 1 *) let largestFunc x y = if x > y then x else y let rec maxIntFunc list = match list with |[] -> 0 |x::xs -> largestFunc x (maxIntFunc xs) let maxInt = maxIntFunc list1 printfn "Problem 1: %d" maxInt (* Problem 2 *) let rec nElementFunc list n = match list with |[] -> 0 |x::xs -> if n = 0 then x else nElementFunc xs (n-1) let nElement = nElementFunc list1 4 printfn "Problem 2: %d" nElement (* Problem 3 *) let rec doubleElementFunc list = match list with |[] -> [] |x::xs -> 2*x::doubleElementFunc xs let doubleList = doubleElementFunc list1 printf "Problem 3: " for x in doubleList do printf "%A " x printfn "" (*Problem 4*) let rec inListFunc list n = match list with |[] -> -1 |x::xs -> if x = n then 1 else inListFunc xs n let inList = inListFunc list1 10 printfn "Problem 4: %d" inList (*Problem 5*) let joinInt n list = match list with |[] -> (n,0) |x::xs -> (n, x) let rec jointListFunc listOne listTwo = match listOne with |[] -> [] |x::xs -> (joinInt x listTwo) :: (jointListFunc xs listTwo) let jointList = jointListFunc list1 list2 printfn "Problem 5: %A" jointList
//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