Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:21:52+00:00 2026-06-06T13:21:52+00:00

How do I construct functional and recursive datatypes in javascript? I would like to

  • 0

How do I construct functional and recursive datatypes in javascript?

I would like to be able to do something ML like:

datatype binary_node = Node of binary_node*binary_node 
                     | Lead of int

Some time ago I took a course in functional programming — the course was, for some random reason, in Scheme, and we constructed datatypes by making tubles, starting with the name of the data type and then the ‘payload’, is this the way to do functional programming-style data types in Javascript?

construct_node(n1,n2) -> 
    ("Node", n1, n2).

construct_leaf(int_value) ->
    ("Leaf", int_value).

and then a type checker:

is_node(n) ->
    if (n[0] == "Node") ->
        is_binary_tree(n[1])  and is_binary_tree(n[2])
    else
        false
is_leaf(l) ->
    if(l[0] == "Leaf") ->
        is_integer(n[1])
    else 
        false
is_binary_tree(t) ->
    is_node(t) or is_leaf(t)

What would be the smartest way to do this in javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:21:54+00:00Added an answer on June 6, 2026 at 1:21 pm

    JavaScript is commonly used in a duck typed fashion. In this way, you don’t need to define any special datatypes. Any object that has properties node1 and node2 may be considered a binary tree node.

    var n = {
        node1: {
            node1: 456,
            node2: 789
        },
        node2: 1002
    };
    
    function isNode(x) {
        return x && isBinaryTree(x.node1) && isBinaryTree(x.node2);
    }
    
    function isLeaf(x) {
        return typeof x === 'number';
    }
    
    function isBinaryTree(x) {
        return isNode(x) || isLeaf(x);
    }
    

    Note that the functions above are for recursively checking the integrity of an entire tree, not for node-by-node case distinction during traversal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I set the default arument value in Construct function something like ? public
I'm writing a recursive function to construct a multidimensional array. Basically, the problem is
I have a simple recursive array function that looks like this: function recursive_array($results) {
I would like to plot a decomposition tree in Mathematica. I have a function
In many scripts that I write, I often construct programs in a functional style.
I have lots of code like this in my constructors:- function __construct($params) { $this->property
For some reason, when defining: function __construct() { if(!isset($_GLOBALS[className])) { $_GLOBALS[className] = new className;
I'm new to Scheme (via Racket) and (to a lesser extent) functional programming, and
I'm writing a program to solve the result of primitive recursive functions: 1 --Basic
I have the following recursive function for project euler question no. 74 : chain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.