Просмотр исходного кода

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
Родитель
Сommit
f1efe42dc8
1 измененных файлов с 8 добавлено и 6 удалено
  1. 8 6
      hs/DataStructures.hs

+ 8 - 6
hs/DataStructures.hs

32
32
33
    isEmpty  :: s a -> Bool
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
fromList :: (Seq s) => [a] -> s a
43
fromList :: (Seq s) => [a] -> s a
36
fromList [] = nil
44
fromList [] = nil
37
fromList (x:xs) = cons x $ fromList xs
45
fromList (x:xs) = cons x $ fromList xs
60
        Nothing -> nil
68
        Nothing -> nil
61
        Just x -> cons x $ take (n-1) (rest s)
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
concat :: (Seq s) => s a -> s a -> s a
71
concat :: (Seq s) => s a -> s a -> s a
70
concat l r | isEmpty l = r
72
concat l r | isEmpty l = r
71
concat l r | isEmpty r = l
73
concat l r | isEmpty r = l