[Compiler] [Haskell] Filtern von Instanzen - Property Funktion

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von Smokers, 21. Mai 2011 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 21. Mai 2011
    [Haskell] Filtern von Instanzen - Property Funktion

    Hi ho Mitmenschen =)

    Erstmal : nein das is kein Compiler, aber Haskell wird hier als Sprache nicht angeboten.

    Also ich habe die folgende Aufgabe. Ich soll in Haskell die Funktion/Property mit dem Namen "prop1" fertig schreiben.

    Den Rest habe ich halt schon alles fertig.


    Code:
    module Blueprint where
    
    data Colour = Blue | Green | Red 
     deriving ( Eq, Show )
    
    data Car = Car { wheels :: Integer
     , colour :: Colour
     }
     deriving ( Eq, Show )
    
    data Property = Colour_Is Colour
     | Wheels Ordering Integer
     | And Property Property
     | Not Property 
     deriving Show 
    
    check :: Property -> Car -> Bool
    check prop car = case prop of
     Colour_Is col -> col == colour car
     Wheels ord num -> case ord of 
     LT -> num > wheels car
     EQ -> num == wheels car
     And l r -> check l car && check r car
     Not p -> not (check p car)
    
    cars = [ Car { wheels = 4, colour = Red }
     , Car { wheels = 2, colour = Blue }
     , Car { wheels = 14, colour = Green }
     , Car { wheels = 4, colour = Green }
     , Car { wheels = 2, colour = Red }
     ]
    
    prop1 :: Property
    prop1 = undifined
    
    test :: Bool
    test = and
     [ check ( Wheels EQ 4 ) ( cars !! 0 )
     , check ( Wheels LT 3 ) ( cars !! 1 )
     , check ( And ( Wheels EQ 14 ) ( Colour_Is Green )) ( cars !! 2 )
     , check ( Not ( Colour_Is Red ) ) ( cars !! 3 )
     , filter ( check prop1 ) cars == take 3 cars
     ]
    
    Ich habe bisher die Funktion check fertig geschrieben und nun soll prop1 so definiert werden das wie unten im Prüffall halt nur die ersten drei Instanzen von cars gefiltert werden.

    Siehe Testfall :
    filter ( check prop1 ) cars == take 3 cars

    Wenn ich prop1 definiere :

    prop1 :: Property
    prop1 = Wheels EQ 4


    Dann ist prop1 vom richtigen Typ filtern natürlich aber nicht richtig.

    Hat dort jemand Ideen? Denn Wheels EQ 4 && Wheels EQ 14 ... funktioniert irgendwie nicht.


    (Bin Haskell Anfänger)


    \\
    aus irgend einem Grund funktioniert der hier nicht :-/

    prop1 :: Property
    prop1 = And (And (Wheels EQ 2) (Colour_Is Red) ) (And (Wheels EQ 4) (Colour_Is Green))

    Da bekomme ich KEINE Cars anstatt die letzten beiden :-/
     
  2. 22. Mai 2011
    AW: [Haskell] Filtern von Instanzen - Property Funktion

    Okay ich habe zumindest schonmal die Or Function implementiert bekommen :

    Code:
    module Blueprint where
    
    data Colour = Blue | Green | Red 
     deriving ( Eq, Show )
    
    data Car = Car { wheels :: Integer
     , colour :: Colour
     }
     deriving ( Eq, Show )
    
    data Property = Colour_Is Colour 
     | Wheels Ordering Integer
     | And Property Property
     | Not Property
     | Or Property Property 
     deriving Show 
    
    check :: Property -> Car -> Bool
    check prop car = case prop of
     Colour_Is col -> col == colour car
     Wheels ord num -> case ord of 
     LT -> num > wheels car
     EQ -> num == wheels car
     And l r -> check l car && check r car
     Not p -> not (check p car)
     Or x y -> check x car || check y car 
    
    cars = [ Car { wheels = 4, colour = Red }
     , Car { wheels = 2, colour = Blue }
     , Car { wheels = 14, colour = Green }
     , Car { wheels = 4, colour = Green }
     , Car { wheels = 2, colour = Red }
     ]
    
    prop1 :: Property
    prop1 = Not (Or (And (Wheels EQ 4) (Colour_Is Green)) (And (Wheels EQ 2) (Colour_Is Red)))
    
    test :: Bool
    test = and
     [ check ( Wheels EQ 4 ) ( cars !! 0 )
     , check ( Wheels LT 3 ) ( cars !! 1 )
     , check ( And ( Wheels EQ 14 ) ( Colour_Is Green )) ( cars !! 2 )
     , check ( Not ( Colour_Is Red ) ) ( cars !! 3 )
     , filter ( check prop1 ) cars == take 3 cars
     ]
    
    Leider sagt mir mein Abgabe -Tool das ich wohl keine neue Funktion hinzufügen darf, also kein "Or".

    Kann man mittels And und Not ein Or erstellen?, nicht das ich wüsste oder?
     
  3. 22. Mai 2011
    AW: [Haskell] Filtern von Instanzen - Property Funktion

    Code:
    prop1 = And ( And ( Wheels LT 5 ) ( Not ( Color_is Blue ) ) Not ( And ( Color_Is Red ) ( Wheels EQ 4 ) ) )
    kann es leider nicht testen, der ausdruck sollte aber stimmen.

    habs mal in javascript nachgestellt:
    Code:
    // And ( And ( Wheels LT 5 ) ( Not ( Color_is Blue ) ) Not ( And ( Color_Is Red ) ( Wheels EQ 4 ) ) )
    
    var cars = [
     { wheels: 4, colour: "Red" }
     , { wheels: 2, colour: "Blue" }
     , { wheels: 14, colour: "Green" }
     , { wheels: 4, colour: "Green" }
     , { wheels: 2, colour: "Red" }
    ];
    
    var filtered = cars.filter(
     function(car) {
     return ((car.wheels < 5 && !(car.colour == "Blue")) 
     && !(car.colour == "Red" && car.wheels == 4));
     }
    );
    
    console.log("gefunden: ", filtered.length);
    
    for each(car in filtered)
     console.log(car.colour, " ", car.wheels);
    hier stimmt das ergebnis:
    Code:
    gefunden: 2
    Green 4
    Red 2
    // jetzt sind es btw. schon 2 biere auf die du mich einladen darfst
     
  4. 22. Mai 2011
    AW: [Haskell] Filtern von Instanzen - Property Funktion

    Nicht schlecht, nicht schlecht DDD
    Dann musste mir mal deine Adresse schicken dann lass ich dir mal n Kasten zukommen. Gleich ein paar Flaschen in Reserve.... ^^


    Hier DEIN Output :

    Code:
    [B]Neue Bewertung[/B]
    module Blueprint where
    
    data Colour = Blue | Green | Red 
     deriving ( Eq, Show )
    
    data Car = Car { wheels :: Integer
     , colour :: Colour
     }
     deriving ( Eq, Show )
    
    data Property = Colour_Is Colour 
     | Wheels Ordering Integer
     | And Property Property
     | Not Property
     deriving Show 
    
    check :: Property -> Car -> Bool
    check prop car = case prop of
     Colour_Is col -> col == colour car
     Wheels ord num -> case ord of
     LT -> num > wheels car
     EQ -> num == wheels car
     And l r -> check l car && check r car
     Not p -> not (check p car)
    
    cars = [ Car { wheels = 4, colour = Red }
     , Car { wheels = 2, colour = Blue }
     , Car { wheels = 14, colour = Green }
     , Car { wheels = 4, colour = Green }
     , Car { wheels = 2, colour = Red }
     ]
    
    prop1 :: Property
    prop1 = Not (And (And ( Wheels LT 5 ) ( Not ( Colour_Is Blue ) )) ( Not ( And ( Colour_Is Red ) ( Wheels EQ 4 ) ) ))
    
    
    test :: Bool
    test = and
     [ check ( Wheels EQ 4 ) ( cars !! 0 )
     , check ( Wheels LT 3 ) ( cars !! 1 )
     , check ( And ( Wheels EQ 14 ) ( Colour_Is Green )) ( cars !! 2 )
     , check ( Not ( Colour_Is Red ) ) ( cars !! 3 )
     , filter ( check prop1 ) cars == take 3 cars
     ]
    
    partiell korrekt?
    paßt Ihr Quelltext zum Muster?Ja.
    total korrekt?
    expression test 
    type Bool
    richtiger Typ?[INDENT]Ja.[/INDENT]
    richtiger Wert?[INDENT]Ja.[/INDENT]
    [B]Bewertung der Einsendung: Okay[/B]
    
     { punkte = 1 , size_ = 1346 }

    Oh man, was für eine Qual. ich hatte den Prof noch angeschrieben bzgl meiner Or Funktin, aber er meinte er hat das mit Absicht nicht zugelassen ^^
    Alles klar.

    Genatwortet hat er mir 4:14 Uhr.... was n Freak.
    Und Haskell lässt dich jetzt nicht mehr los Murdoc?
     
  5. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.