mostly-adequate-guide
mostly-adequate-guide copied to clipboard
Chapter 11 arrayToList
trafficstars
In Chapter 11
// arrayToList :: [a] -> List a
const arrayToList = List.of;
I execute in the ternimal console.log(arrayToList([1,2,3,4])), got nested array List([[1, 2, 3, 4]])
Is this expected behavior ?
Look at implementation of list from https://mostly-adequate.gitbooks.io/mostly-adequate-guide/appendix_b.html#list
class List {
constructor(xs) {
this.$value = xs;
}
// ----- Pointed List
static of(x) {
return new List([x]);
}
// ----- Functor List
map(fn) {
return new List(this.$value.map(fn));
}
}
So, yes apparently arrayToList([1,2,3,4]) will result List([[1, 2, 3, 4]])
But that does not make sense,
Because that means compose(arrayToList, map(f)) !== compose(map(f), arrayToList)