www.jammni.de

Logo - Kleiner Drache
Login
Username:

Passwort:

Daten merken
Auto-Login
Registrieren
 
Online
niemand
 
Forumsuche
Suche nach:

Logo - DracheHaskell-Forum

Sanne1612

Gepostet:
25.11.2012 12:18

Maybe/Nothing  
Hallo ihr Lieben,

habe wieder ein Problemchen. Die Funktion majority soll zunächst aus einer Liste ein Pivotelement auswählen (majority\':klappt) und dann gucken,
ob es in der Liste die absolute Mehrheit hat (majority\'\'). Gibt es eine absolute Mehrheit dann wird das Element ausgespuckt (Just a), sonst Nothing.
Die Funktion majority\'\' funktioniert an sich, spuckt aber immer Just a aus, egal ob if ((((length xs) `div` 2) + 1) <= zaehler) True oder False ergibt.
Wie kann das sein? Wo ist der Fehler?

majority::(Eq a)=>[a]->Maybe a
majority xs = majority\'\' xs (majority\' (tail xs) ((head xs),1)) 0


majority\' xs (a,b)
| xs == [] = a
| otherwise = if ((head xs) == a)
then majority\' (drop 1 xs) (a,(b+1))
else (if (b==0) then majority\' (drop 1 xs) ((head xs),1) else majority\' (drop 1 xs) (a,(b-1)))

majority\'\'::(Eq a)=>[a] -> a -> Int -> Maybe a
majority\'\' xs a zaehler
| xs == [] = if ((((length xs) `div` 2) + 1) <= zaehler)
then Just a
else Nothing
| otherwise = if (head xs == a) then majority\'\' (drop 1 xs) a (zaehler+1) else majority\'\' (drop 1 xs) a zaehler

Vielleicht kann jemand helfen. Vielen Dank!
Zum Seitenanfang    
 
IKT

Gepostet:
25.11.2012 14:38

   
bzgl xs == [] = if ((((length xs) `div` 2) + 1) :

wieso (length xs), wenn du weißt dass xs die Länge 0 hat?
Zum Seitenanfang    
 
Sanne1612

Gepostet:
25.11.2012 15:02

I see I see  
Thanx...das muss \'türlich die Länge der Ursprungsliste sein.
Je länger man drauf guckt, desto mehr steht man auf dem Schlauch.
Zum Seitenanfang