Skip to main content
added 26 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately,

It would be nice it we could do (x:)<$>(a,b) only, but that gives us (a,x:b) -- the Functor instance of tuples lets us act on the second element but not the first.

ButHowever, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a, b) = (x:a, b) 

It suffices to use *> which ignores f and leaves b unchanged.

((x:), 0) *> (a, b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It would also work to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using <> to do concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples lets us act on the second element but not the first.

But, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a, b) = (x:a, b) 

It suffices to use *> which ignores f and leaves b unchanged.

((x:), 0) *> (a, b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It would also work to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using <> to do concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b).

It would be nice it we could do (x:)<$>(a,b), but that gives (a,x:b) -- the Functor instance of tuples lets us act on the second element but not the first.

However, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a, b) = (x:a, b) 

It suffices to use *> which ignores f and leaves b unchanged.

((x:), 0) *> (a, b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It would also work to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using <> to do concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

added 21 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples only lets us act on the second element but not the first.

But, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a, b) = (x:a, b) 

Since we're leaving the second element unchanged, itIt suffices to ususe *> which ignores f and just leaves b unchanged.

((x:), 0) *> (a, b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It would also workswork to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using  <> to do elementwise concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples only lets us act on the second element.

But, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a,b) = (x:a, b) 

Since we're leaving the second element unchanged, it suffices to us *> which ignores f and just leaves b unchanged.

((x:), 0) *> (a,b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It also works to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using<> to do elementwise concatenate (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples lets us act on the second element but not the first.

But, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a, b) = (x:a, b) 

It suffices to use *> which ignores f and leaves b unchanged.

((x:), 0) *> (a, b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It would also work to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using  <> to do concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

added 21 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples only lets us act on the second element.

But, Applicative lets us docombine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a,b) = (x:a, b) 

Since we're leaving the second element unchanged, it suffices to us *> which ignores f and just leaves b unchanged.

((x:), 0) *> (a,b) = (x:a, b) 

The 0 could be anything -- it doesn't matter.


  It also works to use >> in place of *>. We could also use <> to do elementwise concatenate (a, b) <> (c, d) = (a++c, b++d) for 27 bytes:

26 bytes

f(x:y)=([x],[]y)<>span>>=span(/=x)y 

Try it online!Try it online!

Another weirdA alternative, this time using the Monad instance (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

2627 bytes

f(x:y)=([x],y[])>>=span<>span(/=x)y 

Try it online!Try it online!

Using<> to do elementwise concatenate (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples only lets us act on the second element.

But, Applicative lets us do

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a,b) = (x:a, b) 

Since we're leaving the second element unchanged, it suffices to us *> which ignores f and leaves b unchanged.

((x:), 0) *> (a,b) = (x:a, b) 

The 0 could be anything -- it doesn't matter.


  It also works to use >> in place of *>. We could also use <> to do elementwise concatenate (a, b) <> (c, d) = (a++c, b++d) for 27 bytes:

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Another weird alternative, this time using the Monad instance (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

Haskell, 26 bytes

f(x:y)=([x],0)*>span(/=x)y 

Try it online!

Shortens Zgarb's OG solution

f(x:y)|(a,b)<-span(/=x)y=(x:a,b) 

by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b). Unfortunately, (x:)<$>(a,b) only gives us (a,x:b) -- the Functor instance of tuples only lets us act on the second element.

But, Applicative lets us combine tuples as:

(p, f) <*> (a, b) = (p++a, f b) ([x], id) <*> (a,b) = (x:a, b) 

Since we're leaving the second element unchanged, it suffices to us *> which ignores f and just leaves b unchanged.

((x:), 0) *> (a,b) = (x:a, b) 

The 0 could be anything -- it doesn't matter. It also works to use >> in place of *>.

26 bytes

f(x:y)=([x],y)>>=span(/=x) 

Try it online!

A alternative, this time using the Monad instance (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)

27 bytes

f(x:y)=([x],[])<>span(/=x)y 

Try it online!

Using<> to do elementwise concatenate (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.

added 760 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
added 760 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
added 760 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading