This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
progra19/tut11/aufgabe1.hs

27 lines
875 B
Haskell
Raw Normal View History

2020-01-17 15:44:44 +00:00
data Mobile a = Stern | Seepferdchen | Elefant (Mobile a)
| Kaenguru a (Mobile a) (Mobile a) deriving Show
mobileRechts = Elefant (Kaenguru 1 (Elefant (Stern)) (Elefant (Seepferdchen)))
mobileLinks = Kaenguru 1 (Elefant
(Kaenguru 2 (Stern) (Kaenguru 3 (Seepferdchen) (Stern)))
) (Seepferdchen)
count :: Mobile a -> Int
count Stern = 1
count Seepferdchen = 1
count (Elefant x) = 1 + count x
count (Kaenguru _ y z) = 1 + count y + count z
liste :: Mobile a -> [a]
liste Stern = []
liste Seepferdchen = []
liste (Elefant x) = liste x
liste (Kaenguru x y z) = x : liste y ++ liste z
greife :: Mobile a -> Int -> Mobile a
greife x 1 = x
greife (Elefant x) i = greife x (i-1)
greife (Kaenguru _ x y) i | (i-1) <= count x = greife x (i-1)
| otherwise = greife y (i-1-(count x))