on master help sign in
Make all code examples typecheck
8d486a7David Raymond Christiansen
libs/prelude/Prelude/List.idr+71−35
@@ -341,13 +341,15 @@ reverse = reverse' []
341
341
342||| Insert some separator between the elements of a list.
342||| Insert some separator between the elements of a list.
343|||
343|||
344−||| > intersperse ',' (unpack "abcde") == unpack "a,b,c,d,e"
344+||| ````idris example
345+||| with List (intersperse ',' ['a', 'b', 'c', 'd', 'e'])
346+||| ````
345|||
347|||
346intersperse : a -> List a -> List a
348intersperse : a -> List a -> List a
347intersperse sep [] = []
349intersperse sep [] = []
348intersperse sep (x::xs) = x :: intersperse' sep xs
350intersperse sep (x::xs) = x :: intersperse' sep xs
349 where
351 where
350−-- intersperse' : a -> List a -> List a
352+ intersperse' : a -> List a -> List a
351 intersperse' sep [] = []
353 intersperse' sep [] = []
352 intersperse' sep (y::ys) = sep :: y :: intersperse' sep ys
354 intersperse' sep (y::ys) = sep :: y :: intersperse' sep ys
353
355
@@ -356,13 +358,15 @@ intercalate sep l = concat $ intersperse sep l
356
358
357||| Transposes rows and columns of a list of lists.
359||| Transposes rows and columns of a list of lists.
358|||
360|||
361+||| ```idris example
359−||| > transpose [[1, 2], [3, 4]] = [[1, 3], [2, 4]]
362+||| with List transpose [[1, 2], [3, 4]]
363+||| ```
360|||
364|||
361||| This also works for non square scenarios, thus
365||| This also works for non square scenarios, thus
362||| involution does not always hold:
366||| involution does not always hold:
363|||
367|||
364−||| > transpose [[], [1, 2]] = [[1], [2]]
368+||| transpose [[], [1, 2]] = [[1], [2]]
365−||| > transpose (transpose [[], [1, 2]]) = [[1, 2]]
369+||| transpose (transpose [[], [1, 2]]) = [[1, 2]]
366|||
370|||
367||| TODO: Solution which satisfies the totality checker?
371||| TODO: Solution which satisfies the totality checker?
368%assert_total
372%assert_total
@@ -473,7 +477,9 @@ elemIndices = elemIndicesBy (==)
473
477
474||| filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; e.g.,
478||| filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; e.g.,
475|||
479|||
480+||| ````idris example
476−||| > filter (<3) [0..5] == [0,1,2]
481+||| filter (< 3) [Z, S Z, S (S Z), S (S (S Z)), S (S (S (S Z)))]
482+||| ````
477|||
483|||
478filter : (a -> Bool) -> List a -> List a
484filter : (a -> Bool) -> List a -> List a
479filter p [] = []
485filter p [] = []
@@ -495,10 +501,14 @@ nubBy = nubBy' []
495 else
501 else
496 x :: nubBy' (x::acc) p xs
502 x :: nubBy' (x::acc) p xs
497
503
504+||| O(n^2). The nub function removes duplicate elements from a list. In
498−||| O(n^2). The nub function removes duplicate elements from a list. In particular, it keeps only the first occurrence of each element. It is a special case of nubBy, which allows the programmer to supply their own equality test.
505+||| particular, it keeps only the first occurrence of each element. It is a
499−|||
506+||| special case of nubBy, which allows the programmer to supply their own
500−||| > nub (the (List _) [1,2,1,3]) == [1,2,3]
507+||| equality test.
501|||
508|||
509+||| ```idris example
510+||| nub (the (List _) [1,2,1,3])
511+||| ```
502nub : Eq a => List a -> List a
512nub : Eq a => List a -> List a
503nub = nubBy (==)
513nub = nubBy (==)
504
514
@@ -507,18 +517,25 @@ deleteBy : (a -> a -> Bool) -> a -> List a -> List a
507deleteBy _ _ [] = []
517deleteBy _ _ [] = []
508deleteBy eq x (y::ys) = if x `eq` y then ys else y :: deleteBy eq x ys
518deleteBy eq x (y::ys) = if x `eq` y then ys else y :: deleteBy eq x ys
509
519
510−||| delete x removes the first occurrence of x from its list argument. For example,
520+||| `delete x` removes the first occurrence of `x` from its list argument. For
521+||| example,
511|||
522|||
512−||| > delete 'a' (unpack "banana") == unpack "bnana"
523+|||````idris example
524+|||delete 'a' ['b', 'a', 'n', 'a', 'n', 'a']
525+|||````
513|||
526|||
514−||| It is a special case of deleteBy, which allows the programmer to supply their own equality test.
527+||| It is a special case of deleteBy, which allows the programmer to supply
528+||| their own equality test.
515delete : (Eq a) => a -> List a -> List a
529delete : (Eq a) => a -> List a -> List a
516delete = deleteBy (==)
530delete = deleteBy (==)
517
531
518−||| The \\ function is list difference (non-associative). In the result of xs \\ ys, the first occurrence of each element of ys in turn (if any) has been removed from xs, e.g.,
532+||| The `\\` function is list difference (non-associative). In the result of
519−|||
533+||| `xs \\ ys`, the first occurrence of each element of ys in turn (if any) has
520−||| > (([1,2] ++ [2,3]) \\ [1,2]) == [2,3]
534+||| been removed from `xs`, e.g.,
521|||
535|||
536+||| ```idris example
537+||| (([1,2] ++ [2,3]) \\ [1,2])
538+||| ```
522(\\) : (Eq a) => List a -> List a -> List a
539(\\) : (Eq a) => List a -> List a -> List a
523(\\) = foldl (flip delete)
540(\\) = foldl (flip delete)
524
541
@@ -527,7 +544,9 @@ unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
527
544
528||| The union function returns the list union of the two lists. For example,
545||| The union function returns the list union of the two lists. For example,
529|||
546|||
530−||| > union (unpack "dog") (unpack "cow") == unpack "dogcw"
547+||| ```idris example
548+||| union ['d', 'o', 'g'] ['c', 'o', 'w']
549+||| ```
531|||
550|||
532union : (Eq a) => List a -> List a -> List a
551union : (Eq a) => List a -> List a -> List a
533union = unionBy (==)
552union = unionBy (==)
@@ -539,8 +558,9 @@ union = unionBy (==)
539||| Given a list and a predicate, returns a pair consisting of the longest
558||| Given a list and a predicate, returns a pair consisting of the longest
540||| prefix of the list that satisfies a predicate and the rest of the list.
559||| prefix of the list that satisfies a predicate and the rest of the list.
541|||
560|||
561+||| ```idris example
542−||| > span (<3) [1,2,3,2,1] == the ((List _, List _)) ([1, 2], [3, 2, 1])
562+||| span (<3) [1,2,3,2,1]
543−|||
563+||| ```
544span : (a -> Bool) -> List a -> (List a, List a)
564span : (a -> Bool) -> List a -> (List a, List a)
545span p [] = ([], [])
565span p [] = ([], [])
546span p (x::xs) =
566span p (x::xs) =
@@ -551,17 +571,20 @@ span p (x::xs) =
551 ([], x::xs)
571 ([], x::xs)
552
572
553||| Given a list and a predicate, returns a pair consisting of the longest
573||| Given a list and a predicate, returns a pair consisting of the longest
554−||| prefix of the list that does not satisfy a predicate and the rest of the list.
574+||| prefix of the list that does not satisfy a predicate and the rest of the
555−|||
575+||| list.
556−||| > break (>=3) [1,2,3,2,1] == the ((List _, List _)) ([1, 2], [3, 2, 1])
557|||
576|||
577+||| ```idris example
578+||| break (>=3) [1,2,3,2,1]
579+||| ```
558break : (a -> Bool) -> List a -> (List a, List a)
580break : (a -> Bool) -> List a -> (List a, List a)
559break p = span (not . p)
581break p = span (not . p)
560
582
561||| Split on any elements that satisfy the given predicate.
583||| Split on any elements that satisfy the given predicate.
562|||
584|||
585+||| ```idris example
563−||| > split (<2) [2,0,3,1,4] == [[2], [3], [4]]
586+||| split (<2) [2,0,3,1,4]
564−|||
587+||| ```
565split : (a -> Bool) -> List a -> List (List a)
588split : (a -> Bool) -> List a -> List (List a)
566split p xs =
589split p xs =
567 case break p xs of
590 case break p xs of
@@ -578,8 +601,9 @@ splitAt n xs = (take n xs, drop n xs)
578
601
579||| The partition function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; e.g.,
602||| The partition function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively; e.g.,
580|||
603|||
604+||| ```idris example
581−||| > partition (<3) [0..5] == (the (List Integer) [0,1,2], the (List Integer) [3,4,5])
605+||| partition (<3) [0, 1, 2, 3, 4, 5]
582−|||
606+||| ```
583partition : (a -> Bool) -> List a -> (List a, List a)
607partition : (a -> Bool) -> List a -> (List a, List a)
584partition p [] = ([], [])
608partition p [] = ([], [])
585partition p (x::xs) =
609partition p (x::xs) =
@@ -589,19 +613,23 @@ partition p (x::xs) =
589 else
613 else
590 (lefts, x::rights)
614 (lefts, x::rights)
591
615
592−||| The inits function returns all initial segments of the argument, shortest first. For example,
616+||| The inits function returns all initial segments of the argument, shortest
593−|||
617+||| first. For example,
594−||| > inits [1,2,3] == [[],[1],[1,2],[1,2,3]]
595|||
618|||
619+||| ```idris example
620+||| inits [1,2,3]
621+||| ```
596inits : List a -> List (List a)
622inits : List a -> List (List a)
597inits xs = [] :: case xs of
623inits xs = [] :: case xs of
598 [] => []
624 [] => []
599 x :: xs' => map (x ::) (inits xs')
625 x :: xs' => map (x ::) (inits xs')
600
626
601−||| The tails function returns all final segments of the argument, longest first. For example,
627+||| The tails function returns all final segments of the argument, longest
602−|||
628+||| first. For example,
603−||| > tails [1,2,3] == [[1,2,3], [2,3], [3], []]
604|||
629|||
630+||| ```idris example
631+||| tails [1,2,3] == [[1,2,3], [2,3], [3], []]
632+|||```
605tails : List a -> List (List a)
633tails : List a -> List (List a)
606tails xs = xs :: case xs of
634tails xs = xs :: case xs of
607 [] => []
635 [] => []
@@ -609,14 +637,18 @@ tails xs = xs :: case xs of
609
637
610||| Split on the given element.
638||| Split on the given element.
611|||
639|||
640+||| ```idris example
612−||| > splitOn 0 [1,0,2,0,0,3] == [[1],[2],[],[3]]
641+||| splitOn 0 [1,0,2,0,0,3]
642+||| ```
613|||
643|||
614splitOn : Eq a => a -> List a -> List (List a)
644splitOn : Eq a => a -> List a -> List (List a)
615splitOn a = split (== a)
645splitOn a = split (== a)
616
646
617||| Replaces all occurences of the first argument with the second argument in a list.
647||| Replaces all occurences of the first argument with the second argument in a list.
618|||
648|||
649+||| ```idris example
619−||| > replaceOn '-' ',' (unpack "1-2-3") == unpack "1,2,3"
650+||| replaceOn '-' ',' ['1', '-', '2', '-', '3']
651+||| ```
620|||
652|||
621replaceOn : Eq a => a -> a -> List a -> List a
653replaceOn : Eq a => a -> a -> List a -> List a
622replaceOn a b l = map (\c => if c == a then b else c) l
654replaceOn a b l = map (\c => if c == a then b else c) l
@@ -647,8 +679,12 @@ isSuffixOf = isSuffixOfBy (==)
647
679
648||| The isInfixOf function takes two lists and returns True iff the first list is contained, wholly and intact, anywhere within the second.
680||| The isInfixOf function takes two lists and returns True iff the first list is contained, wholly and intact, anywhere within the second.
649|||
681|||
650−||| > isInfixOf "Idris" "I really like Idris." == True
682+||| ```idris example
651−||| > isInfixOf "Ial" "I really like Idris." == False
683+||| isInfixOf ['b','c'] ['a', 'b', 'c', 'd']
684+||| ```
685+||| ```idris example
686+||| isInfixOf ['b','d'] ['a', 'b', 'c', 'd']
687+||| ```
652|||
688|||
653isInfixOf : Eq a => List a -> List a -> Bool
689isInfixOf : Eq a => List a -> List a -> Bool
654isInfixOf n h = any (isPrefixOf n) (tails h)
690isInfixOf n h = any (isPrefixOf n) (tails h)