Code: Alles auswählen
slice :: [a] -> Int -> Int -> Int -> [a]
slice x start end step = _slice (drop start (take end x)) [] step
where
_slice :: [a] -> [a] -> Int -> [a]
_slice [] ret step = ret
_slice (x:xs) ret step =
let x' = drop (step-1) xs
in _slice x' (ret ++ [x]) step
main :: IO()
main = print $ slice [0,1,2,3,4,5,6,7,8,9] 1 8 5
Die main Funktion ist äquivalent zu diesem Python Schnipsel:
Code: Alles auswählen
print [0,1,2,3,4,5,6,7,8,9][1:8:5]
Alternativ/Verbesserungsvorschläge willkommen.
Seit neuer Forumsoftware jetzt auch mit Syntaxhighlighting für Haskell.