miking-lang / miking

Miking - the meta viking: a meta-language system for creating embedded languages
Other
51 stars 31 forks source link

Definitions of some built-in functions feel reversed #38

Open johnwikman opened 4 years ago

johnwikman commented 4 years ago

Some built-in functions such as nth are defined to be used as nth <list> <pos> which feels a bit reversed as most languages use nth <pos> <list> instead. Is there a reason for having the arguments in this order?

An annoyance with having the list first is that it blocks many convenient partial applications. For example, if I would like to extract the nth element of all lists within a list, I would currently have to do it like this in MCore:

let nthception: ([[Elem]] -> Int -> [Elem]) = lam lists. lam pos.
        map (lam l. nth l pos) lists
in

How I would like to do it:

let nthception: (Int -> [[Elem]] -> [Elem]) = lam pos. lam lists.
        map (nth pos) lists
in
EliasC commented 4 years ago

Even though I spontaneously agreed with you when I read your post, both Haskell and OCaml actually have the list first!

(!!) :: [a] -> Int -> a
val nth : 'a list -> int -> 'a

This is less of a hassle in Haskell (heh) since you can write map (!! pos) lists. In OCaml you would have to do what you did above, or write map (Fun.flip nth pos) lists. My feeling is that it is probably best to follow existing practices in this case.

johnwikman commented 4 years ago

I can buy the reason for having the list first on !! in haskell since it is an infix operator, but in our case it is not. To me it makes more sense to have the list second in a functional context (for the reasons explained above). Maybe I have glossed over some important detail? Looking at OCaml's List it seems as if Nth is the lone exception that has the list first.