@@ -148,19 +148,39 @@ module NonEmptyList =
148148 let zipShortest ( list1 : NonEmptyList < 'T >) ( list2 : NonEmptyList < 'U >) =
149149 { Head = ( list1.Head, list2.Head); Tail = List.zipShortest list1.Tail list2.Tail }
150150
151- /// Returns a new NonEmptyList with the element added to the beginning.
152- let cons e { Head = x ; Tail = xs } = { Head = e ; Tail = x:: xs}
151+ /// <summary>Adds an element to the beginning of the given list</summary>
152+ /// <param name="value">The element to add</param>
153+ /// <param name="list">The list to add to</param>
154+ /// <returns>A new list with the element added to the beginning.</returns>
155+ let cons e { Head = x ; Tail = xs } = { Head = e ; Tail = x:: xs }
156+
157+ /// <summary>Splits the list in head and tail.</summary>
158+ /// <param name="list">The input list.</param>
159+ /// <returns>A tuple with the head and the tail of the original list.</returns>
160+ /// <exception cref="T:System.ArgumentException">Thrown when the input list tail is empty.</exception>
161+ let uncons ({ Head = x ; Tail = xs } as list ) =
162+ match xs with
163+ | [] -> invalidArg ( nameof( list)) " The input sequence has an empty tail"
164+ | _ -> x, ofList xs
165+
166+ /// <summary>Splits the list in head and tail.</summary>
167+ /// <param name="list">The input list.</param>
168+ /// <returns>A tuple with the head and the tail of the original list.</returns>
169+ let unconsAsList ({ Head = x ; Tail = xs } as list ) = x, xs
153170
154171 /// Returns the first element of a new non empty list. You can also use property nel.Head.
155172 let head { Head = x ; Tail = _ } = x
156173
157174 /// <summary>Returns a new NonEmptyList of the elements trailing the first element.</summary>
158175 /// <exception cref="System.ArgumentException">Thrown when the tail is empty.</exception>
159176 /// <remarks>Throws exception for empty tail</remarks>
160- let tail { Head = _ ; Tail = xs } = ofList xs
177+ let tail ({ Head = _ ; Tail = xs } as list ) =
178+ match xs with
179+ | [] -> invalidArg ( nameof( list)) " The input sequence has an empty tail"
180+ | _ -> ofList xs
161181
162182 /// <summary>Returns a new NonEmptyList of the elements trailing the first element or None.</summary>
163- let tryTail { Head = _ ; Tail = xs } = tryOfList xs
183+ let tryTail { Head = _ ; Tail = xs } = tryOfList xs
164184 let rec tails s =
165185 let { Tail = xs } = s
166186 match xs with
0 commit comments