
Hi, I think I need to use a list comprehension for this function but Im not good with list comprehension. this is what I have so at the moment? filmsInGivenYear :: Int -> [Film] -> [String] filmsInGivenYear filmYear ?= [ title | year <- (Film title director year fans) , year == filmYear] (this code wont compile - error given '?Syntax error in expression (unexpected `;', possibly due to bad layout)') Is there an alternative solution instead of comprehension? I want the function to be given a release date and then return all films with that date. Thanks -- View this message in context: http://www.nabble.com/List-comprehension-tp23389686p23389686.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello applebiz89, Tuesday, May 5, 2009, 7:20:35 PM, you wrote:
filmsInGivenYear :: Int -> [Film] -> [String] filmsInGivenYear filmYear ?= [ title | year <- (Film title director year fans) , year == filmYear] (this code wont compile - error given '?Syntax error in expression (unexpected `;', possibly due to bad layout)')
you forget films list variable: filmsInGivenYear filmYear films = [ title | (Film title director year fans) <- films, year == filmYear] -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

2009/5/6 Bulat Ziganshin
Hello applebiz89,
Tuesday, May 5, 2009, 7:20:35 PM, you wrote:
filmsInGivenYear :: Int -> [Film] -> [String] filmsInGivenYear filmYear ?= [ title | year <- (Film title director year fans) , year == filmYear] (this code wont compile - error given '?Syntax error in expression (unexpected `;', possibly due to bad layout)')
you forget films list variable:
filmsInGivenYear filmYear films = [ title | (Film title director year fans) <- films, year == filmYear]
And this is nicer if you define Film with records: -- just guessing as to types data Film = Film { title :: String, director :: String, year :: Int, fans :: [Fan] } filmsInGivenYear filmYear films = [title | film <- films, year film == filmYear] -- and then it's probably easier written as: filmsInGivenYear filmYear films = filter (\film -> year film == filmYear) films -- or slightly simpler: filmsInGivenYear filmYear = filter (\film -> year film == filmYear) -- or with Data.Function.Predicate (shameless plug) http://hackage.haskell.org/packages/archive/predicates/0.1/doc/html/Data-Fun...: filmsInGivenYear filmYear = filter (year `equals` filmYear)

porges@porg.es wrote:
-- or with Data.Function.Predicate (shameless plug) http://hackage.haskell.org/packages/archive/predicates/0.1/doc/html/Data-Fun...:
Whoa, a function called "isn't", why is this the first time I see that? :-) Martijn.

2009/5/5 applebiz89
Hi, I think I need to use a list comprehension for this function but Im not good with list comprehension. this is what I have so at the moment?
filmsInGivenYear :: Int -> [Film] -> [String] filmsInGivenYear filmYear ?= [ title | year <- (Film title director year fans) , year == filmYear] (this code wont compile - error given '?Syntax error in expression (unexpected `;', possibly due to bad layout)')
Is there an alternative solution instead of comprehension? I want the function to be given a release date and then return all films with that date.
Hi, I suggest you start by some simple examples, then move to your goal. For instance you can try to write a function which takes in input a list of Int and outputs a list of those Int that are odd. f [1,2,3,4,5] => [1,3,5] You can generalise this function to accept a predicate. g even [1,2,3,4,5] => [2,4] Returning back to your problem, can you write a function that takes a Film and returns its release date ? Then you can write a function which test if that date match a given one. Then, finally, you can write a function which get a list of Film and outputs only those satisfying your desire. Cheers, Thu

Hi, applebiz89 wrote:
Hi, I think I need to use a list comprehension
There is no need to use list comprehensions, there is always a way to express the same thing without them. In fact, list comprehensions are defined as syntactic shorthands for this other way.
filmsInGivenYear :: Int -> [Film] -> [String] filmsInGivenYear filmYear ?= [ title | year <- (Film title director year fans) , year == filmYear]
Let's look at the type of your function. You have two arguments of types Int and [Film], so your definition should have two argument names: filmsInGivenYear filmYear films = ... Now, we want to look at each of the films filmsInGivenYear filmYear films = [ ... | film <- films ... ] The part "film <- films" means: Look at each element of films in turn, and name the current one film. But we actually want to look inside the film (we want to check the year). filmsInGivenYear filmYear films = [ ... | Film title director year fans <- films ...] This means to look at each of the films in turn, and match the current film against the pattern (Film title director year fans). So if the current film has been created by a call like (Film ...), we will get the various data fields in the variables title, directory, year and fans. Now we want to check that the year is correct. filmsInGivenYear filmYear films = [ ... | Film title director year fans <- films, filmYear == year] The part (filmYear == year) means that we are only interested in such films where year is filmYear. Finally, we want to return the name of the remaining films. filmsInGivenYear filmYear films = [title | Film title director year fans <- films, filmYear == year] Hope that helps, Tillmann PS. I'm not a native speaker, but shouldn't it be "movies" and not "films"?
participants (7)
-
applebiz89
-
Brent Yorgey
-
Bulat Ziganshin
-
Martijn van Steenbergen
-
minh thu
-
porges@porg.es
-
Tillmann Rendel