我要说的是:
type Key = String
type Score = Int
data Thing = Thing Key Score
And if I have an array of them like this:
[Thing "a" 7, Thing "b" 5, Thing "a" 10]
是否有减少这种情况的标准方法,以便我不会有任何重复的钥匙? 如果两个关键点相匹配,我想取更好的分数。
[Thing "b" 5, Thing "a" 10]
我要说的是:
type Key = String
type Score = Int
data Thing = Thing Key Score
And if I have an array of them like this:
[Thing "a" 7, Thing "b" 5, Thing "a" 10]
是否有减少这种情况的标准方法,以便我不会有任何重复的钥匙? 如果两个关键点相匹配,我想取更好的分数。
[Thing "b" 5, Thing "a" 10]
从根本上说,我们必须首先决定问题解决和执行工作的困难。 因此,如果我们首先通过<代码>S分进行分类,然后在分类名单上保留第一个与<代码”有关的编号。 关键 这应当奏效,请看一看执行情况:
import Data.List
import Data.Function
type Key = String
type Score = Int
data Thing = Thing { key :: Key, score :: Score }
deriving (Show)
myNub = nubBy ((==) `on` key)
mySort = sortBy (compare `on` (negate . score))
selectFinest = myNub . mySort
我们现在尝试在<代码>上这样做。 缩略语
Prelude> :load Test.hs
[1 of 1] Compiling Main ( Test.hs, interpreted )
Ok, modules loaded: Main.
*Main> selectFinest [Thing "a" 7, Thing "b" 5, Thing "a" 10]
[Thing {key = "a", score = 10},Thing {key = "b", score = 5}]
查询http://haskell.org/hoogle/rel=“noreferer”>gle 如果你对我在解决办法中使用的职能不确定。 学习如何使用<条码><<>>>>>>和这些功能确实需要一些时间。
一种非常简单的解决办法是使用
Prelude Data.Map> fromListWith max [("a", 7), ("b", 5), ("a", 10)]
fromList [("a",10),("b",5)]
请注意,这预示着les,以便在必要时转换。 此外,它并不维护投入内容的顺序。 运行时间为O(nlog n)。
I m 寄出一个O(nlog n)解决方案,因为每个人都似乎因O(n^2)而被罚款。
consolidate :: (Ord a, Ord b) => [Thing a b] -> [Thing a b]
consolidate xs =
max_from_each_group (sortBy (compare `on` getKey) xs)
where
max_from_each_group [] = []
max_from_each_group (x:xs) =
let (same_key, rest) = span ( -> x == getKey t) xs in
let group_max = maximumBy (compare `on` getValue) (x:same_key) in
group_max : max_from_each_group rest
Here is my feeble attempt. There surely is a nicer way but I m not much of a Haskell programmer.
import Data.List
type Key = String
type Score = Int
data Thing = Thing Key Score
deriving (Show, Ord)
instance Eq Thing where
(Thing k1 _) == (Thing k2 _) = k1 == k2
(Thing k1 _) /= (Thing k2 _) = k1 /= k2
thingSort :: [Thing] -> [Thing]
thingSort = Data.List.sortBy (flip compare)
ex = [Thing "a" 7, Thing "b" 5, Thing "a" 10]
filtered = nub (thingSort ex)
I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...
Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....
How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...
I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...
I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...
The type of the Prelude function flip is: flip :: (a -> b -> c) -> b -> a -> c I.e., it takes one binary function and two arguments. The type of the Prelude function id is: id :: a -...
group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...
I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?