如果不使用drop,如何在列表中获取和打印备用行(奇数或偶数)?

Yuu*_*Yuu 1 haskell list

我在这里使用drop编写了一个工作版本:

main = do cs <- getContents
          putStr $ unlines $ oddL $ lines cs

oddL :: [a] -> [a]
oddL [] = []
oddL (x:xs) = x : (oddL $ drop 1 xs)
Run Code Online (Sandbox Code Playgroud)

但是我想知道是否有办法在没有掉线的情况下做到这一点?即使它不那么有效.

Zet*_*eta 8

oddL :: [a] -> [a]
oddL (x:_:xs) = x : oddL xs -- "forget" the even element
oddL [x]      = [x]
oddL  _       = [ ]
Run Code Online (Sandbox Code Playgroud)


dfe*_*uer 5

提示:更换x:xs带有图案[x]样式和x1 : x2 : xs图案.