Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

APL's assignment operator is very strange to me; simple array of numbers assigned to 'nums':

    nums←1 2 3 4 5
Then catenate (append) a 6 to the right side end of it:

          nums,←6
          nums
    1 2 3 4 5 6 
Now use position-reversed catenate to append a 6 to the left side end of it; how does that work?:

          nums,⍨←6
          nums
    6 1 2 3 4 5 6 
Now use addition-assign to add 2 to each item; how:

          nums+←2
          nums
    8 3 4 5 6 7 8 
Now assign a single value into non-consecutive locations using array subscript notation on the left side of the assignment; that's unusual:

          nums[2 4]←99
          nums
    8 99 4 99 6 7 8 
Now do a max-assignment to replace values which are less than 7 with 7:

          nums⌈←7
          nums
    8 99 7 99 7 7 8 
      
Nums reset to 1 2 3 4 5, laminated with 'hello' to pair each number with a letter:

          nums←1 2 3 4 5
          nums,[0.5]'hello'
    1 2 3 4 5
    h e l l o
          nums
    1 2 3 4 5 
Want to do that and update nums? Stick an assignment in there:

          nums←1 2 3 4 5
          nums,[0.5]←'hello'
          nums
    1 2 3 4 5
    h e l l o
It seems so odd that this can pull values out and modify them with the value being assigned and put the result back, assigning to 'nums' and modifying it, while keeping the shape and structure of it.


For my amusement mostly, here's the Julia translation:

    nums = [1,2,3,4,5]
    nums ↑ 6              #  const ↑ = push!; const ⤉ = pushfirst!
    nums ⤉ 6  
    nums .+= 2            # addition-assign to add 2 to each item
    nums[[2,4]] .= 99     
    nums
    nums ⩠ 7              #  ⩠(a,b) = a[a.<b].=b
    nums

    nums = [1,2,3,4,5]
    nums ↔ "hello"        #  ↔(a,b) = hcat(collect(a), collect(b))
    nums = nums ↔ "hello" # re-binding nums




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: