• Keine Ergebnisse gefunden

Fallunterscheidung, Pattern Matching

N/A
N/A
Protected

Academic year: 2022

Aktie "Fallunterscheidung, Pattern Matching"

Copied!
10
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Algebraische Datentypen in Haskell

data Foo = Foo { bar :: Int, baz :: String } deriving Show

Bezeichnungen (benannte Notation) I data Fooist Typname

I Foo { .. }ist Konstruktor I bar, bazsind Komponenten x :: Foo

x = Foo { bar = 3, baz = "hal" } Bezeichnungen (positionelle Notation) data Foo = Foo Int String y = Foo 3 "bar"

Mathematisch: Produkt Foo=Int×String

(2)

Datentyp mit mehreren Konstruktoren

Beispiel (selbst definiert)

data T = A { foo :: Int }

| B { bar :: String, baz :: Bool } deriving Show

Bespiele (in Prelude vordefiniert) data Bool = False | True data Ordering = LT | EQ | GT

Mathematisch: (disjunkte) Vereinigung Bool={False} ∪ {True}

(3)

Fallunterscheidung, Pattern Matching

data T = A { foo :: Int }

| B { bar :: String } Fallunterscheidung:

f :: T -> Int f x = case x of

A {} -> foo x

B {} -> length $ bar x

Pattern Matching (Bezeichnerf,bwerden lokal gebunden):

f :: T -> Int f x = case x of

A { foo = f } -> f

B { bar = b } -> length b

(4)

data und case

typisches Vorgehen beim Programmieren einer Funktion f :: T -> ...

Für jeden Konstruktor des Datentyps data T = C1 ...

| C2 ...

schreibe einen Zweig in der Fallunterscheidung f x = case x of

C1 ... -> ...

C2 ... -> ...

(5)

Zusammengesetzte Datentypen

Operationen:

Produkt A×B Beispiel:

data Punkt = Punkt { x :: Float, y :: Float}

data Kreis = Kreis { mp :: Punkt, rad :: Float } (disjunkte) Vereinigung AB

Beispiel Wahrheitswerte (vordefiniert)

data Bool = True | False

data Shape = Circle { mp :: Punkt, rad :: Float }

| Rect { ol, ur :: Punkt}

umfang :: Shape -> Float umfang s = case s of

Circle {} -> 2 * pi * ( rad s ) Rect ol ur -> ...

Potenz AB={f :BA}

z.B. gerade_laenge :: String -> Bool

(6)

Polymorphe Datentypen – Beispiele

data Pair a b = Pair a b (Produkt) Beispiel: 4 Elemente inPair Bool Bool=

{Pair False False,Pair False True, Pair True False,Pair True True}

data Either a b = Left a | Right b (Vereinigung) Beispiel: 4 Elemente inEither Bool Bool=

{Left False,Left True,Right False,Right True} data Maybe a = Nothing | Just a (Vereinigung) Listen: data List a = Nil | Cons a (List a)

(7)

Rekursive Datentypen

Wiederholung: Peano-Zahlen data Nat = Z | S Nat Pattern Matching:

f :: Nat a -> ...

f x = case x of Z -> ...

S x -> ...

Listen (polymorph mit Typvariablea) data List a = Nil

| Cons { head :: a, tail :: List a}

Pattern Matching:

f :: List a -> ...

f xs = case xs of Nil -> ...

Cons x xss -> ...

(8)

Rekursive Funktionen – Beispiele

data Nat = Z | S Nat

data List a = Nil | Cons a (List a) (rekursive) Funktionen über Peano-Zahlen, z.B.

double :: Nat -> Nat double x = case x of

Z -> Z

S x’ -> S ( S ( double x’ ) )

(rekursive) Funktionen auf Listen von Peano-Zahlen, z.B.

doubles :: List Nat -> List Nat doubles xs = case xs of

Nil -> Nil

Cons x xss -> Cons ( double x ) ( doubles xss ) (rekursive) Funktionen auf polymorphen Listen, z.B.

append :: List a -> List a -> List a append xs ys = case xs of

Nil -> ys

Cons x xss -> Cons x (append xss ys)

(9)

Vordefinierte Datentypen in Haskell

einfach:

I Bool(False,True) I Char

I Int,Integer(beliebig genau) I Float

zusammengesetzt (Typkonstruktoren):

I selbstdefiniertconstr typ1 ... typn

z.B.HR,Nat,List a(polymorph mit Typ-Variablea) I vordefiniert

I Tupel-Konstruktor (polymorph)(a, b),(a, b, c), (a1, a2, ...)

z.B. (1, True, ’B’) :: (Int, Bool, Char) I Listen-Konstruktor (polymorph)[a],

z.B.[3,5,2] :: [Int],

[[’I’, ’N’],[’B’]] :: [[Char]]

I String = [Char], z.B."INB" = [’I’,’N’,’B’]

(10)

Typsynonyme

(Um-)Benennung vorhandener Typen (meist als Kurzform) Beispiel:

type String = [ Char ] type Name = String

type Telefonnummer = Int

type Telefonbuch = [ ( Name , Telefonnummer ) ] nummern :: Name -> Telefonbuch -> [ Telefonnummer ] nummern name [] = []

nummern name ( ( n , t ) : rest ) ...

allgemeiner: Wörterbücher

type Woerterbuch a b = [ ( a, b ) ] rekursive Typen sind nicht als Typsynonym definierbar

Referenzen

ÄHNLICHE DOKUMENTE

We compare the efficiency of type switching on multiple arguments in comparison to other relational matching alternatives based on double, triple and quadruple dispatch [16], as well

Keywords: pattern matching, superpositional graph, separable permu-..

Da die Guard Elimination auch das Pattern Matching in case -Ausdrücke übersetzt und somit die Anzahl der Regeln auf eins reduziert, stellt die Guard Elimination eine Alternative

Here, we investigate the import of a concept well-proven in all kinds of programming styles into the object oriented paradigm, namely pattern matching: The Paisley library

We augment the approach to include a caching algorithm that further improves matching performance by up to fifty per cent given the model base and patterns we used.. The algorithm

First, the product offer title usually contains several information such as the product name, product type, manufacturer, a manufacturer-specific product code (e.g., HF S10 for

Generic Specification Environment for Conceptual Modelling Languages and Models Hence, modelling languages consist of element types, which are specialized into object types

Subsequent to the partitioned suffix array algorithm each set of multiple substrings is rep- resented by a cluster number and a set of global sequence positions in the