Live data from Hacker News

How to Implement a Programming Language in JavaScript

lisperator.net

21–30 of 36 posts

Re: How to Implement a Programming Language in JavaScript

#21
post #18
post #16

Earlier quoted context omitted.

Your comparison isn't really fair. With similar functions from Core's In_channel you can write something like: let sum_file filename = with_file filename (fold_lines ~init:0 ~f:(fun a l -> a + int_of_string l)) "Using the right tool for the job" when it comes to functional vs. "mainstream" languages is a popular meme, but it doesn't hold up to scrutiny. You can always write your own higher-level code for a given doma…

FWIW I took it straight out of Real World OCaml, assuming that that's idiomatic OCaml. See my other comment -- how does top K lines in OCaml look? I recall that was in the book too, but couldn't find it. I remember it being fantastically ugly.

Idiomatic ocaml would be to use one of those "with" functions whenever possible. Keep in mind that RWO is an introductory book and that they have to show the basics before moving to the larger abstractions...

Re: How to Implement a Programming Language in JavaScript

#22
post #20
post #15

Earlier quoted context omitted.

Right, I was referring to all ML-based languages -- so SML, OCaml, Haskell, F#, and possibly even Rust. Do you know how this would look in Haskell? # Return K most common lines in a file def top_k(f, k): counts = collections.defaultdict(int) for line in f: counts[line] += 1 return sorted(counts.items(), key=lambda x: x[1], reverse=True))[:k] I was actually looking for the OCaml example which does this. I think it was…

The ocaml version of this code would be actually quite similar, since you can even use imperative hash tables if you want. The syntax is a bit fugly and the stdlib doesn't have some of the helpers python has but I wouldn't go as far as say that its "horribly ugly". Beauty is in the eye of the beholder and Ocaml has the advantage of being much less "magical" than python (as per the "explicit is better than implicit" m…

Meh, I don't buy it. Can you show it?

It's conceptually similar, but just plain uglier in practice. A lot of day to day programming is just FILLED with stuff that is very elegant in Python, and not so much in OCaml.

Language engineering is something is very elegant in OCaml.

In my opinion, EVERY language is a DSL. I stopped looking for the ultimate language -- it doesn't exist.

Re: How to Implement a Programming Language in JavaScript

#23
post #15

Earlier quoted context omitted.

Right, I was referring to all ML-based languages -- so SML, OCaml, Haskell, F#, and possibly even Rust. Do you know how this would look in Haskell? # Return K most common lines in a file def top_k(f, k): counts = collections.defaultdict(int) for line in f: counts[line] += 1 return sorted(counts.items(), key=lambda x: x[1], reverse=True))[:k] I was actually looking for the OCaml example which does this. I think it was…

I would do it something like this: top xs = map fst $ sortBy (compare `on` Down . snd) $ M.toList $ foldr (\x -> M.insertWith (+) x 1) mempty xs This has the type `top :: Ord a => [a] -> [a]`. You might object that I didn't include your k parameter. Because Haskell is a lazy language, I can get your behavior very simply by doing `take n . top` and it will lazily only calculate the first n values. This gives you more…

Yeah, so this sort of proves my point :)

I don't begrudge anyone if they want to use Haskell or OCaml for everything.

