Ministry of higher and secondary public education of the republic of uzbekistan academic lyceum at the uzbek state university of world languages


Download 224.95 Kb.
bet4/9
Sana27.02.2023
Hajmi224.95 Kb.
#1234078
1   2   3   4   5   6   7   8   9
Bog'liq
ARRAYS

let compute_v1 = function
| Some x -> "The value is: " ^ string_of_int x
| None -> "No value"


(* This function uses the built-in `fold` function *)
let compute_v2 =
Option.fold ~none:"No value" ~some:(fun x -> "The value is: " ^ string_of_int x)


let () =
(* Define variables that are `option`s of type `int` *)
let full = Some 42 in
let empty = None in


(* compute_v1 full -> The value is: 42 *)
print_endline ("compute_v1 full -> " ^ compute_v1 full);


(* compute_v1 empty -> No value *)
print_endline ("compute_v1 empty -> " ^ compute_v1 empty);


(* compute_v2 full -> The value is: 42 *)
print_endline ("compute_v2 full -> " ^ compute_v2 full);


(* compute_v2 empty -> No value *)
print_endline ("compute_v2 empty -> " ^ compute_v2 empty)

F#[edit]


// This function uses pattern matching to deconstruct `option`s
let compute_v1 = function
| Some x -> sprintf "The value is: %d" x
| None -> "No value"


// This function uses the built-in `fold` function
let compute_v2 =
Option.fold (fun _ x -> sprintf "The value is: %d" x) "No value"


// Define variables that are `option`s of type `int`
let full = Some 42
let empty = None


// compute_v1 full -> The value is: 42
compute_v1 full |> printfn "compute_v1 full -> %s"


// compute_v1 empty -> No value
compute_v1 empty |> printfn "compute_v1 empty -> %s"


// compute_v2 full -> The value is: 42
compute_v2 full |> printfn "compute_v2 full -> %s"


// compute_v2 empty -> No value
compute_v2 empty |> printfn "compute_v2 empty -> %s"

Haskell[edit]


-- This function uses pattern matching to deconstruct `Maybe`s
computeV1 :: Maybe Int -> String
computeV1 (Just x) = "The value is: " ++ show x
computeV1 Nothing = "No value"


-- This function uses the built-in `foldl` function
computeV2 :: Maybe Int -> String
computeV2 = foldl (\_ x -> "The value is: " ++ show x) "No value"

main :: IO ()


main = do
-- Define variables that are `Maybe`s of type `Int`

Download 224.95 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling