Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.03 KB | None | 0 0
  1. module SimpleLinkedList
  2.  
  3. type Node(value, nextNode: Node option) =
  4.     member this.Value = value
  5.     member this.Next = nextNode
  6.  
  7. let nil = None
  8.  
  9. let isNil (x: Node option) = x = None
  10.  
  11. let create (x: int) (n: Node option) = Node(x, n) |> Some
  12.  
  13. let rec add (x: int) (n: Node option) =
  14.     match n with
  15.     | None -> Node(x, None) |> Some
  16.     | Some(node) ->
  17.         match node.Next with
  18.         | None -> create node.Value (Node(x, None) |> Some)
  19.         | Some(next) -> create node.Value (add x (Some next))
  20.  
  21. let next (x: Node option) =
  22.     match x with
  23.     | None -> None
  24.     | Some(x) -> x.Next
  25.  
  26. let datum (x: Node option) =
  27.     match x with
  28.     | None -> failwith "x is not a valid node"
  29.     | Some(x) -> x.Value
  30.  
  31. let rec toList (x: Node option) =
  32.     match x with
  33.     | None -> []
  34.     | Some(x) -> List.append [x.Value] (toList x.Next)
  35.  
  36. let rec reverse (x: Node option) =
  37.     match x with
  38.     | None -> x
  39.     | Some(x) -> add x.Value (reverse x.Next)
  40.  
  41. let fromList xs = List.fold (fun x n -> add n x) None xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement