Skip to content

Instantly share code, notes, and snippets.

@MaisaMilena
Created July 2, 2019 14:30
Show Gist options
  • Save MaisaMilena/cd7e76a9f27fc43ed99c95e7ee024c9e to your computer and use it in GitHub Desktop.
Save MaisaMilena/cd7e76a9f27fc43ed99c95e7ee024c9e to your computer and use it in GitHub Desktop.
  1. dup, only works on boxed elements
  2. To be able to duplicate x in getCategory, we need a boxed number as a parameter
  3. We are passing a boxed number to getCategory, unboxing it with dup and we should box it again before return (read more about Stratification Condition). As we should box any return, the best place to put the # if before if.
def isTeenager: {x}
  |x < 18|

def isAdult: {x}
  |x < 60|

def getCategory: {x}
  dup age = x 
  #if (isTeenager age) 
  then: "boring teenager" 
  else: 
    if (isAdult age) 
    then: "cool adult"
    else: "respect the elder"

def main: 
  (getCategory #15)

Easy, right? Now assume that a teenager is someone with age less than or equal to 17. You have to work only with unboxed values to do it, that is, you can't use dup. How you are going to fix it?
You have 3 options:

  1. Add 1 more parameter in isTeenager
def isTeenager: {x} {y}
  ||x < 17| | |y == 17||
...
#if (isTeenager age age)
...
  1. Using a pair
def isTeenager: {x} 
  get [aux1, aux2] = x
  ||aux1 < 17| | |aux2 == 17||
...
#if (isTeenager [age, age]) 
...
  1. Use cpy which can be used to copy numbers. Given a number n, returns &(n, n), a pair with two copies of the element.
def isTeenager: {x} 
  get [aux1, aux2] = cpy x
  ||aux1 < 17} | |aux2 == 17||
...
#if (isTeenager age) 
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment