Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:27:21+00:00 2026-05-24T05:27:21+00:00

I defined the left / right methods for stream functions (SF) of the ArrowChoice

  • 0

I defined the left/right methods for stream functions (SF) of the ArrowChoice class as follows:
newtype SF a b = SF { runSF :: [a] -> [b] }

instance ArrowChoice SF where
  left (SF f) =
   SF $ map (either (\x -> Left . head $ f [x]) Right)
  right (SF f) =
   SF $ map (either Left (\x -> Right . head $ f [x]))

A few tests in ghci makes it seem like everything is fine:

λ> let lst = [Left 'c', Right 2, Left 'a', Right 3, Left 't']
λ> let foo = SF $ map toUpper
λ> let bar = SF $ map (+1)
λ> runSF (left foo) lst
[Left 'C',Right 2,Left 'A',Right 3,Left 'T']
λ> runSF (right bar) lst
[Left 'c',Right 3,Left 'a',Right 4,Left 't']

But using it with mapA says otherwise:

λ> let delay x = SF $ init . (x:)
λ> runSF (mapA (delay 0)) [[1,2,3],[4,5,6],[7,8,9]]
[[0,0,0],[0,0,0],[0,0,0]]

The correct answer should be:

[[0,0,0],[1,2,3],[4,5,6]]

where mapA is defined as:

mapA :: ArrowChoice arr => arr a b -> arr [a] [b]
mapA f = arr listcase >>>
         (arr (const []) ||| (f *** mapA f >>> arr (uncurry (:))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:27:22+00:00Added an answer on May 24, 2026 at 5:27 am

    I don’t think your ArrowChoice instance is correct.

    Note that the delay function takes a stream and replaces the first element; critically, it doesn’t treat all elements identically. Now, consider your definition of Left:

    left (SF f) = SF $ map (either (\x -> Left . head $ f [x]) Right)
    

    Observe that f is the guts of an entire stream function, and thus may behave differently depending on stream position. The left function then creates a new stream function that maps a homogeneous function over its stream, where each element is either passed through (for Rights) or lifted to a singleton list on which the input stream function is run.

    Rather than delay, consider instead the following function:

    skip = SF $ drop 1
    

    This drops the first element of a stream entirely, and since left runs its input independently each time on a singleton list, this would filter out all Lefts entirely! Probably not what you want.

    Rather, you need to do something like partition the stream into its Left and Right components, apply the input stream function to the entire stream of Lefts, then merge the lefts and rights back together in the same sequence they were in originally.

    I get the impression that you’re doing this as some sort of exercise so I won’t spoil the fun by simply writing the solution out in full. But I will say that my code indeed gives [[0,0,0],[1,2,3],[4,5,6]] on your example.


    If you do want to see my test code, it’s below. (shh, it’s hiding)

     instance ArrowChoice SF where
         left (SF f) = SF $ \xs -> let (ls, rs) = partitionEithers xs 
                                   in merge xs (f ls) rs
     
     merge (Left  _:xs) (l:ls) rs = Left l  : merge xs ls rs
     merge (Right _:xs) ls (r:rs) = Right r : merge xs ls rs
     merge _ _ _ = []
     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Expressions like ls map (_ + 1) sum are lovely because they are left-to-right
I asked a question earlier about why left joins in Linq can't use defined
User-Defined Functions & Collating Sequences Full support for user-defined functions and collating sequences means
I defined next class with virtual properties: public class Order: BaseEPharmObject { public Order()
I have a WCF service defined as follows: Imports System.ServiceModel Imports System.ServiceModel.Web <ServiceContract()> Public
I defined a record named log . I want to create an mnesia table
I defined a Controller to force authentication by using the [Authorize] attribute. When a
I defined an handler for EVT_IDLE that does a certain background task for me.
ive defined the following and filled it with elements: vector <vector<double> > my_vector; but
I defined the Scheme procedure to return another procedure with 2 parameters : (define

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.