Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. J. O. Blech Dipl.-Inform. M. J. Gawkowski Dipl.-Inform. N. Rauch
TU Kaiserslautern
Fachbereich Informatik AG Softwaretechnik
Lösungvorschlag zum Übungsblatt 3: Software-Entwicklung I (WS 2007/08)
Aufgabe 1 Operatorpräzedenzen (2 Punkte)
fun f (x, y) = (((x < x) = true) = y);
val f = fn : int * bool -> bool
fun m (a, b) = ((((a * b) + a) + b) > 0);
val m = fn : int * int -> bool
Aufgabe 2 Syntax-, Typisierungs- und Laufzeitfehler (4 Punkte)
- 6.08 * 3.14 * 2.05;
val it = 39.13696 : real - 1 + 1 = 2.0;
stdIn:5.1-5.12 Error: operator and operand don’t agree [literal]
operator domain: int * int operand: int * real in expression:
1 + 1 = 2.0 Korrekt: 1 + 1 = 2;
- implode(rev (explode "1234567890"));
val it = "0987654321" : string - ((-4)=(~(1+3)));
stdIn:6.3 Error: expression or pattern begins with infix identifier "-"
stdIn:6.3-6.5 Error: operator and operand don’t agree [literal]
operator domain: ’Z * ’Z
operand: int
in expression:
- 4
Minus - und ~ werden unterschieden (zweistelliger und einstelliger Operator) - (1=2) <> true;
val it = true : bool - #1(true,true);
val it = true : bool
- [1,2,3] = [1]@[2,3];
val it = true : bool - [1,2,3] = [1,[2,3]];
stdIn:9.11-9.20 Error: operator and operand don’t agree [literal]
operator domain: int * int list operand: int * int list list in expression:
1 :: (2 :: 3 :: nil) :: nil Liste vs. geschachtelte Liste - [1,2,3] = 1::[2,3];
val it = true : bool - [1,2,3] = [1]::[2,3];
stdIn:10.1-10.21 Error: operator and operand don’t agree [literal]
operator domain: int list * int list list operand: int list * int list in expression:
(1 :: nil) :: 2 :: 3 :: nil
Liste wird als Listenelement an Liste gehaengt
Aufgabe 3 Die Datenstruktur String
1 (****
2 val it = fn : char -> int
3 - ord (#"a");
4 val it = 97 : int
5 - ord (#"z");
6 val it = 122 : int
7 - ord (#"A");
8 val it = 65 : int
9 - ord (#"Z");
10 val it = 90 : int
11 -
12 *****)
13
14 (** map ord [#"A",...,#"Z"] = [65,...,90] **)
15 (** map ord [#"a",...,#"z"] = [97,...,122] **)
16
17 (** map chr [65,...,90] = [#"A",...,#"Z"] **)
18 (** map chr [97,...,122] = [#"a",...,#"z"] **)
19
20 fun is_capital ch = let val x = ord ch in 65 <= x andalso x <= 90 end
21 fun is_small ch = let val x = ord ch in 97 <= x andalso x <= 122 end
22 fun capital2small ch = chr((ord ch)+32)
23 fun small2capital ch = chr((ord ch)-32)
24 fun swap_char c = if is_capital c
25 then capital2small c
26 else if is_small c
27 then small2capital c
28 else c
29 fun swap_chars l = if null l then l
30 else (swap_char(hd l))::(swap_chars(tl l))
31 fun swap_letters s = implode(swap_chars(explode s))
Aufgabe 4 Rekursion
a) Die Korrektheit der Eingabe wird nicht überprüft. Wir nehmen an, dass bei Aufrufen nur Parameterwerte größer gleich Null übergeben werden.
1 fun sum 0 = 0.0
2 | sum n = Real.+(sum (n-1),(Real./(1.0,Real.fromInt n)))
b)
1 fun lsum_helper(uebertr,x::xs,y::ys)=
2 ((x+y+uebertr) mod 10)::(lsum_helper((x+y+uebertr) div 10,xs,ys))
3 | lsum_helper(u,x::xs,[])=if u=0
4 then x::xs
5 else ((u+x) mod 10)::lsum_helper((u+x)div 10,xs,[])
6 | lsum_helper(u,[],x::xs)=if u=0
7 then x::xs
8 else ((u+x) mod 10)::lsum_helper((u+x)div 10,xs,[])
9 | lsum_helper(u,[],[]) =[u]
10
11 fun lsum(l1,l2)= rev(lsum_helper(0,rev l1, rev l2));
c)
1 fun check(n,[])=(n=0)
2 | check(n,x::xs)=check(n-1,xs)
d)
1 fun multiples_helper(n,m,i,[])=true
2 | multiples_helper(n,m,i,x::xs)=((i*m)=x) andalso (multiples_helper(n,m,i+1,xs))
3
4 fun nmultiples(n,m,l)=multiples_helper(n,m,1,l)
e)
1 fun helper (#"I"::(#"F"::xs))= helper xs
2 | helper (#"T"::(#"H"::(#"E"::(#"N"::xs))))= helper xs
3 | helper (#"E"::(#"L"::(#"S"::(#"E"::xs))))= helper xs
4 | helper [] = true
5 | helper _ = false
6
7 fun concattest1 s = if s="" then false else helper (explode s);
f)
1 fun h_if (#"F"::xs)= helper xs
2 | h_if l = false
3 and h_then (#"H"::(#"E"::(#"N"::xs)))= helper xs
4 | h_then l = false
5 and h_else (#"L"::(#"S"::(#"E"::xs)))= helper xs
6 | h_else l = false
7 and helper (#"I"::xs)= h_if xs
8 | helper (#"T"::xs)= h_then xs
9 | helper (#"E"::xs)= h_else xs
10 | helper [] = true
11 | helper _ = false
12
13 fun concattest2 s = if s="" then false else helper (explode s);
Aufgabe 5 Terminierung
1 (* lookup s env = (0,0) : In env kommt keine Bindung mit dem Schlüssel s vor. *)
2 (* lookup s env = (1,x) : In env wurde eine Bindung (s,x) gefunden. *)
3 fun lookup key (env : (string * int) list) =
4 if null env
5 then (0,0)
6 else (if key = #1(hd env)
7 then (1,#2(hd env))
8 else lookup key (tl env))