But personally I am interested in using ML-like languages for language engineering and not general purpose programming (maybe Rust could change that, but I'm not convinced). I program in 5+ different languages regularly so I've dealt with all the friction involved, mainly by explicitly designing systems as heterogeneous collections of Unix processes.

Re: How to Implement a Programming Language in JavaScript

#24
post #18
post #16

Earlier quoted context omitted.

Your comparison isn't really fair. With similar functions from Core's In_channel you can write something like: let sum_file filename = with_file filename (fold_lines ~init:0 ~f:(fun a l -> a + int_of_string l)) "Using the right tool for the job" when it comes to functional vs. "mainstream" languages is a popular meme, but it doesn't hold up to scrutiny. You can always write your own higher-level code for a given doma…

FWIW I took it straight out of Real World OCaml, assuming that that's idiomatic OCaml. See my other comment -- how does top K lines in OCaml look? I recall that was in the book too, but couldn't find it. I remember it being fantastically ugly.

Here's one way to write down the K lines example:

  let top_k chan k =
    let incr_count m l = 
      let n = try Map.find_exn m l with Not_found -> 0 in
      Map.add m ~key:l ~data:(n + 1)
    in
    In_channel.input_lines chan
    |> List.fold ~init:String.Map.empty ~f:incr_count
    |> Map.to_alist
    |> List.sort ~cmp:(fun (_,a) (_,b) -> compare b a)
    |> List.sub ~pos:0 ~len:k
You could probably code-golph it down to a couple of lines but I find the 'pipe' operator leads to very readable code.

Re: How to Implement a Programming Language in JavaScript

#25
post #23

Earlier quoted context omitted.

I would do it something like this: top xs = map fst $ sortBy (compare `on` Down . snd) $ M.toList $ foldr (\x -> M.insertWith (+) x 1) mempty xs This has the type `top :: Ord a => [a] -> [a]`. You might object that I didn't include your k parameter. Because Haskell is a lazy language, I can get your behavior very simply by doing `take n . top` and it will lazily only calculate the first n values. This gives you more…

Yeah, so this sort of proves my point :) I don't begrudge anyone if they want to use Haskell or OCaml for everything. But personally I am interested in using ML-like languages for language engineering and not general purpose programming (maybe Rust could change that, but I'm not convinced). I program in 5+ different languages regularly so I've dealt with all the friction involved, mainly by explicitly designing syste…

Hmmm, I'm not sure what you're saying. What point does it prove? I've been using Haskell for general purpose programming for five years now and I've found it to be exceptionally well-suited.

Re: How to Implement a Programming Language in JavaScript

#26
post #15

Earlier quoted context omitted.

I 100% agree that JS and Python are the wrong language for writing languages. Haskell, however gets you the best of both worlds. Your sum_file function in Haskell would look like this: sumFile :: FilePath -> IO Int sumFile file = sum . map read . lines readFile file I don't think many people would dispute that Haskell is at least as good as OCaml for writing languages. And strangely enough, the most advanced Perl 6 i…

Right, I was referring to all ML-based languages -- so SML, OCaml, Haskell, F#, and possibly even Rust. Do you know how this would look in Haskell? # Return K most common lines in a file def top_k(f, k): counts = collections.defaultdict(int) for line in f: counts[line] += 1 return sorted(counts.items(), key=lambda x: x[1], reverse=True))[:k] I was actually looking for the OCaml example which does this. I think it was…

> Do you know how this would look in Haskell?

I'm curious, how did you think it would look in haskell? Can you give a code example of what was in your head?

Re: How to Implement a Programming Language in JavaScript

#27
post #7

I've written multiple parsers/interpreters in both JS and Python -- Python being my favorite language. From that experience, I've come around to the fact that they're both the wrong language for writing languages -- lexers, parsers, interpreter loops, compilers. I have a good analogy to explain this. Take this OCaml program. let sum_file filename = let file = In_channel.create filename in let numbers = List.map ~f:In…

I 100% agree that JS and Python are the wrong language for writing languages. Haskell, however gets you the best of both worlds. Your sum_file function in Haskell would look like this: sumFile :: FilePath -> IO Int sumFile file = sum . map read . lines readFile file I don't think many people would dispute that Haskell is at least as good as OCaml for writing languages. And strangely enough, the most advanced Perl 6 i…

And strangely enough, the most advanced Perl 6 implementation is even written in Haskell!

