Discussion:
[elm-discuss] Arbitrary length currying
Ray Toal
2017-11-06 03:39:56 UTC
Permalink
There's an interesting problem on the Programming Puzzles and Stack
Exchange on arbitrary length currying
here: https://codegolf.stackexchange.com/questions/117017/arbitrary-length-currying.
It asks for a function f behaving as follows:

f () = 0
f (3)(9)(2)() = 14

This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?

The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."

Is this problem impossible in Elm?

If impossibie, can a solution be found to a related problem, say where the
arguments are lists?, e.g.

f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Andrews
2017-11-06 12:22:55 UTC
Permalink
The solution for the list version is very straightforward in elm:
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
Exchange on arbitrary length currying here: https://codegolf.
stackexchange.com/questions/117017/arbitrary-length-currying. It asks for
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where the
arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ray Toal
2017-11-06 16:53:31 UTC
Permalink
Thanks but I was looking not for the obvious, practical approach for
summing integers but was interested in the puzzle of arbitrary-length
currying. When called with no arguments, the function should yield its sum
so far. When called with a single argument, the function should return a
function that knows about what it has seen so far. It sounds stateful, but
can be done without state. But since Elm is statically typed and doesn't
have overloading, I'm wondering if this can even be done with currying.
Post by David Andrews
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
https://codegolf.stackexchange.com/questions/117017/arbitrary-length-currying.
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where
the arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
David Andrews
2017-11-07 04:45:24 UTC
Permalink
Sorry I misread your example as a single list argument.

Strictly speaking, you can not call a function with no arguments in Elm,
but some functions take Unit as an argument.

There are a couple of roadblocks preventing this exact functionality in
Elm. Firstly, Elm does not allow recursive types
<https://ellie-app.com/5YDsxhFLSa1/0>. Secondly, a name in Elm must have
the same type everywhere it is mentioned. Even normal function overloading
is not possible <https://ellie-app.com/5YDsxhFLSa1/1> without renaming the
function.

The closest I could come up with was this
<https://ellie-app.com/g4DpfMDxPa1/1>, which I don't think will be very
satisfactory to you, but I believe is isomorphic to the Haskell example.
You just have to explicitly name all of the function applications in Elm.


On Nov 6, 2017 11:53 AM, "Ray Toal" <***@gmail.com> wrote:

Thanks but I was looking not for the obvious, practical approach for
summing integers but was interested in the puzzle of arbitrary-length
currying. When called with no arguments, the function should yield its sum
so far. When called with a single argument, the function should return a
function that knows about what it has seen so far. It sounds stateful, but
can be done without state. But since Elm is statically typed and doesn't
have overloading, I'm wondering if this can even be done with currying.
Post by David Andrews
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
Exchange on arbitrary length currying here: https://codegolf.stackex
change.com/questions/117017/arbitrary-length-currying. It asks for a
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where
the arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ray Toal
2017-11-07 04:50:07 UTC
Permalink
I like the calls and resolves! Nice.
Post by David Andrews
Sorry I misread your example as a single list argument.
Strictly speaking, you can not call a function with no arguments in Elm,
but some functions take Unit as an argument.
There are a couple of roadblocks preventing this exact functionality in
Elm. Firstly, Elm does not allow recursive types
<https://ellie-app.com/5YDsxhFLSa1/0>. Secondly, a name in Elm must have
the same type everywhere it is mentioned. Even normal function
overloading is not possible <https://ellie-app.com/5YDsxhFLSa1/1> without
renaming the function.
The closest I could come up with was this
<https://ellie-app.com/g4DpfMDxPa1/1>, which I don't think will be very
satisfactory to you, but I believe is isomorphic to the Haskell example.
You just have to explicitly name all of the function applications in Elm.
Thanks but I was looking not for the obvious, practical approach for
summing integers but was interested in the puzzle of arbitrary-length
currying. When called with no arguments, the function should yield its sum
so far. When called with a single argument, the function should return a
function that knows about what it has seen so far. It sounds stateful, but
can be done without state. But since Elm is statically typed and doesn't
have overloading, I'm wondering if this can even be done with currying.
Post by David Andrews
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
https://codegolf.stackexchange.com/questions/117017/arbitrary-length-currying.
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where
the arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google
Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Matthew Bray
2017-11-07 22:25:21 UTC
Permalink
It's not quite what you asked for, but Kris Jenkins' Formatting[0] library
does some interesting stuff with variadic functions. In that library, the
`print` function's arity depends on the value of its first argument.

