1

Hi I'm a Haskell beginner and I'm really lost. This is for my assignment, and it asks me to do something like below using higer order function

Main> mySort (<) [1,5,3,6,4,1,3,3,2] 
[1,1,2,3,3,3,4,5,6] 
Main> mySort (>) [1,5,3,6,4,1,3,3,2] 
[6,5,4,3,3,3,2,1,1] 
Main> mySort longerWord [“Hello”, “The”, “a”, “Daniel”, “Declarative”]
[“Declarative”, “Daniel”, “Hello”, “The”, “a”]

First of all, I thought I should make a function that distinguish whether it's < , > or longerWord

checkConditionStr::String->Int
checkConditionStr str
    |str=="(<)" =1
    |str=="(>)" =2
    |str=="longerWord" =3

but the example doesn't have quotation mark (i.e. mysort (<) not my sort"(<)" so here is my first problem. I worte this function but it's not compiling. otherwise is for longerWord

checkCondition::Ordering->Int
checkCondition ord
    |ord==(<) =1
    |ord==(>) =2
    |otherwise =2

secondly I still have difficulty understanding higher order function. would this make sense?

mySort::(String->Int)->[a]->[a]
mySort i list
    |i==1 map (sortBy compare) list
    |i==2 map (sortBy(flip compare)) list

1 Answers1

5

You're not supposed to match against those functions specifically. It defeats the purpose of using a higher-order function in the first place. In fact, you can't write it like this, since there is no general way of comparing functions.

Instead, use the passed function directly for the sorting. That way, it will work for any suitable comparison function, not just the ones you've explicitly written code for.

For example, imagine the task was to combine two values using a passed operator:

combine (+) 2 3 = 5
combine (*) 3 5 = 15
combine max 10 100 = 100

You would solve it like this:

combine op x y = x `op` y

Can you use a similar approach to solving the sorting problem?

Hint: You may want to define a helper function to transform the passed comparison function into a form suitable for sortBy:

compareUsing :: (a -> a -> Bool) -> (a -> a -> Ordering)
compareUsing op x y = ...
hammar
  • 138,522
  • 17
  • 304
  • 385
  • I'd say that comparing function is useless since the whole idea of high-order function is to leave it open for injecting something that would be known in the future. – ony Apr 19 '13 at 10:35
  • either you didn't understood what I said or I don't understand what you are saying. I'm trying to say that having `myHighOrder f = if f == (<) then ...` doesn't make sense since high order function in this case will be limited to a set of functions. And thus can be written directly. While usual high-order function actually is an open family of functions that can be parametrized by any function that satisfy some restrictions. – ony Apr 19 '13 at 11:00
  • @ony: That is exactly what I'm saying, or trying to say in my answer. – hammar Apr 19 '13 at 11:01
  • for me "not supposed to" doesn't sounds so strict as I'd prefer in this case :) – ony Apr 19 '13 at 11:03
  • @hammar I appreciate your answer but I still can not solve this. I spent lots of time but it's just too hard to understand. –  Apr 24 '13 at 10:25
  • `(a->a->Ordering)`, to return Ordering there is only one function, using 'compare' as far as I know. but then the input like (<),(>) would mean nothing. Then I have come up with these, I know it's not making sense but this is all I can do atm `compareUsing :: (a -> a -> Bool) -> (a -> a -> Bool)` `compareUsing op x y = x `op` y` `mySort :: (a -> a -> Ordering)->(a -> a -> Bool) -> [a]->[a]` `mySort op (h:t) = sortBy compareUsing (h:t)` –  Apr 24 '13 at 10:29
  • @SahyunKim: Take a look at [this answer](http://stackoverflow.com/a/16182424/98117). His definition of `wrap` is the same as what I called `compareUsing`. – hammar Apr 24 '13 at 10:30
  • @SahyunKim: You know what `compare` does, right? How would you implement `compare` using `<`? Then take that and replace `<` with `op` and you have `compareUsing`. – hammar Apr 24 '13 at 10:35
  • I really appreciate your help, and have read wrap. Althogh it's working fine and understand the logic, I don't know what `$` does and don't understand `sortBy . wrap` –  Apr 24 '13 at 12:16
  • @SahyunKim: See: [Haskell: difference between `.` and `$`](http://stackoverflow.com/questions/940382/haskell-difference-between-dot-and-dollar-sign) – hammar Apr 24 '13 at 12:31