That hasn't been true for a few years, but it was true for quite a while. At this point Pugs is fairly out of date and (I believe) abandoned, as it's main developer discontinued development. For a long while it was the most advanced Perl 6 compiler though, and from what I understand a lot of it's rapid advancement was attributed to it being written in Haskell (Perl 6 and Haskell share a good portion of advanced features, so that may have helped).

The current state of support of the various Perl 6 compilers can be seen here[1].

1: http://perl6.org/compilers/features

Re: How to Implement a Programming Language in JavaScript

#28
post #22
post #20

Earlier quoted context omitted.

The ocaml version of this code would be actually quite similar, since you can even use imperative hash tables if you want. The syntax is a bit fugly and the stdlib doesn't have some of the helpers python has but I wouldn't go as far as say that its "horribly ugly". Beauty is in the eye of the beholder and Ocaml has the advantage of being much less "magical" than python (as per the "explicit is better than implicit" m…

Meh, I don't buy it. Can you show it? It's conceptually similar, but just plain uglier in practice. A lot of day to day programming is just FILLED with stuff that is very elegant in Python, and not so much in OCaml. Language engineering is something is very elegant in OCaml. In my opinion, EVERY language is a DSL. I stopped looking for the ultimate language -- it doesn't exist.

Elegance is subjective... While its undeniable that you often need to type less characters in Python and list/dictionary comprehensions are wonderful syntactic sugar, its kind of nice that there is much less magic going on under the hood in Ocaml. In a highly dynamic language such as Python or Ruby you never know exactly what your code is doing: any method call can can be overloaded and even things like function calls and array indexing can be overloaded too. On the other hand, in Ocaml almost everything is statically dispatched and the static typing is really great (I'm sure Python wouldn't look as nice if you added the required modifications necessary to make it statically types as ocaml is)

While you might not see this dynamism as a big problem in Python, its something that I always found confusing about Ruby. Over there, people everything is a method call and people are super happy to monkeypatch extra methods to classes, which makes it very hard for me to understand the program by reading it. At some point, things get so dynamic that static analysis in your head gets too hard and you are better off just running the program and seeing if it worked...

In any case, I can't give you a concrete example right now because my laptop doesn't have ocaml installed on it but in my other post I already listed some of the bigger differences that the ocaml version has. The tldr is that some of the features you are using in Python aren't available in the ocaml stdlib (so you would need to write the sorting comparator the long way or create the helper function yourself) and that creating the polymorphic hash table is not very beginner friendly (this is a cost we pay for stronger typing). What I wanted to say is that the big things about the Python function (hash tables and generic sorting) are both things you can do in Ocaml just fine.

Re: How to Implement a Programming Language in JavaScript

#29
post #27

Earlier quoted context omitted.

I 100% agree that JS and Python are the wrong language for writing languages. Haskell, however gets you the best of both worlds. Your sum_file function in Haskell would look like this: sumFile :: FilePath -> IO Int sumFile file = sum . map read . lines readFile file I don't think many people would dispute that Haskell is at least as good as OCaml for writing languages. And strangely enough, the most advanced Perl 6 i…

And strangely enough, the most advanced Perl 6 implementation is even written in Haskell! That hasn't been true for a few years, but it was true for quite a while. At this point Pugs is fairly out of date and (I believe) abandoned, as it's main developer discontinued development. For a long while it was the most advanced Perl 6 compiler though, and from what I understand a lot of it's rapid advancement was attributed…

Ahh cool, thanks for the correction.

Re: How to Implement a Programming Language in JavaScript

#30
post #6

Given the domain name I was hoping they'd first implement a Lisp in 12 lines of Javascript, and then implement the new language in that lisp, that would've been an interesting tutorial. I get that it's a beginners tutorial and they have some constraints, but it starts off with "let's dream up a language" and then presents a super standard language, it's basically just Javascript with obligatory semicolons..

... and then it gets continuations, and that's where it becomes interesting.

Any case, the intent was purely didactic, but I did implement a Scheme dialect based on half of that code. To be released some day...

Post reply on HN