Skip to content

Instantly share code, notes, and snippets.

@grapefroot
Created March 7, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grapefroot/7af496c4cd3049788d50 to your computer and use it in GitHub Desktop.
Save grapefroot/7af496c4cd3049788d50 to your computer and use it in GitHub Desktop.
f# practical
//let operationMaker operation a b =
// operation a b
//
//
//let rec loop i acc1 acc2 =
// match i with
// |0 |1 -> (acc1, acc2)
// |_ -> loop (i-1) (acc2) (acc1 + acc2)
//
//
//let compute n = snd(loop n 1 1)
//
//
//let rec get xs n =
// if(n = 0) then List.head xs
// else get (List.tail xs) (n-1)
//
//let rec product xs acc =
// if(xs = []) then acc
// else product xs.Tail ((xs.Head)*acc)
//
//let answer = product [3; 4; 5]
//
//
//let rec rem a b =
// if a < b then a
// else rem (a-b) b
//
//let prod xs = List.reduce (*) xs
//
//printf "%i" (prod [1;1;1])
//
//let rec gcd a b =
// match (a,b) with
// | _ when a > b -> gcd(a-b)b
// | _ when a < b -> gcd(b-a)a
// | _ -> a
//
//let explode (s:string)=
// [for ch in s -> ch]
//
//let implode (xs:'a list)=
// let sb = System.Text.StringBuilder(xs.Length)
// xs |> List.iter(sb.Append >> ignore)
// sb.ToString()
//
//let rec zip xs ys=
// match (xs,ys) with
// |(_,[])|([],_)
//
type MyList<'a> =
|Empty
|Elem of 'a * MyList<'a>
let myHead list =
match list with
|Empty->invalidArg "list" "list..."
|Elem(head, tail)->head
let r = [1.10]
let a = [for i in r do yield i*i]
let e = [for i in r -> i*i]
let tail list=
match list with
|(h::t)->t
|_->[]
let rec nth n list =
match n with
|0->List.head list
|_->nth (n-1) (List.tail list)
let sum list =
let rec sum_ acc list=
match list with
|[]->acc
|[h::t]->sum_ (acc+h) (List.tail list)
sum_ 0 list
let rev list=
let rec rev_ l r=
match l with
|[] -> r
|(x::xs) -> rev_ xs (x::r)
rev_ list []
//
//let map f list =
// match list with
// |(h::t)-> f h::map f t
// |_ -> []
let list = [1;7;13]
let list2 = List.map (fun x->x+3) list
let filtered = List.filter (fun x -> x % 2=0) list
let l = List.map2 (fun x y -> x*y) [1;5;2] [3;7;4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment