Przeglądaj źródła

allow overriding append.

by putting it back into the Seq type class. the same would be possible
for all those functions defined on Seq, but i don't want to put that
many things in there. later, maybe?

(one way to make all those functions overridable is by making a type
class for each of them and adding a default instance that does what they
currently do. is there an extension for these "extendable" functions?
there probably is.)
Lucas Stadler 11 lat temu
rodzic
commit
f1efe42dc8
1 zmienionych plików z 8 dodań i 6 usunięć
  1. 8 6
      hs/DataStructures.hs

+ 8 - 6
hs/DataStructures.hs

@ -32,6 +32,14 @@ class Seq s where
32 32
33 33
    isEmpty  :: s a -> Bool
34 34
35
    -- additional interfaces
36
37
    append :: (Seq s) => a -> s a -> s a
38
    append x s | isEmpty s = cons x nil
39
    append x s =
40
        case first s of
41
             Just x -> cons x $ append x (rest s)
42
35 43
fromList :: (Seq s) => [a] -> s a
36 44
fromList [] = nil
37 45
fromList (x:xs) = cons x $ fromList xs
@ -60,12 +68,6 @@ take n s =
60 68
        Nothing -> nil
61 69
        Just x -> cons x $ take (n-1) (rest s)
62 70
63
append :: (Seq s) => a -> s a -> s a
64
append x s | isEmpty s = cons x nil
65
append x s =
66
    case first s of
67
        Just x -> cons x $ append x (rest s)
68
69 71
concat :: (Seq s) => s a -> s a -> s a
70 72
concat l r | isEmpty l = r
71 73
concat l r | isEmpty r = l