5 kyu
Lambda Calculus: Lists as folds I
27 of 95JohanWiltink
Loading description...
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
This comment has been hidden.
(cons (head x) ...
x is not a listSorry, I meant
cons (head l) ...
.Don't look at the type error - it's not helping in this case. You are misapplying types somewhere. Have a good look at what is a list and what is an element. ( It helps me to name lists
xs
instead ofl
. )I need a hint to optimze the Python code. I can't reach the 12s time limit.
My current times are:
snoc and filter is slower because this functions use recursion.
Is there a general method to optimize the functions with recursion?
This comment has been hidden.
The predefined head and tail functions may not be the way forward.
Python translation, complete with syntax checker.
Once approved and happy with the syntax checker, I will use it in some other katas.
Approved. Thank you lots! :]
..... meh... :/ whyyyy...?
mmmh... I beleive I got the "why"...
(did I already tell you how much I suck at LC? x) )
Show me where
===
is in Lambda Calculus and you will have shown me something I didn't know. :PThat's not how
isEmpty
works. It returns a Church Boolean, which chooses between its two arguments.What you are doing could work in JS, because it has a notion of object equality; but in LC, what you are doing can never work, because it is undecidable if one function is "the same as" another function.
Does this answer your question?
yup x)
(did I tell...? Err... Yeah I did. x) )
note: I believe I didn't get that failing tests when running the samplet tests. Could you check?
I checked, and if I add the offending line the example tests already fail me. ( They run in
Preloaded
in JS, so you get whacked on the head before youAttempt
. Seemed nicer - people might do things that solve the kata but are not LC. )Is it me or you didn't specify that map and filter always use
cons
and neversnoc
? (but it's entierly possible I just (yet again) didn't understand anything to the task... You know how much I... like this stuff... XD )map
andfilter
return lists, so use thec
that is passed to them ( later - you might not even have realised there will be ac
and ann
coming later ). See the definition of a list, in the code blocks up top.But if you can write them in terms of
cons
orsnoc
, be my guest. I don't think it can be done, because a list has to be parametrised overc
andn
, so you can't hardcode them. But if you can .. you'll have shown me something I didn't know. :PIt might be possible to hardcode the
c
andn
I use for testing. I'll add a test that uses different ones, so your solution has to use what's coming in.The JS random tests will now test if a list is correctly encoded as
c => n => c(x)(c(y)(c(z)(n)))
. It will pass varying appropriate values forc
andn
.You can write them in terms of
cons
( andnil
). Of course you can, because those are already parametrised overc
andn
.The input list is encoded as a
foldr
, which associates to the right, sosnoc
is not very useful, but it can be done.Realistically, stick to
cons
. Once you havecons
, you can use that ( andnil
, which you havePreloaded
, andfoldr
, which is just an input list ) to do anything. Onlycons
itself has to be written primitively.This fork, which you can't see yet I guess, shows
map
andfilter
by way offoldl
andsnoc
instead offoldr
andcons
.But it can't be done in the kata, because you need a polymorphically existentially quantified
List
type for that, and the kata doesn't have that. ( The sequel will. Hi carrott! )Also, it defines
foldl
andsnoc
in terms offoldr
,cons
andnil
to arrive at a monstrosity that seems to run quadratically, with nested folds, over the list while building ( which is then probably optimised to a single linearfoldr
by GHC in post. GHC is quite aggressive at that ).In short, don't do it. In this kata, you can't even do it. Do it the way Dog intended.
I guess you could do it in JS. The above pertains to Haskell.
(I cannot see the fork as long I didn't solve the kata, btw ;) )
Yeah, I know that. There are two solutions to that, and I think you know them. :P
It is indeed possible to do
map
andfilter
in terms ofsnoc
in JS; see here. You needfoldl
in terms offoldr
for that, which can be done but may be unintuitive.Can I do anything to help you enjoy Lambda Calculus, or at least get better at it?
While
filter
is stillundefined
, there is one sample test:Which throws type complaints, making it hard to solve the kata step by step (especially if you define
filter
last, like I did).If it isn't possible to make it calm down, then I would suggest removing that test.
Apologies. I missed that. Fixed now.
You can now solve it step by step. It's calmed down. Better. :] Thanks.
Looks good. Very nice kata, it kind of left me wanting to do more with fold lists.
Thanks. Should I be doing a sequel?
I could probably figure out a way to deal with numbers, and have you do
get
,set
,length
,init
,last
,includes
,indexOf
,find
,some
,every
, and yes,foldl
( which also givesreverse
). Also,zip
might be fun.I would certainly enjoy it! Would numbers just be Church/some other encoding, or something specific to folding?
Church numerals are very Lambda Calculus, but I am leaning towards just embracing native unsigned integers, with predefined
zero
,succ
andpred
functions.Call it optimized Peano. That would minimise the changes to the parser as well ( no operator scribbles ).
"Something specific to folding" - do you have something in mind? I can't imagine what, but I would love to know if such a thing could exist.
You could of course convert the Peano nums to Church or Scott yourself. Church has its advantages.
I would probably also do everything fully typed ( in Haskell ). Takes a bit of wrapping and unwrapping, but that's compile time overhead only and does fit with the theme.
JavaScript: I suggest to add
const
to the initial solution:The initial solution is wrapped inside a funciton. It is not a good practice to define variables inside a function without
const
,let
, orvar
. Moreover, it is prohibited in strict mode.I know it's not good practice, but it's legal. And normally, I absolutely don't do this unless it's a code golf. My ( only ) reason to do it here is that this is embedded Lambda Calculus in JavaScript, and I wanted it to look more like plain LC ( all the parens are bad enough ). Technically, it doesn't really matter ( and I could quite easily fix the parser to require, and not even just allow, it ).
Do I really have to? Will it improve your opinion of the kata if it's added?
It is legal now. But when proper modules are implemented it will be necessary to add
var
,let
, orconst
to all variables which are exported. So I think it is better to addconst
to all declarations now.I'll get on it.
I've added
const
s to the initial solution, made it mandatory, and updated the description.Haskell: The initial solution does not compile:
Apologies. I missed that. Fixed now.