@@ -637,135 +637,135 @@ The @deftech{list} type, which describes lazy linked lists. Since a list is lazy
637
637
as long as the entire list is never demanded. The @racket[::] constructor is pronounced “cons”, and it
638
638
is generally intended to be used infix.}
639
639
640
- @defthing[ head (t:forall [a] { (t:List a) t:-> (t:Maybe a)} )]{
640
+ @defproc[( head [xs (t:List a)]) (t:Maybe a)]{
641
641
642
- Returns @racket[Just] the first element of a list , or @racket[Nothing] if the list is @racket[Nil].
642
+ Returns @racket[Just] the first element of @racket[xs] , or @racket[Nothing] if @racket[xs] is @racket[Nil].
643
643
644
644
@(hackett-examples
645
645
(head {1 :: 2 :: 3 :: Nil})
646
646
(head (: Nil (t:List t:Integer))))}
647
647
648
- @defthing[ last (forall [a] {( List a) -> ( Maybe a)} )]{
648
+ @defproc[( last [xs (t: List a)]) (t: Maybe a)]{
649
649
650
- Returns @racket[just ] the last element of a list , or @racket[nothing ] if the list is @racket[nil ].
650
+ Returns @racket[Just ] the last element of @racket[xs] , or @racket[Nothing ] if @racket[xs] is @racket[Nil ].
651
651
This function is @tech[#:key "partial function " ]{partial}, since it diverges on an infinitely long
652
652
input, e.g. @racket[(letrec ([ones {1 :: ones}]) (last ones))].
653
653
654
654
@(hackett-examples
655
- (last {1 :: 2 :: 3 :: nil })
656
- (last (: nil ( List Integer))))}
655
+ (last {1 :: 2 :: 3 :: Nil })
656
+ (last (: Nil (t: List t: Integer))))}
657
657
658
- @defthing[ tail (forall [a] {( List a) -> ( Maybe (List a))} )]{
658
+ @defproc[( tail [xs (t: List a)]) (t: Maybe (t: List a))]{
659
659
660
- Returns @racket[Just] a list without its first element, or @racket[Nothing] if the list is
660
+ Returns @racket[Just] @racket[xs] without its first element, or @racket[Nothing] if @racket[xs] is
661
661
@racket[Nil].
662
662
663
663
@(hackett-examples
664
664
(tail {1 :: 2 :: 3 :: Nil})
665
665
(tail (: Nil (t:List t:Integer))))}
666
666
667
- @defthing[ init (forall [a] {( List a) -> ( Maybe a)} )]{
667
+ @defproc[( init [xs (t: List a)]) (t: Maybe a)]{
668
668
669
- Returns @racket[just] a list without its the last element, or @racket[nothing ] if the list is
670
- @racket[nil ]. This function is @tech[#:key "partial function " ]{partial}, since it diverges on an
671
- infinitely long input, e.g. @racket[(letrec ([ones {1 :: ones}]) (last ones))].
669
+ Returns @racket[Just] @racket[xs] without its the last element, or @racket[Nothing ] if @racket[xs] is
670
+ @racket[Nil ]. This function is @tech[#:key "partial function " ]{partial}, since it diverges on an
671
+ infinitely long input, e.g. @racket[(letrec ([ones {1 :: ones}]) (init ones))].
672
672
673
673
@(hackett-examples
674
- (init {1 :: 2 :: 3 :: nil })
675
- (init (: nil ( List Integer))))}
674
+ (init {1 :: 2 :: 3 :: Nil })
675
+ (init (: Nil (t: List t: Integer))))}
676
676
677
- @defthing[ head! (forall [a] { (List a) -> a}) ]{
677
+ @defproc[( head! [xs (List a)]) a ]{
678
678
679
- A @tech[#:key "partial function " ]{partial} version of @racket[head] that returns the first element of
680
- a list . If the list is empty, it raises an error.
679
+ A @tech[#:key "partial function " ]{partial} version of @racket[head] which returns the first element of
680
+ @racket[xs] . If @racket[xs] is empty, @racket[head!] raises an error.
681
681
682
682
@(hackett-examples
683
683
(head! {1 :: 2 :: 3 :: Nil})
684
684
(eval:error (head! (: Nil (t:List t:Integer)))))}
685
685
686
- @defthing[ last! (forall [a] {( List a) -> a}) ]{
686
+ @defproc[( last! [xs (t: List a)]) a ]{
687
687
688
- A less-safe version of @racket[last] which tries to return the last element of any list . This function
689
- is @tech[#:key "partial function " ]{partial}; if the given list is empty, @racket[last!] raises an
690
- error, and if the list is infinitely long, the function wil not return.
688
+ A less-safe version of @racket[last] which returns the last element of @racket[xs] . This function
689
+ is @tech[#:key "partial function " ]{partial}, since when @racket[xs] is empty, @racket[last!] raises
690
+ an error, and if @racket[xs] is infinitely long, @racket[last!] will never return.
691
691
692
692
@(hackett-examples
693
- (last! {1 :: 2 :: 3 :: nil })
694
- (eval:error (last! (: nil ( List Integer)))))}
693
+ (last! {1 :: 2 :: 3 :: Nil })
694
+ (eval:error (last! (: Nil (t: List t: Integer)))))}
695
695
696
- @defthing[ tail! (forall [a] {( List a) -> ( List a)} )]{
696
+ @defproc[( tail! [xs (t: List a)]) (t: List a)]{
697
697
698
- A @tech[#:key "partial function " ]{partial} version of @racket[tail] that returns a list without its
699
- first element. If the list is empty, it raises an error.
698
+ A @tech[#:key "partial function " ]{partial} version of @racket[tail] that returns @racket[xs] without
699
+ its first element. If @racket[xs] is empty, @racket[tail!] raises an error.
700
700
701
701
@(hackett-examples
702
702
(tail! {1 :: 2 :: 3 :: Nil})
703
703
(eval:error (tail! (: Nil (t:List t:Integer)))))}
704
704
705
- @defthing[ init! (forall [a] {( List a) -> a}) ]{
705
+ @defproc[( init! [xs (t: List a)]) a ]{
706
706
707
- A less-safe version of @racket[init ] which tries to return the last element of any list . This function
708
- is @tech[#:key "partial function " ]{partial}; if the given list is empty, @racket[init!] raises an
709
- error, and if the list is infinitely long, the function wil not return.
707
+ A less-safe version of @racket[init ] which returns the last element of @racket[xs] . This function
708
+ is @tech[#:key "partial function " ]{partial}, since when @racket[xs] is empty, @racket[init!] raises an
709
+ error, and if @racket[xs] is infinitely long, @racket[init!] will never return.
710
710
711
711
@(hackett-examples
712
- (init! {1 :: 2 :: 3 :: nil })
713
- (eval:error (init! (: nil ( List Integer)))))}
712
+ (init! {1 :: 2 :: 3 :: Nil })
713
+ (eval:error (init! (: Nil (t: List t: Integer)))))}
714
714
715
- @defthing[ uncons (forall [a] {( List a) -> ( Maybe (Tuple a (List a)))} )]{
715
+ @defproc[( uncons [xs (t: List a)]) (t: Maybe (t: Tuple a (t: List a)))]{
716
716
717
- Returns @racket[nothing] if the list is @racket[nil ], and @racket[just] a pair of the list 's first
718
- element and the rest of it otherwise .
717
+ When @racket[xs] is @racket[Nil ], @racket[uncons xs] is @racket[Nothing]. Otherwise, if @racket[xs]
718
+ is @racket[{y :: ys}] then @racket[uncons xs] is @racket[Just (Tuple y ys)] .
719
719
720
720
@(hackett-examples
721
- (uncons {1 :: 2 :: 3 :: nil })
722
- (uncons (: nil ( List Integer))))}
721
+ (uncons {1 :: 2 :: 3 :: Nil })
722
+ (uncons (: Nil (t: List t: Integer))))}
723
723
724
- @defthing[ uncons! (forall [a] {( List a) -> ( Tuple a (List a))} )]{
724
+ @defproc[( uncons! [xs (t: List a)]) (t: Tuple a (t: List a))]{
725
725
726
- A @tech[#:key "partial function " ]{partial} version of @racket[uncons] that returns a pair of the
727
- list 's first element and the rest of it . If the list is empty, it raises an error.
726
+ A @tech[#:key "partial function " ]{partial} version of @racket[uncons] that returns
727
+ @racket[Tuple (head! xs) (tail! xs)] . If @racket[xs] is empty, it instead raises an error.
728
728
729
729
@(hackett-examples
730
- (uncons! {1 :: 2 :: 3 :: nil })
731
- (eval:error (uncons! (: nil ( List Integer)))))}
730
+ (uncons! {1 :: 2 :: 3 :: Nil })
731
+ (eval:error (uncons! (: Nil (t: List t: Integer)))))}
732
732
733
- @defthing[null? (forall [a] {( List a) -> Bool} )]{
733
+ @defproc[(nil? [xs (t: List a)]) (t: Bool)]{
734
734
735
- This predicate is @racket[true ] when its argument is of the form @racket[nil ], and is false otherwise.
735
+ This predicate is @racket[True ] when @racket[xs] is of the form @racket[Nil ], and is false otherwise.
736
736
737
737
@(hackett-examples
738
- (null ? {1 :: 2 :: 3 :: nil })
739
- (null ? (: nil ( List Integer))))}
738
+ (nil ? {1 :: 2 :: 3 :: Nil })
739
+ (nil ? (: Nil (t: List t: Integer))))}
740
740
741
- @defthing[ length (forall [a] {( List a) -> Integer})]
741
+ @defproc[( length [xs (t: List a)]) t: Integer]{
742
742
743
- Returns the length of a finite list . Since the function will diverge on an infinitely long input,
744
- @racket[length] is @tech[#:key "partial function " ]{partial}.
743
+ Returns the length of @racket[xs] when @racket[xs] is finite . Since the function diverges on an
744
+ infinitely long input, @racket[length] is @tech[#:key "partial function " ]{partial}.
745
745
746
746
@(hackett-examples
747
- (length {1 :: 2 :: 3 :: nil })
748
- (length (: nil ( List Integer))))}
747
+ (length {1 :: 2 :: 3 :: Nil })
748
+ (length (: Nil (t: List t: Integer))))}
749
749
750
- @defproc[(take [n Integer] [xs (List a)]) (List a)]{
750
+ @defproc[(take [n t: Integer] [xs (t: List a)]) (t: List a)]{
751
751
752
752
Produces a list with the first @racket[n] elements of @racket[xs]. If @racket[xs] contains fewer than
753
753
@racket[n] elements, @racket[xs] is returned unmodified.
754
754
755
755
@(hackett-examples
756
- (take 2 {1 :: 2 :: 3 :: nil })
757
- (take 2 {1 :: nil })
758
- (take 2 (: nil ( List Integer))))}
756
+ (take 2 {1 :: 2 :: 3 :: Nil })
757
+ (take 2 {1 :: Nil })
758
+ (take 2 (: Nil (t: List t: Integer))))}
759
759
760
760
@defproc[(drop [n t:Integer] [xs (t:List a)]) (t:List a)]{
761
761
762
762
Produces a list like @racket[xs] without its first @racket[n] elements. If @racket[xs] contains fewer
763
763
then @racket[n] elements, the result is @racket[Nil].
764
764
765
765
@(hackett-examples
766
- (drop 2 {1 :: 2 :: 3 :: nil })
767
- (drop 2 {1 :: nil })
768
- (drop 2 (: nil ( List Integer))))}
766
+ (drop 2 {1 :: 2 :: 3 :: Nil })
767
+ (drop 2 {1 :: Nil })
768
+ (drop 2 (: Nil (t: List t: Integer))))}
769
769
770
770
@defproc[(filter [f {a t:-> t:Bool}] [xs (t:List a)]) (t:List a)]{
771
771
@@ -816,13 +816,161 @@ Adds the elements of @racket[xs] together and returns the sum. Equivalent to @ra
816
816
(eval:check (sum {1 :: 2 :: 3 :: Nil}) 6 )
817
817
(eval:check (sum Nil) 0 ))}
818
818
819
- @defthing[intersperse (forall [a] {a -> (List a) -> (List a)})]{
819
+ @defproc[(reverse (xs (t:List a))) (t:List a)]{
820
+
821
+ Returns @racket[xs] in reversed order.
822
+
823
+ @(hackett-examples
824
+ (reverse {1 :: 2 :: 3 :: Nil})
825
+ (reverse (: Nil (t:List t:Integer))))}
826
+
827
+ @defproc[(zip-with [f {a t:-> b t:-> c}] [as (t:List a)] [bs (t:List b)]) (t:List c)]{
828
+
829
+ This function will apply @racket[f] to each element in @racket[as] and @racket[bs] until it
830
+ has reached the end of either, then it returns a list like
831
+ @racket[{f _a0 _b0 :: f _a1 _b1 :: f _a2 _b2 :: ... :: Nil}] (where @racket[as] contains
832
+ elements named @racket[_a0], @racket[_a1], @racket[_a2] etc., and @racket[bs] contains elements
833
+ named @racket[_b0], @racket[_b1], @racket[_b2] etc.).
834
+
835
+ @(hackett-examples
836
+ (zip-with + {1 :: 2 :: 3 :: Nil} {18 :: 42 :: 50 :: Nil})
837
+ (zip-with + {1 :: 2 :: 3 :: Nil} {18 :: 42 :: 50 :: 100 :: Nil})
838
+ (zip-with + {1 :: 2 :: 3 :: 4 :: Nil} {18 :: 42 :: 50 :: Nil})
839
+ (zip-with * {1 :: 2 :: 3 :: Nil} {18 :: 42 :: 50 :: Nil})
840
+ (zip-with + (: Nil (t:List t:Integer)) (: Nil (t:List t:Integer))))}
841
+
842
+ @defproc[(zip [as (t:List a)] [bs (t:List b)]) (t:List (t:Tuple a b))]{
843
+
844
+ Returns a list of componenetwise pairs from @racket[as] and @racket[bs]. The length of this list is
845
+ the length of the shortest input list. Equivalent to @racket[(zip-with Tuple as bs)].
846
+
847
+ @(hackett-examples
848
+ (zip {1 :: 2 :: 3 :: Nil} {18 :: 42 :: 50 :: Nil})
849
+ (zip {1 :: 2 :: 3 :: Nil} {18 :: 42 :: 50 :: 100 :: Nil})
850
+ (zip {1 :: 2 :: 3 :: 4 :: Nil} {18 :: 42 :: 50 :: Nil})
851
+ (zip (: Nil (t:List t:Integer)) (: Nil (t:List t:Integer))))}
852
+
853
+ @defproc[(repeat [x a]) (t:List a)]{
854
+
855
+ Returns an infinite list containing only @racket[x].
856
+
857
+ @(hackett-examples
858
+ (take 5 (repeat 1 )))}
859
+
860
+ @defproc[(cycle! [xs (t:List a)]) (t:List a)]{
861
+
862
+ Returns the infinite list @racket[{xs ++ xs ++ xs ++ ... }]. If @racket[xs] is infinite,
863
+ @racket[cycle! xs == xs]. This function is @tech[#:key "partial function " ]{partial},
864
+ because it errors when given @racket[Nil].
865
+
866
+ @(hackett-examples
867
+ (take 10 (cycle! {1 :: 2 :: 3 :: Nil}))
868
+ (eval:error (cycle! (: Nil (t:List t:Integer)))))}
869
+
870
+ @defproc[(or [xs (t:List t:Bool)]) t:Bool]{
871
+
872
+ Logically ors the elements of @racket[xs] together and returns the result. Equivalent to @racket[(foldr || False)].
873
+ Because it uses a right fold, the only elements which will be evaluated are those before the first expression which
874
+ evaluates to @racket[True]. Additionally, @racket[or infinite-list] can never return @racket[False], and
875
+ @racket[or (repeat False)] will never terminate.
876
+
877
+ @(hackett-examples
878
+ (or {True :: False :: Nil})
879
+ (or {False :: True :: Nil})
880
+ (or {True :: (error! "never happens " ) :: Nil})
881
+ (or Nil))}
882
+
883
+ @defproc[(and [xs (t:List t:Bool)]) t:Bool]{
884
+
885
+ Logically ands the elements of @racket[xs] together and returns the result. Equivalent to @racket[(foldr && True)].
886
+ Because it uses a right fold, the only elements which will be evaluated are those before the first expression which
887
+ evaluates to @racket[False]. Additionally, @racket[and infinite-list] can never return @racket[True], and
888
+ @racket[and (repeat True)] will never terminate.
889
+
890
+ @(hackett-examples
891
+ (and {True :: False :: Nil})
892
+ (and {False :: True :: Nil})
893
+ (and {False :: (error! "never happens " ) :: Nil})
894
+ (and Nil))}
895
+
896
+ @defproc[(any? [p {a t:-> t:Bool}] [xs (t:List t:Bool)]) t:Bool]{
897
+
898
+ Returns @racket[True] if @racket[xs] contains an element @racket[x] such that @racket[(p x)] is
899
+ @racket[True]. Equivalent to @racket[(or (map p xs))].
900
+
901
+ @(hackett-examples
902
+ (any? (<= 2 ) {1 :: 2 :: 3 :: Nil})
903
+ (any? (<= 4 ) {1 :: 2 :: 3 :: Nil})
904
+ (any? (<= 0 ) {1 :: (error! "never happens " ) :: Nil})
905
+ (any? (<= 0 ) Nil))}
906
+
907
+ @defproc[(all? [p {a t:-> t:Bool}] [xs (t:List t:Bool)]) t:Bool]{
908
+
909
+ Returns @racket[True] if for every element @racket[x] of @racket[xs] @racket[(p x)] is
910
+ @racket[True]. Equivalent to @racket[(and (map p xs))].
911
+
912
+ @(hackett-examples
913
+ (all? (<= 1 ) {1 :: 2 :: 3 :: Nil})
914
+ (all? (<= 2 ) {1 :: 2 :: 3 :: Nil})
915
+ (all? (<= 2 ) {1 :: (error! "never happens " ) :: Nil})
916
+ (all? (<= 1 ) Nil))}
917
+
918
+ @defproc[(elem? [_ (t:Eq a)] [x a] [xs (t:List a)]) t:Bool]{
919
+
920
+ Returns @racket[True] if @racket[xs] contains @racket[x]. Equivalent to @racket[(any? (== x) xs)].
921
+ Only elements to the left of the first expression equal to @racket[x] in @racket[xs] will be checked
922
+ for equality.
923
+
924
+ @(hackett-examples
925
+ (elem? 2 {1 :: 2 :: 3 :: Nil})
926
+ (elem? 0 {1 :: 2 :: 3 :: Nil})
927
+ (elem? 1 {1 :: (error! "never happens " ) :: Nil})
928
+ (elem? 1 Nil))}
929
+
930
+ @defproc[(not-elem? [_ (t:Eq a)] [x a] [xs (t:List a)]) t:Bool]{
931
+
932
+ Returns @racket[False] if @racket[xs] contains @racket[x]. Equivalent to @racket[(not (elem? x xs))].
933
+ Only elements to the left of the first expression equal to @racket[x] in @racket[xs] will be checked
934
+ for equality.
935
+
936
+ @(hackett-examples
937
+ (elem? 2 {1 :: 2 :: 3 :: Nil})
938
+ (elem? 0 {1 :: 2 :: 3 :: Nil})
939
+ (elem? 1 {1 :: (error! "never happens " ) :: Nil})
940
+ (elem? 1 Nil))}
941
+
942
+ @defproc[(delete [_ (t:Eq a)] [x a] [xs (t:List a)]) (t:List a)]{
943
+
944
+ Returns @racket[xs] with the first occurrence of @racket[x] removed. Equivalent to
945
+ @racket[(delete-by == x xs)]. Only elements to the left of the first expression equal to @racket[x]
946
+ in @racket[xs] will be checked for equality.
947
+
948
+ @(hackett-examples
949
+ (delete 2 {1 :: 2 :: 3 :: Nil})
950
+ (delete 0 {1 :: 2 :: 3 :: Nil})
951
+ (head (delete 1 {1 :: 2 :: (error! "never happens " ) :: Nil}))
952
+ (delete 1 Nil))}
953
+
954
+ @defproc[(delete-by [rel {a t:-> a t:-> t:Bool}] [x a] [xs (t:List a)]) (t:List a)]{
955
+
956
+ Finds the first element @racket[y] such that @racket[{y rel x}] and returns @racket[xs] with
957
+ @racket[y] removed. Generalizes @racket[delete]. Only elements to the left of the first expression
958
+ such @racket[y] will be checked for equality.
959
+
960
+ @(hackett-examples
961
+ (delete-by > 2 {1 :: 2 :: 3 :: Nil})
962
+ (delete-by > 0 {1 :: 2 :: 3 :: Nil})
963
+ (head (delete-by not= 1 {1 :: 2 :: (error! "never happens " ) :: Nil}))
964
+ (delete-by (λ [y x] {(remainder! y x) == 0 }) 2 Nil)
965
+ (delete-by (error! "never happens " ) (error! "never happens " ) (: Nil (t:List t:Integer))))}
966
+
967
+ @defproc[(intersperse [x a] [xs (t:List a)]) (t:List a)]{
820
968
821
969
Given a separator and a list, intersperse intersperses the separator between each element of the list.
822
970
823
971
@(hackett-examples
824
- (intersperse 42 {1 :: 2 :: 3 :: nil })
825
- (intersperse 42 nil ))}
972
+ (intersperse 42 {1 :: 2 :: 3 :: Nil })
973
+ (intersperse 42 Nil ))}
826
974
827
975
@section[#:tag "reference-typeclasses " ]{Typeclasses}
828
976
0 commit comments