Here's the idea applied to this problem (code lifted straight from the
Formatting library)[1]. The number of arguments taken by the `sum` function
(and their types!) is determined by the `Sum r a` object you pass as the
first argument. You construct a `Sum r a` object by joining up `i`s (when
you want to pass an `Int`) and `f`s (when you want to pass a `Float`) using
the composition operator `<>`.

[0]
http://package.elm-lang.org/packages/krisajenkins/formatting/4.2.0/Formatting
[1] https://ellie-app.com/rpJq3K6Lra1/0
Post by Ray Toal
I like the calls and resolves! Nice.
Post by David Andrews
Sorry I misread your example as a single list argument.
Strictly speaking, you can not call a function with no arguments in Elm,
but some functions take Unit as an argument.
There are a couple of roadblocks preventing this exact functionality in
Elm. Firstly, Elm does not allow recursive types
<https://ellie-app.com/5YDsxhFLSa1/0>. Secondly, a name in Elm must
have the same type everywhere it is mentioned. Even normal function
overloading is not possible <https://ellie-app.com/5YDsxhFLSa1/1>
without renaming the function.
The closest I could come up with was this
<https://ellie-app.com/g4DpfMDxPa1/1>, which I don't think will be very
satisfactory to you, but I believe is isomorphic to the Haskell example.
You just have to explicitly name all of the function applications in Elm.
Thanks but I was looking not for the obvious, practical approach for
summing integers but was interested in the puzzle of arbitrary-length
currying. When called with no arguments, the function should yield its sum
so far. When called with a single argument, the function should return a
function that knows about what it has seen so far. It sounds stateful, but
can be done without state. But since Elm is statically typed and doesn't
have overloading, I'm wondering if this can even be done with currying.
Post by David Andrews
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
https://codegolf.stackexchange.com/questions/117017/arbitrary-length-currying.
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about
the number of arguments, and is easy to do in statically typed languages
which allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where
the arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google
Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Adrian Roe
2017-11-06 20:15:26 UTC
Permalink
I’m pretty certain you can’t do this in elm. The ellie is for f [] and f
[3,9,2] whereas the question is for f [] and f [3] [9] [2] []

In the latter example, you need f [] to return an integer and f [3] to
return a function that takes two lists of integers and a list of any type
and returns an integer

Post by David Andrews
https://ellie-app.com/g4DpfMDxPa1/0
Post by Ray Toal
There's an interesting problem on the Programming Puzzles and Stack
https://codegolf.stackexchange.com/questions/117017/arbitrary-length-currying.
f () = 0
f (3)(9)(2)() = 14
This is trivial in dynamically typed languages that don't care about the
number of arguments, and is easy to do in statically typed languages which
allow overloading. But what about the ML-like languages?
The only ML-like language with a solution is Haskell. Its author says "Forcing
Haskell's strict type system to allow this requires some magic, namely,
enabling the GHC extension for flexible typeclass instances."
Is this problem impossible in Elm?
If impossibie, can a solution be found to a related problem, say where
the arguments are lists?, e.g.
f [] = 0
f [3] [9] [2] [] = 14
--
You received this message because you are subscribed to the Google Groups
"Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marek Fajkus
2017-11-08 13:46:05 UTC
Permalink
Unit () and integer are different types, therefore, a function that takes >*either
Int or Unit<* isn't compatible with elm type system (I think it's actually
a property of System F - https://en.wikipedia.org/wiki/System_F but don't
quote me on that).
However, you can construct either type like `Either () Int`. In elm there
is `Result a b` which in essence Either type. Anyway since Unit / () has
exactly one type constructor this one is unnecessary to be specified - We
already have types which has this exact behavior - *Maybe*. So `Maybe Int`
is an essential part of a correct solution in my opinion.

However, we still need to solve return type which can be for instance
either an intermediate result of final one (Either Int Int).

Now we can just put all of this together. This is what is the best solution
in my opinion: https://ellie-app.com/g3Z6VS9YCa1/1

This is just one of the possible solution - for sake of simplicity I've
tried to reuse existing types instead of defining my owns.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marek Fajkus
2017-11-08 14:14:14 UTC
Permalink
Reading assignment one more time I think it's fair to add laziness to the
mix. This is lazy version https://ellie-app.com/g3Z6VS9YCa1/2 (can't test
it though as ellie is broken at the time of posting this)
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...