From OCaml to Javascript

Discussion in 'Javascript' started by Jon Harrop, Aug 25, 2007.

  1. Jon Harrop

    Jon Harrop Guest

    I'm trying to grok Javascript. As my background is functional programming
    languages, all the higher-order functions, currying and so forth are easy
    but I've no idea how to express sum types and pattern matches.

    Is there a resource to teach the (tiny minority!) of people like me
    Javascript?

    How do you translate the following symbolic simplifier into Javascript, for
    example:

    http://www.codecodex.com/wiki/index.php?title=Derivative

    --
    Dr Jon D Harrop, Flying Frog Consultancy
    OCaml for Scientists
    http://www.ffconsultancy.com/products/ocaml_for_scientists/?usenet
     
    Jon Harrop, Aug 25, 2007
    #1
    1. Advertising

  2. Jon Harrop wrote:
    > I'm trying to grok Javascript. As my background is functional programming
    > languages, all the higher-order functions, currying and so forth are easy
    > but I've no idea how to express sum types and pattern matches.


    This may surprise you, but ECMAScript implementations include features of
    functional languages.

    http://javascript.crockford.com/javascript.html

    > Is there a resource to teach the (tiny minority!) of people like me
    > Javascript?


    This newsgroup? Maybe one of the many resources linked in its FAQ?

    > How do you translate the following symbolic simplifier into Javascript, for
    > example:
    >
    > http://www.codecodex.com/wiki/index.php?title=Derivative


    I don't know yet, but please tell us when you got it.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Aug 25, 2007
    #2
    1. Advertising

  3. Jon Harrop

    Jon Harrop Guest

    Thomas 'PointedEars' Lahn wrote:
    > Jon Harrop wrote:
    >> How do you translate the following symbolic simplifier into Javascript,
    >> for example:
    >>
    >> http://www.codecodex.com/wiki/index.php?title=Derivative

    >
    > I don't know yet, but please tell us when you got it.


    I think I've made some progress. If you take the minimal OCaml example:

    # let rec ( +: ) f g = match f, g with
    | `Int 0, f | f, `Int 0 -> f
    | f, `Add(g, h) -> f +: g +: h
    | f, g -> `Add(f, g);;
    val ( +: ) : ([> `Add of 'a * 'a | `Int of int ] as 'a) -> 'a -> 'a = <fun>

    Then the polymorphic variant type that OCaml inferred is equivalent to a
    class hierarchy in JavaScript, which must be something like:

    // Equivalent to `Add
    function Add(f, g) {
    this.f = f;
    this.g = g;
    }

    // Equivalent to +:
    function add(f, g) {
    if f=0 then g else
    if g=0 then f else
    if g instanceOf Add then add(add(f, g.f), g.g) else
    Add(f, g)
    }

    --
    Dr Jon D Harrop, Flying Frog Consultancy
    OCaml for Scientists
    http://www.ffconsultancy.com/products/ocaml_for_scientists/?usenet
     
    Jon Harrop, Aug 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Rubin

    OT: Ocaml?

    Paul Rubin, Aug 23, 2003, in forum: Python
    Replies:
    6
    Views:
    520
    Donn Cave
    Aug 26, 2003
  2. Max Powers

    Embedding OCaml within Python

    Max Powers, Apr 28, 2004, in forum: Python
    Replies:
    5
    Views:
    570
    Bruno Desthuilliers
    May 1, 2004
  3. Bryan

    ocaml extension

    Bryan, Jun 16, 2004, in forum: Python
    Replies:
    0
    Views:
    335
    Bryan
    Jun 16, 2004
  4. Jelle Feringa // EZCT / Paris

    OCAMl a more natural extension language for python?

    Jelle Feringa // EZCT / Paris, Jan 17, 2005, in forum: Python
    Replies:
    4
    Views:
    507
  5. Gigs_

    ocaml to python

    Gigs_, Feb 19, 2007, in forum: Python
    Replies:
    2
    Views:
    517
    Laurent Pointal
    Feb 20, 2007
Loading...

Share This Page