Upgrade to Pro — share decks privately, control downloads, hide ads and more …

コードで遊ぼう Haskell #2.1

YooWaan
November 25, 2015

コードで遊ぼう Haskell #2.1

YooWaan

November 25, 2015
Tweet

More Decks by YooWaan

Other Decks in Programming

Transcript

  1. ؀ڥ ❖ ݴޠ ❖ Haskell
 The Glorious Glasgow Haskell Compilation

    System, version 7.10.2 ❖ Package؅ཧ ❖ cabal
 cabal-install version 1.22.6.0
 using version 1.22.4.0 of the Cabal library
  2. ❖ Package؅ཧ ❖ stack version 0.1.6.0 ❖ cabal hell ճආ

    ❖ ෳ਺όʔδϣϯghcڞଘ ❖ ෳ਺cabalύοέʔδ
 ͷϓϩδΣΫτϏϧυՄೳ
  3. ։ൃ؀ڥ ❖ ։ൃσΟϨΫτϦ ɹHaskell Πϯετʔϧ $ brew install ghc
 $

    yum install haskell
 $ apt-get install haskell-platform 
 ɹύοέʔδ؅ཧπʔϧΠϯετʔϧ $ brew intall cabal $ brew install haskell-stack (লུ)
  4. ։ൃ؀ڥ ❖ cabal $ cabal init $ cabal configure $

    cabal build $ cabal run $ cabal test $ cabal install $ stack new <project name> $ cd <project name> $ stack build $ stack exec <app name> $ stack test $ stack install ❖ stack
  5. ͳΜ͔ྑ͛͞Λମݧʂʁ ੔਺ͷϦετ or ഑ྻΛೖྗͯ͠ɺ͔̌Β਺্͑͛ͯ n൪໨ͷཁૉʹ͸ n Λֻ͚ɺ͢΂ͯΛ଍͠߹ΘͤΔॲཧΛॻ͍ͯΈΑ͏ ͭ·Γ ೖྗ [

    10, 20, 30, 40 , 50 ] ͷ͜ΕΛ ͜͏͍͏෩ʹܭࢉ (10 × 0) + (20 × 1) + (30 × 2) + (40 × 3) + (50 × 4) = ?
  6. ͳΜ͔ྑ͛͞::໋ྩΛॻ͍ͯ͘৔߹ def func(ary) sum = 0 i = 0 ary.each{|x|

    sum += x * i i += 1 } return sum end puts func([ 10,20,30,40,50 ]) ❖ ruby def func(ary): sum = 0 for i, v in enumerate(ary): sum += i * v return sum print func([10,20,30,40,50]) ❖ python ഁյత୅ೖͷੈք
  7. ͳΜ͔ྑ͛͞::Haskell ❖ Haskell func :: [Int] -> Int func list

    = foldl (+) 0 -- ৞ΈࠐΈؔ਺ $ map( \(i,x) -> x*i ) -- Ϛοϓؔ਺ $ zip [0..] list -- ೋͭͷϦετΛϖΞʹ main = print (func([10,20,30,40,50])) map , reduce ؔ਺߹੒
  8. ͳΜ͔ྑ͛͞::pythonͰॻ͘ͱ ❖ python # -*- coding: utf-8 -*- # def

    func(ary): return reduce( lambda a,b : a+b , map( lambda (i,x) : i*x , zip( range(0,len(ary), 1), ary) )) print func([ 10, 20, 30, 40, 50 ])
  9. ͜͜·ͰͰ ❖ ഁյత୅ೖ͕ͳ͍ ❖ ७ਮ ❖ λΠϓྔ΋গͳ͍ ❖ h( g(

    f() ) ) ΑΓ h ◦ g ◦ f ͳײ͕͍͍͡ΑͶ ͍͍͡ΌΜʂʂ
  10. ྫʣࣗ༝࠷খ਺ɿ໰୊ ༩͑ΒΕͨࣗવ਺ͷ༗ݶू߹ X ʹؚ·Εͳ͍࠷খͷࣗવ਺Λ໭Δ໰୊ ྫɿ [ 08, 23, 09, 00,

    12, 11, 01, 10, 13, 07, 41, 04, 14, 21, 05, 17, 03, 19, 02, 06 ] ʹؚ·Εͳ͍࠷খͷࣗવ਺ΛͲ͏΍ͬͯ୳͔͢ʁ ಄͍͍෩ຯ: ೚ҙͷϦετͷ੔ྻ͸ઢܗ࣌ؒͰ͸Ͱ͖ͳ͍ɻʹ΋͔͔ΘΒͣ͜ͷ໰୊͸ઢܗ࣌ؒͰͷղ๏͕͍ͭ͘΋ଘࡏ͠·͢
  11. ྫʣࣗ༝࠷খ਺ɿղ๏ [1] ୯७ʹର৅ͷ഑ྻͷ਺ࣈΛऔΓআ ͍ͯɺઌ಄ͷ਺ࣈΛฦ͢ [2] 0͔Βn·Ͱͷ n+1ݸͷεϩοτΛ ࣋ͭ࿦ཧ஋ͷ഑ྻΛ࡞੒ͯ͠ɺ͜ͷ ഑ྻΛԼ͔Βݟͯ࠷ॳʹFalseʹͳΔ
 [1]

    min1st :: [Int] -> Int min1st xs = head ( [0 ..] \\ xs ) [2] min2nd :: [Int] -> Int min2nd = search . checklist search :: Array Int Bool -> Int search = length . takeWhile id . elems checklist :: [Int] -> Array Int Bool checklist xs = accumArray (||) False (0,n) (zip (filter (<= n) xs) (repeat True)) where n = length xs
  12. ྫʣࣗ༝࠷খ਺ɿղ๏ [3] ෼ׂ౷࣏๏ [3] minfree :: [Int] -> Int minfree

    xs = minform 0 (length xs, xs) minform :: Int -> (Int, [Int]) -> Int minform a (n,xs) | n == 0 = a | m == b - a = minform b (n-m, vs) | otherwise = minform a (m, us) where (us,vs) = partition (< b) xs b = a + 1 + n `div` 2 m = length us
  13. ߟ࡯ ❖ pros ❖ ίʔυ͕୹͍ ❖ ΞϧΰϦζϜ࣮૷͕༰қ
 (࿦ূ͠΍͍͢) ❖ ෳࡶͳࣄ͕؆୯ʹͰ͖Δ

    ❖ ੜ࢈ੑ޲্ ❖ όάͷࠞೖ͕গͳ͍ ❖ cons ❖ ෑډ͕Ϋιߴ͍ ❖ ։ൃ/อकऀ গͳ͍ ❖ νʔϜߏங͕ࠔ೉ ❖ LL ͱ͔ͳΜ͔΋͏໋ྩܕͰ͍͍ ΑͶʁ
  14. ࢲͳΓͷ݁࿦ ❖ ศར͚ͩͲ ❖ ֶशίετ͕ߴ͍ ❖ ࢖͏ͷ͕೉͍͠
 ɹ㱺 ྲྀߦΓ͖Βͳ͍ͱࢥͬͯ͠·͏ ❖

    Լهͷ΋ͷ͕χϡΞϯε͕ࣅ͍ͯΔͱࢥ͏ ❖ SOAP ❖ xhtml ศར ΤϯδχΞ ؔ਺ϓϩάϥϛϯά Τοηϯε ݍ࿦ น