すごいhaskell読書会 in 大阪 #4 「第6章 モジュール」

29
すごいHaskellたのしく学ぼう! 6. モジュール 伊勢 シン / 伊藤 伸裕 すごいHaskell読書会 in #4 @ Fenrir Inc.

Upload: shin-ise

Post on 31-May-2015

1.347 views

Category:

Technology


4 download

TRANSCRIPT

  • 1. Haskell! 6. / Haskell in #4 @ Fenrir Inc.

2. / @iseebi / id:iseebi CAD C# Ruby Objective-C + Java PHPer 3. https://gist.github.com/4509540 4. () ghci:load:load [*] ... load module(s) and their dependents 5. ghci $ ghciGHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Prelude> package gem (cabal) Module Package OK 6. Haskell Prelude 5 Hoogle 7. import GHCi :m import import Prelude> import Data.ListPrelude Data.List> :m :load :load :m () importOK 8. import Prelude> import Data.List (nub, sort) Prelude> import Data.List hiding (nub) () Prelude> import qualified Data.MapData.Map.filter Prelude> import qualified Data.Map as MM.filter . qualified 9. Prelude> import Data.MapPrelude Data.Map> null "a":1:1:Ambiguous occurrence `nullIt could refer to either `Prelude.null, imported from Preludeor `Data.Map.null, imported from Data.Map 10. (1) (Data.List.words) Prelude Data.List> words "hey thease are the words in this sentence Prelude Data.List> words "hey theasearethe words in this sentence" words Prelude import (import) (Data.List.group) Prelude Data.List> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7] Prelude Data.List> group ["boom","bip","bip","boom","boom] (Data.List.sort) Prelude Data.List> sort [5,4,3,7,2,1] Prelude Data.List> sort ["boom","bip","bip","boom","boom] 11. module A ( words)whereimport Data.Listmodule A ( module Data.List)whereimport Data.ListQ. hiding A. 12. 3 -> -> work01.hs*Main> wordNums "wa wa wee wa wa wa"[("wa",5),("wee",1)] :edit 13. (2) tails tail isPrefixOf 21 any 14. 3 -> -> work02.hs*Main> "art" `isIn` "party"True Data.List isInfixOf 15. (3) Data.Char ord Char chr Char 16. 2 -> -> work03.hs*Main> encode 4 "Sleipnir""Wpimtrmv"*Main> decode 4 "Wpimtrmv""Sleipnir" decodeencode it GHCi 17. GHCi :set +t :set +s ~/.ghci $ cat ~/.ghci :set +t +s $ chmod 600 ~/.ghci http://www.kotha.net/ghcguide_ja/latest/ ghci.html 18. (4) 19. (5) 40 digitToInt (Data.Char) Char show 20. (5) 40 digitToInt (Data.Char) Char show 21. (5) find (Data.List) Prelude Data.List> :t findfind :: (a -> Bool) -> [a] -> Maybe a Maybe a Just 22. Maybe*Main Data.List> firstTo40Just 49999it :: Maybe Int Maybe (0.01 secs, 1581784 bytes)*Main Data.List> digitSum it:1:10:Couldnt match expected type `Int with actual type `MaybeIntIn the first argument of `digitSum, namely `itIn the expression: digitSum itIn an equation for `it: it = digitSum it(0.00 secs, 1588312 bytes) 23. Maybe Data.Maybe.fromJust*Main Data.List> import Data.Maybe(0.00 secs, 2109848 bytes)*Main Data.List Data.Maybe> firstTo40Just 49999it :: Maybe Int(0.01 secs, 2115376 bytes)*Main Data.List Data.Maybe> digitSum $ fromJust it40it :: Int(0.01 secs, 2119672 bytes) 24. work05.hs Data.Map 25. Map Data.Map import qualified Data.Map as Map fromList MapPrelude Data.Map> Map.fromList [(3,"shoes"),(4,"trees"),(9,"bees")]fromList [(3,"shoes"),(4,"trees"),(9,"bees")] work06.hs 26. Map Map.lookup Map.insert Map.size Map.map Map () 27. Map.fromList Map.fromListWith work07.hs 28. module Geometry( sphereVolume, sphereArea, cubeVolume, cubeArea, cuboidVolume, cuboidArea) where-- 29. Data.Map (Geometry/Sphere.hs) module (module Geometry.Sphere)