tags: ðProgLang
Haskell
çŽç²é¢æ°åèšèª.
ææ³
æšæºã©ã€ãã©ãª
åãããªããšãã¯, Hoogle ã§èª¿ã¹ã. source ã¿ããæŒãã°å®çŸ©ãèŠãã.
ããã Haskell ã楜ããåŠãŒãããªã³ã©ã€ã³ã§ç¡æã§èªãã.
å°ãå€ã 98 ä»æ§
åºç€ããŸãšãŸã£ãŠãã.
List
Prelude> head[1,2,3,4,5]
1
Prelude> tail [1,2,3,4,5]
[2,3,4,5]
Prelude> [1,2,3,4,5] !! 2
3
Prelude> take 3 [1,2,3,4,5]
[1,2,3]
Prelude> drop 3 [1,2,3,4,5]
[4,5]
Prelude> length [1,2,3,4,5]
5
Prelude> sum [1,2,3,4,5]
15
Prelude> [1,2,3] ++ [4,5]
[1,2,3,4,5]
Prelude> reverse[1,2,3,4,5]
[5,4,3,2,1]
Monad
cabal update
cabal install mtl
IO Action
Haskell ã§ããã°ã©ãã³ã°ãããšãã«å¯äœçšããããããªåŠç, ãããã¯å€éšã«å¯ŸããŠåãããããããªåŠçãæžããããšã㯠ã¢ã¯ã·ã§ã³ã䜿ããŸã.
ãªã«ãè¿ããªãå Žåã¯, () ãè¿ã.(ããŒã¯ã³ãšãã)
Haskell ã§ã¯,ã¢ããã¯
-
åæ§ç¯åæ £ç¿çã«, m ãšåŒã°ããŠãã.
-
ãã®åã®å€ãæ§ç¯ããé¢æ° (a -> m a), æ £ç¿çã«,return
-
ãã®åã®å€ãšãã®åã®å€ãçæããèšç®ãšãçµåããŠ, ãã®åã®æ°ããèšç®ã çæããé¢æ° (m a -> (a -> m b) -> m b) bind ãšãã. >>= ãšãã.
ã§è¡šçŸãããŸã.
- ã¢ããã®åæ§ç¯åã¯èšç®ã®åãå®çŸ©
- return é¢æ°ã¯ãã®èšç®ã®åã®ããªããã£ããªå€ãçæ
-
= ã¯ãã®åã®èšç®ãçµã¿åããã
ãã®åã®ããè€éãªèšç®ãäœãåºããŸã
-- ã¢ãã m ã®å data m a = ... -- return ã¯ã¢ããã®ã€ã³ã¹ã¿ã³ã¹ãäœãåæ§ç¯å return :: a -> m a -- bind ã¯ã¢ããã®ã€ã³ã¹ã¿ã³ã¹ m a ãš, -- a ããå¥ã®ã¢ããã®ã€ã³ã¹ã¿ã³ã¹ m b ãäœãèšç®ãš -- ãçµã¿åãããŠæ°ããã¢ããã®ã€ã³ã¹ã¿ã³ã¹ m b ãäœã (>>=) :: m a -> (a -> m b) -> m b
do èšæ³
main = do
s <- getLine
print s
- <- æŒç®åã§, å€ã« IO ã®çµæã æçž (bind) ã§ãã.
-
Haskell ãäžæµã«ããéæ³ãã¡ - Google ã¹ã©ã€ã
do ãããã¯ã¯éåžžã®é¢æ°ãšç°ãªãã®ã§ã¢ã¯ã·ã§ã³ãšåŒã°ããŸã. do ãããã¯ããå€éšã«å€ãè¿ãã«ã¯ return ãçšããŸã. return a ãšãããšå€éšã«ã¯ IO a ãè¿ããŸã
do ãããã¯ã®çµæ㯠do ãããã¯ã®æåŸã®ã¢ã¯ã·ã§ã³ã®çµæãšãªããŸã. return 㧠do ãããã¯ã®çµæ (ã¢ã¯ã·ã§ã³) ãå€éšã«è¿ãããšãã§ããŸã.
IO a ã a ã«æããã«ã¯, do ãããã¯ã®äžã§ <- ã䜿ããŸã
Bookmarks
Ruby ã§ã®è§£èª¬:
- ã¢ãããšããèšèã䜿ãããšãªã çŽç²é¢æ°åèšèªã®å ¥åºåã解説ãã
- Ruby ã«ããé¢æ°åããã°ã©ãã³ã°
Haskell ã«ãããã¢ããã®è§£èª¬ããŒãž:
- All About Monads
- Easy Monad
- Haskell ã® sequence é¢æ° - foldr ãã€ã¡ãŒãžã㊠| ããã«å¿ããè³ã¿ãã®ããã®ã¡ã¢
- Haskell ã® mapM_ â foldr ãš (>>) ãæèã㊠| ããã«å¿ããè³ã¿ãã®ããã®ã¡ã¢
edX ã®ããŒãž:
Tips
n+k Pattern
- ãæäžã§å©çšããã«ã¯, ãœãŒã¹ã«ä»¥äžãå ¥ãã.
{-# LANGUAGE NPlusKPatterns #-}
or
$ ghci -XNPlusKPatterns
ããŒã«
Haskell Platform
Haskell éçºçšã®ç°å¢.
ArchLinux ã«ã¯, haskell platform ããªã??
- Haskell - ArchWiki
- ArchLinux Haskell ã®éçºç°å¢ãæ§ç¯ãã - çœåžœåç 究宀
- æè¡è èŠç¿ãã®ç¬ãèš: ArchLinux 㧠Haskell Platform!
AUR ã® haskell-platform ãå©çšããã®ã§ã¯ãªããŠä»¥äžãããã«ã€ã³ã¹ããŒã«ããã®ããããš, Arch Wiki ã«ã¯æžããŠãã.
- ghc (ghc) - The compiler
- cabal-install (cabal-install) - A command line interface for Cabal and Hackage
- haddock (haddock) - Tools for generating documentation
- happy (happy) - Parser generator
- alex (alex) - Lexical analyzer generator
% sudo pacman -S ghc cabal-install haddock happy alex
ghc
Haskell ã³ã³ãã€ã©.
% sudo pacman -S ghc
ghci
Haskell ã®ã³ãã³ãã©ã€ã³ã€ã³ã¿ããªã¿.
# èµ·å
ghci test.hs
# åèªã¿èŸŒã¿
> :reload
cabal
ããã±ãŒãžãããŒãžã£ãŒ.
ãã¹ã
ã¡ãžã£ãŒãªãã®ã¯ä»¥äž
- doctest
- QuickCheck
- HSpec
- HUnit
doctest
ã³ã¡ã³ãã«ãã¹ããæžãã¹ã¿ã€ã«ã®ããŒã«.
Python ã® doctest ã haskell ã«ç§»æ€ãããã®ã ãšã.
QuickCheck
ã©ã³ãã ãªãã¹ãããŒã¿ã«ãã£ãŠé¢æ°ã®æ§è³ªããã¹ããã.
xUnit ãšã¯ç°ãªãã³ã³ã»ããããã€.
HSpec
xSpec ã©ã€ã¯ãªãã¹ãããŒã«.
Ruby ã® RSpec ã«ã€ã³ã¹ãã€ã€ãããããã.
èšæ³ã BDD ç.
HUnit
xUnit ã©ã€ã¯ãªãã¹ãããŒã«. JUnit ã©ã€ã¯.
- HUnit: A unit testing framework for Haskell | Hackage
- Haskell - HUnit è¶ å ¥é - Qiita
- Haskell ã® UnitTest, HUnit ã«ã€ããŠåŠã¶ - ãšã³ãžãã¢ã®ãœãããŠã§ã¢çææ
Install
$ cabal install HUnit
Usage
Test.HUnit ãã€ã³ããŒã.
import Test.HUnit
ãã¹ã察象ã³ãŒã
import Data.List
import Data.Char
import Unsafe.Coerce
data Nat = Zero
| Succ Nat
deriving Show
natToInteger (Succ n) = natToInteger n + 1
natToInteger Zero = 0
ãã¹ãã³ãŒã
èšæ³ã¯ä»¥äž.
tests = TestList
[ "natToInteger 1" ~: natToInteger Zero ~?= 0
, "natToInteger 2" ~: natToInteger (Succ Zero) ~?= 1
, "natToInteger 3" ~: natToInteger (Succ (Succ Zero)) ~?= 2
]
h = tests
ãã¹ãå®è¡
runTestTT (ãã¹ãé¢æ°å) ã§ãã¹ãå®è¡.
$ runTestTT tests
Cases: 3 Tried: 3 Errors: 0 Failures: 0
Counts {cases = 3, tried = 3, errors = 0, failures = 0}
ãããšå€±æãããŠã¿ã.
*Main> runTestTT tests
### Failure in: 2:natToInteger 3
expected: 1
but got: 2
Cases: 3 Tried: 3 Errors: 0 Failures: 1
Counts {cases = 3, tried = 3, errors = 0, failures = 1}
Tips
ç¬èªã«åãå®çŸ©ãããšãã¯, Eq ãç¶æ¿ãã.
data Nat = Zero
| Succ Nat
deriving (Show, Eq)
Topics
- ð§TidalCycles: live coding.
ðŽðŒErik Meijer
ã¢ããã·ã£ãã®æå¿«ãªå€ãªãããã, Haskell Hacker.
Haskell 䜿ã
- ðcoursera 㧠Principles of Reactive Programming ãåãã | Futurismo
- ðHaskell 㧠é¢æ°åèšèªå ¥é! edX ã® Introduction to Functinal Programming ãåãã | Futurismo
- ðHaskell
As a Facebook Developer
ããããFacebookã®ç€Ÿèšã§ããðThe Hacker Wayã«å€§ããªåœ±é¿ãäžããŠãã.
Haskel with Emacs
ãããã®èšäºãæ€çŽ¢äžäœã ãšæ«æ³ã ãªâŠ
ðReferences
Haskell ãæããè¥è ãã¡ãž
ã翻蚳ã Haskell ãæããè¥è ãã¡ãž | POSTD
人ããç¥çã§å€ãã®ç¥èãæã£ãŠãããšå®æããããšã, ãŸãäž»æµãªèšèªã«äžç³æããããšã§, èªåãããããªèšèªã«å·çããŠããã®ã¯ééãã§ã¯ãªããšãã確蚌ãåŸãŠå®å¿ãããã®ã§ãããã?
Haskell ã䜿ãããšã§ã¹ããŒããªããšãã§ããŸãã, ã ãããšãã£ãŠãããè³¢ã人ã®ããã®èšèªãšããããã§ã¯ãããŸãã. ãã®èšèªã¯ã·ã³ãã«ããšã¹ããŒããã䜵ãæã€èšèªã§ãã, ç§ãã¡ã¯æã«ã·ã³ãã«ããæ±ã, æã«ã¯ã¹ããŒãããæ±ããŸã. ã§ãç§ãã¡ã¯è³¢ãããããšã«ç¹å¥ã«éãã眮ããŠã¯ããŸãã.