Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Arrow tutorial
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== An example == Now let's build something using our simple arrow definition and some of the tools we've just created. We start with two simple arrow values, <code>f</code> and <code>g</code>: * <code>f</code> halves its input: <haskell> f :: SimpleFunc Int Int f = arr (`div` 2) </haskell> * and <code>g</code> triples its input and adds one: <haskell> g :: SimpleFunc Int Int g = arr (\x -> x*3 + 1) </haskell> We can combine these together using <code>liftA2</code>: <haskell> h :: SimpleFunc Int Int h = liftA2 (+) f g hOutput :: Int hOutput = runF h 8 </haskell> What is <code>h</code>? How does it work? The process defined by <code>h</code> is <code>split >>> first f >>> second g >>> unsplit (+)</code>. Let's work through an application of <code>h</code> to the value <code>8</code>: :{| |<code>8</code> |β |<code>(8, 8)</code> |<code>split</code> |- |<code>(8, 8)</code> |β |<code>(4, 8)</code> |<code>first f</code> β <code>x `div` 2</code>, where <code>x</code> is the first component of the pair |- |<code>(4, 8)</code> |β |<code>(4, 25)</code> |<code>second g</code> β <code>3*y + 1</code>, where <code>y</code> is the second component of the pair |- |<code>(4, 25)</code> |β |<code>29</code> |apply <code>(+)</code> to the components of the pair |} :::{| | f β β 8 β (split) (unsplit (+)) β 29 β β g |} We can see that <code>h</code> is a new arrow value that, when applied to <code>8</code>, will apply both <code>f</code> and <code>g</code> to <code>8</code>, then adds their results. A lot of juggling occurred to get the plumbing right since <code>h</code> wasn't defined as a linear combination of arrow values. GHC has a syntactic notation that simplifies this in a similar way to how <code>do</code>-notation simplifies monadic computations. The <code>h</code> function can then be defined as: <haskell> h' :: SimpleFunc Int Int h' = proc x -> do fx <- f -< x gx <- g -< x returnA -< (fx + gx) hOutput' :: Int hOutput' = runF h' 8 </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width