在使用高阶函数的参数中对haskell进行排序 [英] Sorting in haskell with parameter using higher order function

查看:113
本文介绍了在使用高阶函数的参数中对haskell进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是一名Haskell初学者,我真的迷失了。
这是我的任务,它要求我使用更高的顺序功能来做类似下面的事情:

$ $ p $ 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]

首先,我认为我应该创建一个函数来区分它是否是< ,>或moreWord

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

但这个例子没有引号(即mysort(<)不是我的排序(<),所以这是我的第一个问题。这个函数,但它不是编译。否则是forWordWord

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

其次,我仍然难以理解高阶函数,这是否合理?

  mySort i列表
| i == 1 map(sortBy compare)list
| i == 2 map(sortBy(flip compare))list


解决方案

你不应该匹配这些功能sp特别地,它首先破坏了使用高阶函数的目的。事实上,你不能这样写,因为没有比较函数的一般方法。



相反,直接使用传递的函数进行排序。这样,它将适用于任何适合的比较功能,而不仅仅是您明确写入代码的功能。



例如,假设任务是使用传递的运算符合并两个值:

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

您可以像这样解决:

  combine op xy = x `op` y 

您可以使用类似的方法解决排序问题吗?



提示:您可能需要定义一个辅助函数来将传递的比较函数转换为适合 sortBy 的形式:

  compareUsing ::(a  - > a  - > Bool) - > (a  - > a  - >订购)
compareUsing op x y = ...


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

解决方案

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 = ...

这篇关于在使用高阶函数的参数中对haskell进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