Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Haskell Übung 9
- Tatjana Harder, Christine Krömke, Peter Obert
- Aufgabe 9.2
- derive gibt die Ableitung eines Polynoms aus, das als Liste ganzer
- Zahlen in der Form 1+7x+5x^2+2x^3 -> [1,7,5,2] repräsentiert wird
- >derive :: [Int] -> [Int]
- >derive [] = []
- >derive (as)
- > |(length as>1) = derive (init as) ++ [(last as)*((length as)-1)]
- > | otherwise =[]
- derive' bildet die n-te Ableitung des Polynoms
- >derive' :: Int -> [Int] -> [Int]
- >derive' n (as)
- > | (n==0) = as
- > | (length as == 1) = [0]
- > | otherwise = derive' (n-1) (derive as)
- Aufgabe 9.3
- mysqrt berechnet die Wurzel einer (positiven) Zahl nach dem Heron-Verfahren,
- wobei x die Zahl und r der Startwert (z.B. 1) ist
- >mysqrt :: Float -> Float -> Float
- >mysqrt x r
- > |(x<0) || (r<0) = error "Negative Eingabe"
- > |(abs(y-r)<(0.00001)) = y
- > |otherwise = mysqrt x y
- > where y=0.5*(r+x/r)
- xs ist die Liste der Differenzen der haskelleigenen sqrt Funktion und mysqrt
- xs = [abs((sqrt x)-(mysqrt x 1)) | x<-[2341234,35845345,4345835435,596656456,7772227272]]
- xs = [0.0,0.0,0.0,1.953125e-3,0.0]
- Schlussfolgerung: das Heron-Verfahren bzw. mysqrt berechnet relativ präzise die Wurzel,
- wobei die Präzision vom Wert 0.00001 abhängt und somit eingestellt werden kann
- Aufgabe 9.4
- der Datentyp Binary ist eine Liste aus Bits, die so aussehen kann:
- [Eins,Null,Eins,Null,Null,Eins,Eins,Eins,Null,Null,Eins]
- sie repräsentiert das Binärsystem und entspricht "10100111001"
- >type Binary = [Bit]
- >data Bit = Null | Eins deriving (Eq, Show)
- int2bin wandelt einen Int wert in das Binärsystem, repräsentiert als Binary
- >int2bin :: Int -> Binary
- >int2bin a
- > |(b>0) && (c==0) = int2bin b ++ [Null]
- > |(b>0) && (c==1) = int2bin b ++ [Eins]
- > |(b==0) && (c==0) = [Null]
- > |(b==0) && (c==1) = [Eins]
- > where b = div a 2
- > c = mod a 2
- bin2int macht das Gegenteil von int2bin
- >bin2int :: Binary -> Int
- >bin2int [] = 0
- >bin2int as
- > |(as!!0==Eins)=2^(length as-1) + bin2int (tail as)
- > |otherwise = bin2int (tail as)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement