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

内包表記(仮)

 内包表記(仮)

Tomohiko Himura

November 02, 2014
Tweet

More Decks by Tomohiko Himura

Other Decks in Programming

Transcript

  1. Python [(x,y) for x in range(1,10) for y in range(1,10)

    if x * y == 24 ] [(3, 8), (4, 6), (6, 4), (8, 3)]
  2. ۩ମྫ 10ະຬͷਖ਼ͷح਺ ֎Ԇ { 1, 3, 5, 7, 9} ಺แ

    { x | x ͸ 10ະຬͷਖ਼ͷح਺ } { x | x ∈ ℕ, x < 10, x % 2 = 1 }
  3. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  4. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  5. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  6. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  7. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  8. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)]
  9. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ
  10. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ
  11. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ
  12. ௚ੵ 81ݸͷϦετ [(x,y) | x <- [1..9], y <- [1..9]]

    [(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9), (2,1),(2,2),(2,3),(2,4),(2,5),(2,6),(2,7),(2,8),(2,9), (3,1),(3,2),(3,3),(3,4),(3,5),(3,6),(3,7),(3,8),(3,9), (4,1),(4,2),(4,3),(4,4),(4,5),(4,6),(4,7),(4,8),(4,9), (5,1),(5,2),(5,3),(5,4),(5,5),(5,6),(5,7),(5,8),(5,9), (6,1),(6,2),(6,3),(6,4),(6,5),(6,6),(6,7),(6,8),(6,9), (7,1),(7,2),(7,3),(7,4),(7,5),(7,6),(7,7),(7,8),(7,9), (8,1),(8,2),(8,3),(8,4),(8,5),(8,6),(8,7),(8,8),(8,9), (9,1),(9,2),(9,3),(9,4),(9,5),(9,6),(9,7),(9,8),(9,9)]
  13. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ ʹͳΔ΋ͷ͚ͩʹ͢Δ
  14. Python [(x,y) for x in range(1,10) for y in range(1,10)

    if x * y == 24 ] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ ʹͳΔ΋ͷ͚ͩʹ͢Δ
  15. –Python νϡʔτϦΞϧ “Ϧετͷ಺แදه (list comprehension) ͸ɺ map() ΍ filter() ΍

    lambda Λ࢖ΘͣʹϦετ Λੜ੒͢ΔͨΊͷ؆ܿͳํ๏Λఏڙ͍ͯ͠· ͢ɻ಺แදهʹΑΔϦετͷఆٛ͸ɺ͍ͨͯ ͍ map(), filter(), lambda Λ࢖ͬͯϦετΛੜ ੒͢ΔίʔυΑΓ΋໌շʹͳΓ·͢ɻ”
  16. Ruby [*1..9].map do |x| [*1..9].map do |y| [*1..9].map do |z|

    [x,y,z] end end end.flatten(2).select do |x,y,z| x * y * z == 24 end
  17. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] TFMFDU GSPN XIFSF
  18. LIMIT :set -XTransformListComp [ (x,y) | x <- [1..9], y

    <- [1..9], x * y == 24, then take 2] [(3, 8), (4, 6)]
  19. ORDER :set -XTransformListComp :m GHC.Exts [ (x,y) | x <-

    [1..9], y <- [1..9], x * y == 24, then sortWith by y] [(8, 3) (6, 4), (4, 6), (3, 8)]
  20. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) [(3, 8), (4, 6), (6, 4), (8, 3)]
  21. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  22. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) GSPN TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  23. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) XIFSF GSPN TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  24. Maybe do x <- Just 3 y <- Just 8

    guard (x * y == 24) return (x,y) Just (3,8)
  25. Maybe do x <- Just 3 y <- Just 8

    guard (x * y == 25) return (x,y) Nothing