Assign

From Esolang
Jump to navigation Jump to search

Assign is an esoteric programming language that is a subset of JavaScript. In Assign there are no assignment, increment or decrement operators.

Strategy

I found that to write programs in assign I would need to use one of two strategies (maybe there are others). The first strategy is to use recursive functions instead of loops in all cases. This actually leads to some very elegant (and short) programs. The second strategy is to use an array to store values. We initialize the array by passing [] into a function, and then provided set and get operations which modify elements in the array.

Example Programs

Primes #1

function checkPrime(a, b){ 
  if(a == b)
    return true 
  return ( b % a != 0 ) && checkPrime(a+1, b) 
} 

function isPrime(x){ 
  return checkPrime(2, x)
} 

function printPrimes(a, b){ 
  if(isPrime(a))
    console.log(a)
  if(a < b)
    printPrimes(a+1, b)
}

printPrimes(2, 30)

Sort

function merge(arr1, arr2){ 
  if( (arr1.length > 0) && (arr2.length > 0) ){ 
    if(arr1[0] < arr2[0])
      return [arr1[0]].concat( merge(arr1.slice(1), arr2) )
    else
      return [arr2[0]].concat( merge(arr1, arr2.slice(1)) )
  } else { 
    return arr1.concat(arr2)
  }
}

function sort(arr){ 
  if(arr.length < 2)
    return arr
  return merge( sort( arr.slice( Math.floor( arr.length / 2 ) ) ), sort( arr.slice( 0, Math.floor( arr.length / 2 ) ) ) )  
}

console.log(sort([4, 3, 2, 1, 5]))

Primes #2

function run(arr, size){
  function init(){ 
    arr.push([])
    arr.push([])
    while(arr[0].length < size){
      arr[0].push(0)
    }
  }
   
  function set(pos, value){ 
     while(arr[0].length > (pos + 1))
       arr[1].push(arr[0].pop())
    
    arr[0].pop()
    arr[0].push(value)
    
    while(arr[1].length > 0) 
      arr[0].push(arr[1].pop())
  }
  
  function get(pos){ 
    return arr[0][pos]    
  }
  
  init()

  function isPrime(x){ 
    set(1, 2)
    while(get(1) < x){ 
      if(x % get(1) == 0)
        return false
      set(1, get(1) + 1)
    }
    return true
  }
  
  for(set(0, 2); get(0) < 30; set(0, get(0)+1 )){ 
    if(isPrime(get(0)))
      console.log(get(0))
  }
  
} 

run([], 10)

Quine

((q,d)=>{console.log(d+q+`',`+q+d+q+`)`)})('"',"((q,d)=>{console.log(d+q+`',`+q+d+q+`)`)})('")