File: proc-do-complex.hs

package info (click to toggle)
haskell-ormolu 0.1.2.0-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,392 kB
  • sloc: haskell: 11,134; makefile: 7
file content (49 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE Arrows #-}
foo
  f
  g
  h
  ma = proc (
        (a, b),
        (c, d),
        (e, f)
      ) ->
    do -- Begin do
        (x,y) -- GHC parser fails if layed out over multiple lines
         <- f -- Call into f
              (a,
               c) -- Tuple together arguments
              (b,
               d)
         -< (b + 1, -- Funnel into arrow
             d * b)
        if
          x `mod` y == 0 -- Basic condition
        then case e -- Only left case is relevant
            of Left (z,
                     w) -> \u -> -- Procs can have lambdas
                let
                    v
                      = u -- Actually never used
                        ^ 2
                in
                    (returnA
                      -< -- Just do the calculation
                     (x + y * z))
        else do
          let u = x -- Let bindings bind expressions, not commands
          -- Could pattern match directly on x
          i <- case u of
            0 -> (g . h -< u)
            n -> (
                  (h . g
                   -< y) -- First actual use of y
                  )
          returnA -< ()
          -- Sometimes execute effects
          if i > 0
            then ma -< ()
            else returnA -< ()
          returnA -< (i +
                      x *
                      y) -- Just do the calculation