Can we put functions to determine the condition for our list comprehensions. Here is my code that I am trying to implement:
mQsort :: [String] -> [F.Record] -> [F.Record]
mQsort [] _ = []
mQsort c@(col:cond:cs) l@(x:xs) = (mQsort c small) ++ mid ++ (mQsort c large)
where
small = [y | y<-xs, (qGetStr col y) (qGetCond cond) (qGetStr col x)]
mid = mQsort cs [y | y<-l, (qGetStr col y) == (qGetStr col x)]
large = [y | y<-xs, (qGetStr col y) (qGetCond' cond) (qGetStr col x)]
qGetStr :: String -> F.Record -> String
qGetStr col r | U.isClub col = F.club r
| U.isMap col = F.mapName r
| U.isTown col = F.nearestTown r
| U.isTerrain col = F.terrain r
| U.isGrade col =F.mapGrade r
| U.isSW col = F.gridRefOfSWCorner r
| U.isNE col = F.gridRefOfNECorner r
| U.isCompleted col = F.expectedCompletionDate r
| U.isSize col = F.sizeSqKm r
| otherwise = ""
qGetCond "ascending" = (<)
qGetCond "decending" = (>)
qGetCond' "ascending" = (>)
qGetCond' "decending" = (<)
I get an error stating that the function qGetStr is applied to 4 arguments instead of 2.
Also, qGetCond – is this the right syntax to return an operator. I had to place the operator in brackets due to a compile error, but I have a feeling this is incorrect
Replace
with
Similarly for
large.The reason is the same as the reason for the syntax you use to return an operator in
qGetCond. An operator is just a function really.foo < baris the same as(<) foo barfoo `fire` baris the same asfire foo barSo you have to move the “operator” to the start of the expression in your list comprehension. (n.b. this is true in general and has nothing to do with list comprehensions in particular.)
Edit: to expand on Chris Kuklewicz’s point:
Backticks only work around a single identifier. So
foo `fire` baris valid syntax, but something more complicated likefoo `fire forcefully` barorfoo `(fire forcefully)` baris a syntax error.Parentheses round operators are more flexible. These expressions all evaluate to the same value:
foo < bar(<) foo bar(foo <) bar(< bar) fooThe last two forms are called operator sections. They are useful when passing a function to another function, e.g.
map (+1) listOfIntegers. There are a couple of subtleties with the syntax: