Haskell:自定义数据类型的排序向量

Haskell:自定义数据类型的排序向量,haskell,Haskell,如何使用data.Vector.Algorithmsie中的sortBy对自定义数据类型的向量进行排序: data Person = Person { name :: String , age :: Int } deriving (Show, Eq) p1,p2,p3 :: Person p1 = Person "Alice" 30 p2 = Person "Bob" 20 p3 = Person "Charles" 10 personVec :: Vector

如何使用
data.Vector.Algorithms
ie中的
sortBy
对自定义数据类型的向量进行排序:

data Person = Person 
  { name :: String
  , age  :: Int
  } deriving (Show, Eq)

p1,p2,p3 :: Person
p1 = Person "Alice"   30
p2 = Person "Bob"     20
p3 = Person "Charles" 10

personVec :: Vector Person
personVec = fromList [p2,p1,p3]
我想做一些类似的事情:

sortedByName :: Vector Person
sortedByName = modify (sortBy (comparing name)) personVec

sirtedByAage :: Vector Person
sortedByAge  = modify (sortBy (comparing age)) personVec

但是不知道该把什么作为
的论据,因此

提出问题

请包括MCVE,包括导入、pragma、代码和错误。例:

{-# LANGUAGE OverloadedLists #-}
import Data.Vector
import Data.Ord
import Data.Vector.Algorithms.Intro

data Person = Person.
  { name :: String
  , age  :: Int
  } deriving (Show, Eq)

p1,p2,p3 :: Person
p1 = Person "Alice"   30
p2 = Person "Bob"     20
p3 = Person "Charles" 10

personVec :: Vector Person
personVec = fromList [p2,p1,p3]

sortedByName :: Vector Person -> Vector Person
sortedByName = modify sortBy (comparing name) personVec

sortedByAge :: Vector Person -> Vector Person
sortedByAge  = modify sortBy (comparing age) personVec
通过
ghci-8.4.3$文件加载时,会产生错误,包括以下最后一个错误:

so.hs:23:31: error:
    • Couldn't match expected type ‘Vector a1’
                  with actual type ‘Person -> Person -> Ordering’
    • Probable cause: ‘comparing’ is applied to too few arguments
      In the second argument of ‘modify’, namely ‘(comparing age)’
      In the expression: modify sortBy (comparing age) personVec
      In an equation for ‘sortedByAge’:
          sortedByAge = modify sortBy (comparing age) personVec
   |
23 | sortedByAge  = modify sortBy (comparing age) personVec
   |                               ^^^^^^^^^^^^^
Failed, no modules loaded.
参数的参数

modify
函数接受两个参数,但由于缺少括号,您传递了三个参数:

sortedByAge = modify (sortBy (comparing age)) personVec
类型:这是向量还是函数?

您的
sortedbage
变量的命名意味着它是一个向量,但您提供的类型表明它是一个计算向量的函数。让我们改变这种类型:

sortedByAge :: Vector Person
sortedByName
也是如此

结果

*Main> sortedByName
[Person {name = "Alice", age = 30},Person {name = "Bob", age = 20},Person {name = "Charles", age = 10}]
*Main> sortedByAge
[Person {name = "Charles", age = 10},Person {name = "Bob", age = 20},Person {name = "Alice", age = 30}]

提出问题

请包括MCVE,包括导入、pragma、代码和错误。例:

{-# LANGUAGE OverloadedLists #-}
import Data.Vector
import Data.Ord
import Data.Vector.Algorithms.Intro

data Person = Person.
  { name :: String
  , age  :: Int
  } deriving (Show, Eq)

p1,p2,p3 :: Person
p1 = Person "Alice"   30
p2 = Person "Bob"     20
p3 = Person "Charles" 10

personVec :: Vector Person
personVec = fromList [p2,p1,p3]

sortedByName :: Vector Person -> Vector Person
sortedByName = modify sortBy (comparing name) personVec

sortedByAge :: Vector Person -> Vector Person
sortedByAge  = modify sortBy (comparing age) personVec
通过
ghci-8.4.3$文件加载时,会产生错误,包括以下最后一个错误:

so.hs:23:31: error:
    • Couldn't match expected type ‘Vector a1’
                  with actual type ‘Person -> Person -> Ordering’
    • Probable cause: ‘comparing’ is applied to too few arguments
      In the second argument of ‘modify’, namely ‘(comparing age)’
      In the expression: modify sortBy (comparing age) personVec
      In an equation for ‘sortedByAge’:
          sortedByAge = modify sortBy (comparing age) personVec
   |
23 | sortedByAge  = modify sortBy (comparing age) personVec
   |                               ^^^^^^^^^^^^^
Failed, no modules loaded.
参数的参数

modify
函数接受两个参数,但由于缺少括号,您传递了三个参数:

sortedByAge = modify (sortBy (comparing age)) personVec
类型:这是向量还是函数?

您的
sortedbage
变量的命名意味着它是一个向量,但您提供的类型表明它是一个计算向量的函数。让我们改变这种类型:

sortedByAge :: Vector Person
sortedByName
也是如此

结果

*Main> sortedByName
[Person {name = "Alice", age = 30},Person {name = "Bob", age = 20},Person {name = "Charles", age = 10}]
*Main> sortedByAge
[Person {name = "Charles", age = 10},Person {name = "Bob", age = 20},Person {name = "Alice", age = 30}]

您的类型声明对于
personVec
sortedByName
sortedByAge
似乎是错误的。请编辑您的问题以包含所有编译器错误。您是否知道
personVec
不是
Vector人
,而是
[Person]
(链接列表)?@ThomasM.DuBuisson谢谢。我已经编辑了它您缺少括号-它应该是
modify(sortBy(比较年龄))myVec
。看起来您的类型声明对于
personVec
sortedByName
sortedByAge
是错误的。请编辑您的问题以包含所有编译器错误。您是否知道
personVec
不是
Vector人
,而是
[Person]
(链接列表)?@ThomasM.DuBuisson谢谢。我已经编辑过了,您缺少括号-应该是
modify(sortBy(比较年龄))myVec
。谢谢您的回答。它帮助了我很多,也向我展示了如何更好地表达我的问题!谢谢你的回答。它帮助了我很多,也向我展示了如何更好地表达我的问题!