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.
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
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more