2021-10-15 14:11:06 +00:00
|
|
|
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
|
|
|
|
-}
|
|
|
|
|
2021-10-15 17:30:29 +00:00
|
|
|
ist_tzr :: Zeichenreihe -> Teilzeichenreihe -> IstTeilzeichenreihe
|
|
|
|
|
|
|
|
ist_tzr z t
|
|
|
|
| t == z = True
|
|
|
|
| t == [] = True
|
|
|
|
| length t > length z = False
|
2021-10-15 14:11:06 +00:00
|
|
|
| otherwise =
|
2021-10-15 17:30:29 +00:00
|
|
|
if head z == head t then
|
|
|
|
ist_tzr (tail z) (tail t)
|
2021-10-15 14:11:06 +00:00
|
|
|
else
|
2021-10-15 17:30:29 +00:00
|
|
|
ist_tzr (tail z) t
|
2021-10-15 14:11:06 +00:00
|
|
|
|
|
|
|
-- Aufgabe A.2
|
2021-10-15 17:30:29 +00:00
|
|
|
{-
|
|
|
|
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
|
|
|
|
-}
|
2021-10-15 14:11:06 +00:00
|
|
|
|
2021-10-15 17:30:29 +00:00
|
|
|
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
|
2021-10-15 18:03:48 +00:00
|
|
|
pre = take pos z
|
2021-10-15 17:30:29 +00:00
|
|
|
suf = drop (pos + (length t)) z
|
|
|
|
in (pre, t, suf)
|
2021-10-15 14:11:06 +00:00
|
|
|
|
2021-10-15 17:30:29 +00:00
|
|
|
-- 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)
|
2021-10-15 14:11:06 +00:00
|
|
|
|
|
|
|
-- Aufgabe A.3
|
|
|
|
{- Knapp, aber gut nachvollziehbar geht tzr_zeugen folgendermassen vor:
|
|
|
|
...
|
|
|
|
-}
|
|
|
|
|
2021-10-15 18:03:48 +00:00
|
|
|
tzr_zeugen :: Zeichenreihe -> Teilzeichenreihe -> Zerlegungszeugen
|
|
|
|
tzr_zeugen [] [] = []
|
|
|
|
tzr_zeugen z t
|
|
|
|
| not (ist_tzr z t) = []
|
|
|
|
| otherwise = []
|
|
|
|
|
2021-10-15 14:11:06 +00:00
|
|
|
|
|
|
|
-- Aufgabe A.4
|
2021-10-15 18:03:48 +00:00
|
|
|
{-
|
|
|
|
Solution:
|
|
|
|
- we handle the case when the second string is empty - the result is always the length of the first string + 1
|
|
|
|
- if the second string is not a substring of the first one, return 0 as a result
|
|
|
|
- otherwise, count how many times the second string occurs in the first string by counting how many times the first string can be "cut" on the position of the second string
|
|
|
|
-}
|
2021-10-15 14:11:06 +00:00
|
|
|
|
2021-10-15 18:03:48 +00:00
|
|
|
wieOft :: Zeichenreihe -> Teilzeichenreihe -> Nat0
|
|
|
|
wieOft z [] = (length z) + 1
|
|
|
|
wieOft z t
|
|
|
|
| not (ist_tzr z t) = 0
|
|
|
|
| otherwise = count_sub_string z t 0
|
2021-10-15 14:11:06 +00:00
|
|
|
|
2021-10-15 18:03:48 +00:00
|
|
|
--- find how many times a substring occurs in a string
|
|
|
|
count_sub_string :: Zeichenreihe -> Teilzeichenreihe -> Nat0 -> Nat0
|
|
|
|
count_sub_string z t n
|
|
|
|
| not (ist_tzr z t) = n
|
|
|
|
| otherwise =
|
|
|
|
let
|
|
|
|
pos = (find_tzr_position z t 0) - 1
|
|
|
|
in count_sub_string (take pos z) t (n + 1)
|