![]() |
Edit |
![]() |
|
![]() |
Recent Changes |
![]() |
Subscriptions |
![]() |
Lost and Found |
![]() |
Find References |
![]() |
Rename |
| Search |
![]() |
List all versions |
Some F# Samples by Michael Hartmann
Finding the maximal value in a list of integers coded "C# style"
open List
open Printf
let data = [1; 2; 3; 8; 6; 5]
let rec list_max plist =
if length plist < 1 then failwith "empty list" else // empty list: fail
if length plist = 1 then (List.hd plist) else // single element list: return the element
max (List.hd plist)(list_max (List.tl plist)) // multiple element list: recursive case
do printf "result = %d\n" (list_max data)
Finding the maximal value in a list of integers coded "F# style"
open List
open Printf
let data = [1; 2; 3; 3; 4; 5]
let rec max_of_list plist =
match plist with
| [] -> failwith "empty list" // empty list: fail
| [x] -> x // single element list: return the element
| x::tail -> max x (max_of_list tail) // multiple element list: recursive case
do printf "result = %d\n" (max_of_list data)
Applying a function twice
open List open Printf let double z = 2 * z let apply f y = f(y) let applytwice f y = f(f(y)) do printf "result = %d\n" (apply double 5) do printf "result = %d\n" (applytwice double 5) do printf "result = %d\n" (apply double (apply double 5))
Some simple array manipulations, using the MLLib Array module
open Printf // just some declarations for arrays // creates an F# matrix ... let a = Array.create_matrix 4 4 6;; // ... same but with explicit type declaration for the return value let (b : int array array) = Array.create_matrix 4 4 6;; // One way to print first vector of matrix a do Array.map (fun i -> printf "%d\n" i) a.(1) do printf "next\n" // print element 1,2 of matrix a do printf "a[1,2] = %d\n" (string_of_int a.(1).(2)) do printf "done"
NCLEX Review | How to Save a Relationship | Saving a Relationship | Save the Marriage | Win Back Ex | Get Him Back
![]() |
| This site supports the new NoFollow anti-spam initiative. |
Recent Topics