0

I need to implement a function that takes a list of Dists and returns a list of Dists. I need the function to return only the Dists with the label "pass", but somehow this does not work. Any help?

data Ex = Ex Float Float String String deriving Show data NewSt = NewSt Float Float String deriving Show data Dist = Dist Float NewSt Ex deriving Show helper1 [] = [] helper1 (Dist x (NewSt midterm1 quiz1 name1) (Ex midterm quiz name label) : xs) = if (label == "pass") then Dist x (NewSt midterm1 quiz1 name1) (Ex midterm quiz name label) : (helper1 xs) else helper1 xs 
0

2 Answers 2

2

This is a little simpler to write with more pattern matching than with an if expression.

helper1 :: [Dist] -> [Dist] helper1 [] = [] helper1 (Dist x newst (Ex midterm quiz name "pass") : xs) = Dist x newst (Ex midterm quiz name "pass") : (helper1 xs) helper (_:xs) = helper1 xs 

However, it is even simpler once you recognize that your recursion is already implemented by the filter function.

helper1 :: [Dist] -> [Dist] helper1 = filter passed where passed (Dist _ _ (Ex _ _ _ "pass")) = True passed _ = False 
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you may do as follows;

data Ex = Ex Float Float String String deriving Show data NewSt = NewSt Float Float String deriving Show data Dist = Dist Float NewSt Ex deriving Show myData :: [Dist] myData = [Dist 1 (NewSt 66 100 "John") (Ex 55 90 "John" "pass"), Dist 2 (NewSt 20 45 "Tom") (Ex 33 50 "Tom" "fail"), Dist 3 (NewSt 75 75 "Mary") (Ex 90 100 "Mary" "pass")] helper1 [] = [] helper1 d@(Dist _ _ (Ex _ _ _ label):ds) | label == "pass" = (head d):(helper1 ds) | otherwise = helper1 ds 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.