【发布时间】:2019-03-11 17:00:21
【问题描述】:
给定一个整数数组,我如何对它们进行排序:最大-最小-第二大-第二小?
我正在尝试: - 按升序和降序复制和排序数组 - 通过挑选如上所述的每个数组的头部来创建排序数组
我不熟悉 Haskell 编程,但这就是我所做的
sort :: [Int] -> [Int]
sort [] = []
sort (x:xs) = sort smaller ++ [x] ++ sort larger
where
smaller = filter (<= x) xs
larger = filter (> x) xs
wave :: [Int] -> [Int]
wave [] = []
wave [x] = [x]
wave (x:xs) = if length (x:xs)>1
then :
ascending = sort (x:xs)
descending = reverse (ascending)
iterator = length(x:xs)
if iterator > 0 && (length(ascending)==0 || length(descending)==0)
then do wave(x:xs) = head(descending) + head(ascending)
tail(descending)
tail(ascending)
提前感谢您的帮助
【问题讨论】:
-
then :是错误的语法。:不属于那里。如果您有多个定义,可以将它们放在一个let中,如let { a = ... ; b = ... ; c = ... } in ...。