Drkcore

25 12 2009 Haskell Python Tweet

HaskellのsplitAt

HaskellのsplitAtはなんでタプルを返すんだろうか?

Prelude> :t splitAt
splitAt :: Int -> [a] -> ([a], [a])
Prelude> splitAt 3 [1,2,3,4,5]
([1,2,3],[4,5])

pythonで同じようなのを書いてみる。

>>> def splitAt (n,xs): return [xs[:n],xs[n:]]
... 
>>> splitAt(3,[1,2,3,4,5])
[[1, 2, 3], [4, 5]]
>>> def splitAt (n,xs): return (xs[:n],xs[n:])
... 
>>> splitAt(3,[1,2,3,4,5])
([1, 2, 3], [4, 5])

なんというか[[1, 2, 3], [4, 5]]のほうが見た目よろしい気がするのです。

追記09.12.25

  • @ringtaro
  • @mzp

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021