Skip to content

Instantly share code, notes, and snippets.

@charlieegan3
Last active April 23, 2016 22:40
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 charlieegan3/804e72638cca31b819dbae4c63f8e2ef to your computer and use it in GitHub Desktop.
Save charlieegan3/804e72638cca31b819dbae4c63f8e2ef to your computer and use it in GitHub Desktop.
F#: 99 questions
// question 1
// Last element of a list
exception ListEmptyError of string
let last xs =
match xs with
|[] -> raise (ListEmptyError("List is empty"))
|x -> x |> List.rev |> List.head
[<EntryPoint>]
let main argv =
[1;2;3] |> last |> printfn "%A"
0
// question 2
// Second last element of a list
exception ListShortError of string
let rec butLast xs =
match xs with
|[] -> raise (ListShortError("List is empty"))
|[x] -> raise (ListShortError("List is too short"))
|x::_::[] -> x
|_::xs -> butLast xs
[<EntryPoint>]
let main argv =
[1] |> butLast |> printfn "%A"
0
// question 3
// Element at index
let atIndex index (xs: 'a List) =
if index >= xs.Length || index < 0
then failwith "Index out of bounds"
else xs.[index]
[<EntryPoint>]
let main argv =
[0;1;2;3;4] |> atIndex 3 |> printfn "%A"
0
// question 4
// Count elements in a list
let rec myLength aList =
match aList with
| [] -> 0
| x::xs -> 1 + myLength xs
[<EntryPoint>]
let main argv =
[1] |> myLength |> printfn "%A"
0
// question 5
// Reverse a list
let myReverse list =
List.fold (fun acc elem -> elem::acc) [] list
[<EntryPoint>]
let main argv =
[1;2;3] |> myReverse |> printfn "%A"
0
// question 6
// Palindrome list
let palindrome list =
(List.rev list) = list
[<EntryPoint>]
let main argv =
[1;2;3;2;1] |> palindrome |> printfn "%A"
0
// question 7
// Flatten a nested list structure
// http://rosettacode.org/wiki/Flatten_a_list#F.23
type 'a ll =
| I of 'a
| L of 'a ll list
let rec flatten =
function
| I x -> [x]
| L x -> List.collect flatten x
[<EntryPoint>]
let main argv =
printfn "%A" (flatten (L [L([I(0)]); I(1); L([I(2); I(3)]);]));
0
// question 8
// Unique elements from a list
let distinctList list = List.distinct list
[<EntryPoint>]
let main argv =
printfn "%A" <| distinctList [1;2;3;4]
0
// quesion9
// Group adjacent elements in a list
// http://www.fssnip.net/an#Problem-9-Pack-consecutive-duplicates-of-list-elements-into-sublists
let collect list = function
|(y::xs)::xss when list = y -> (list::y::xs)::xss
|xss -> [list]::xss
let pack xs =
List.foldBack collect xs []
[<EntryPoint>]
let main argv =
printfn "%A" <| pack [1;1;2;3;3;1;1;4]
0
// question 10
// Count the size of packed groups
let collect list = function
|(y::xs)::xss when list = y -> (list::y::xs)::xss
|xss -> [list]::xss
let pack list =
List.foldBack collect list []
let countGroups list =
List.map (fun xs -> List.length xs, List.head xs) list
[<EntryPoint>]
let main argv =
pack >> countGroups >> printfn "%A" <| List.ofSeq "aabbcccd"
0
// question 11
// Flatten pairs that represent a single element
type ListItem<'a> =
| Item of 'a
| Pair of int * 'a
let pack list =
let collect list = function
|(y::xs)::xss when list = y -> (list::y::xs)::xss
|xss -> [list]::xss
List.foldBack collect list []
let explodePairs list =
let countGroups list =
List.map (fun xs -> List.length xs, List.head xs) list
let explode pair =
match pair with
|(1,x) -> Item x
|(y,x) -> Pair(y, x)
List.map explode (countGroups list)
[<EntryPoint>]
let main argv =
pack >> explodePairs >> printfn "%A" <| List.ofSeq "aabcccd"
0
// question 13
// encode a list in place
type ListItem<'a> =
| Single of 'a
| Multiple of int * 'a
let encode list =
let aggregator acc e =
match acc with
|[] -> [(1,e)]
|(x1,x2)::xs when e = x2 -> (1+x1,e)::xs
|(x1,x2)::xs -> (1,e)::(x1,x2)::xs
let typeHelper (count, x) =
match count with
|1 -> Single x
|y -> Multiple(y, x)
List.rev << List.map typeHelper <| List.fold aggregator [] list
[<EntryPoint>]
let main argv =
printfn "%A" << encode <| List.ofSeq "aaaabccaadeeee"
0
// question 14
// dupe elements in the list
let rec duplicate list =
match list with
|[] -> []
|[x] -> [x; x]
|x::xs -> [x;x] @ duplicate xs
[<EntryPoint>]
let main argv =
printfn "%A" << duplicate <| List.ofSeq "abcd"
0
// question 15
// dupe elements n times
let rec duplicate list count =
match list with
|[] -> []
|[x] -> List.replicate count x
|x::xs -> List.replicate count x @ duplicate xs count
[<EntryPoint>]
let main argv =
printfn "%A" <| duplicate [1;2;3] 5
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment