88 lines
3.1 KiB
Haskell
88 lines
3.1 KiB
Haskell
module Angabe1 where
|
|
|
|
{- 1. Vervollstaendigen Sie gemaess Angabentext!
|
|
2. Vervollständigen Sie auch die vorgegebenen Kommentaranfänge!
|
|
3. Loeschen Sie keine Deklarationen aus diesem Rahmenprogramm, auch nicht die Modulanweisug!
|
|
4. Achten Sie darauf, dass `Gruppe' Leserechte fuer Ihre Abgabedatei hat!
|
|
-}
|
|
|
|
|
|
type Nat0 = Int
|
|
type Zeichenreihe = String
|
|
type Teilzeichenreihe = String
|
|
type IstTeilzeichenreihe = Bool
|
|
type Zerlegungszeuge = (Zeichenreihe,Zeichenreihe,Zeichenreihe)
|
|
type Zerlegungszeugen = [Zerlegungszeuge]
|
|
|
|
|
|
-- Aufgabe A.1
|
|
{-
|
|
Solution:
|
|
- if both strings are the same, True
|
|
- if the second string is empty, True
|
|
- if the second string is longer than the first, False
|
|
- otherwise, compare the first characters of both strings and call the function recursively again.
|
|
If the second string is contained in the first string, it will get equal to [] eventually and the function will return True.
|
|
If it is not, the first string will get smaller than the second eventually and the function will return False
|
|
-}
|
|
|
|
ist_tzr :: Zeichenreihe -> Teilzeichenreihe -> IstTeilzeichenreihe
|
|
|
|
ist_tzr z t
|
|
| t == z = True
|
|
| t == [] = True
|
|
| length t > length z = False
|
|
| otherwise =
|
|
if head z == head t then
|
|
ist_tzr (tail z) (tail t)
|
|
else
|
|
ist_tzr (tail z) t
|
|
|
|
-- Aufgabe A.2
|
|
{-
|
|
Solution:
|
|
- the base cases are as follows:
|
|
- both strings are empty, return a triple with empty strings
|
|
- the second string is empty, return a triple with empty strings as first two elements and the whole first string as a third.
|
|
The problem description states that the split doesn't matter, so we take this because it is easy and cheap to do :)
|
|
- the second string is not a subset of the first string, return the "error triple"
|
|
- for handling the other cases:
|
|
- we find the position of the last subset string in the first string
|
|
- we take all of the characters until this position - this is the first element of the triple
|
|
- we then take all of the characters after this position summed with the length of the second string - this is the third element of the triple
|
|
-}
|
|
|
|
tzr_zeuge :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeuge
|
|
tzr_zeuge [] [] = ("", "", "")
|
|
tzr_zeuge z [] = ("", "", z)
|
|
tzr_zeuge z t
|
|
| not (ist_tzr z t) = ("", t ++ t, "")
|
|
| otherwise =
|
|
let pos = (find_tzr_position z t 0) - 1
|
|
pre = take (pos) z
|
|
suf = drop (pos + (length t)) z
|
|
in (pre, t, suf)
|
|
|
|
-- find the index of the last occurrence of t in z
|
|
find_tzr_position :: Zeichenreihe -> Teilzeichenreihe -> Nat0 -> Nat0
|
|
find_tzr_position (z:zs) t n
|
|
| not (ist_tzr (z:zs) t) = n
|
|
| otherwise = find_tzr_position zs t (n + 1)
|
|
|
|
-- Aufgabe A.3
|
|
|
|
--tzr_zeugen :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeugen
|
|
|
|
{- Knapp, aber gut nachvollziehbar geht tzr_zeugen folgendermassen vor:
|
|
...
|
|
-}
|
|
|
|
|
|
-- Aufgabe A.4
|
|
|
|
--wieOft :: Zeichenreihe -> Teilzeichenreihe -> Nat0
|
|
|
|
{- Knapp, aber gut nachvollziehbar geht wieOft folgendermassen vor:
|
|
...
|
|
-}
|