Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Post Code

Name: Anonymous 2011-03-04 21:33

One recurring complaint is that nobody talks about code on /prog/. So everyone write go and write some code, any code, that does something, anything, and post it. A small explanation wouldn't go amiss either.

Name: Anonymous 2011-03-04 21:53

Posted from my Macbook Wheel, so excuse my brevity.


for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; i++);

both generate the same assembly. Anyone suggesting otherwise is trolling.

Name: Anonymous 2011-03-04 22:24


ORG $2030        
      
BEGIN
    LDAA #$FF
    STAA $1000
    JSR  DELAY
    LDAA #0
    STAA $1000
    JSR  DELAY
    JSR  DELAY
    JMP  BEGIN

DELAY    LDB    #10
OUTER    LDX    #20000
PAUSE    NOP
    NOP
    DEX
    BNE    PAUSE
    DECB
    BNE OUTER
    RTS


Turns a LED in port a($1000) off and on

Name: Anonymous 2011-03-04 22:34

>>3
What platform?

Name: Anonymous 2011-03-04 22:41

My contribution
#!r6rs
(library (generalized-set)
(export (rename (set!!! set!))
        define-getter/setter)
(import (rnrs)
        (rnrs mutable-pairs)
        (rnrs mutable-strings))

;; To /prog/, love that guy who uses R6RS
;;
;; So, I was using guile earlier, and I decided I wanted to add
;; generalized `set!` (which guile supports through both SRFI 17 and
;; it's own `make-procedure-with-setter`) to my R6RS
;; code. Unfortunately, there is no good way to implement SRFI 17 in
;; portable R6RS unless you use lexically hidden "tags" to access the
;; setters of procedures. Which is both ugly, and a stupid
;; implementation strategy.
;;
;; Instead I decided to settle for creating a special form for
;; defining getter/setters. The error handling isn't there yet, but
;; I'll probably fix that at some point.
;;
;; Usage:
;; > (import (generalized-set))
;; > (define-record-type cell
;;     (fields (mutable var)))
;; > (define-getter/setter cell-ref cell-var cell-var-set!)
;; > (define c (make-cell 3))
;; > (cell-ref c)
;; 3
;; > (set! (cell-ref c) 9)
;; > (cell-ref c)
;; 9
;;
;; Tested on Ikarus and Mosh.
;; I see no reason why it wouldn't work on Racket though.

(define-syntax set!!
  (syntax-rules ()
    ((set!! (accessor arg ...) val)
     (set! accessor (arg ... val)))
    ((set!! . rest)
     (set! . rest))))

(define-syntax define-getter/setter
  (syntax-rules ()
    ((define-getter/setter name ?getter ?setter)
     (begin
       ;; No point evaluating ?getter and ?setter multiple times
       (define getter ?getter)
       (define setter ?setter)
       (define-syntax name
         (make-variable-transformer
          (lambda (stx)
            (syntax-case stx (set!)
              ((set! name (vars (... ...)))
               #'(setter vars (... ...)))
              ((name vals (... ...))
               #'(getter vals (... ...)))
              (name
               (identifier? #'name)
               #'getter)))))))))

;; We want to handle various primitives especially, so that we don't
;; have to use complicated imports every time we want to use
;; generalized `set!`. Basically, we make them literals so they
;; capture the appropriate lexical binding, and behave correctly if
;; they are rebound locally.
;; If you don't like this, you just don't have to use it, simply fix
;; the export list at the top to export set!! as set!, not set!!!
(define-syntax set!!!
  (syntax-rules
      (car cdr caar cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar
       cdddr caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr cdaaar
       cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr string-ref vector-ref)
    ((set!!! (car x) val)
     (set-car! x val))
    ((set!!! (cdr x) val)
     (set-cdr! x val))
    ((set!!! (caar x) val)
     (set-car! (car x) val))
    ((set!!! (cadr x) val)
     (set-car! (cdr x) val))
    ((set!!! (cdar x) val)
     (set-cdr! (car x) val))
    ((set!!! (cddr x) val)
     (set-cdr! (cdr x) val))
    ((set!!! (caaar x) val)
     (set-car! (caar x) val))
    ((set!!! (caadr x) val)
     (set-car! (cadr x) val))
    ((set!!! (cadar x) val)
     (set-car! (cdar x) val))
    ((set!!! (caddr x) val)
     (set-car! (cddr x) val))
    ((set!!! (cdaar x) val)
     (set-cdr! (caar x) val))
    ((set!!! (cdadr x) val)
     (set-cdr! (cadr x) val))
    ((set!!! (cddar x) val)
     (set-cdr! (cdar x) val))
    ((set!!! (cdddr x) val)
     (set-cdr! (cddr x) val))
    ((set!!! (caaaar x) val)
     (set-car! (caaar x) val))
    ((set!!! (caaadr x) val)
     (set-car! (caadr x) val))
    ((set!!! (caadar x) val)
     (set-car! (cadar x) val))
    ((set!!! (caaddr x) val)
     (set-car! (caddr x) val))
    ((set!!! (cadaar x) val)
     (set-car! (cdaar x) val))
    ((set!!! (cadadr x) val)
     (set-car! (cdadr x) val))
    ((set!!! (caddar x) val)
     (set-car! (cddar x) val))
    ((set!!! (cadddr x) val)
     (set-car! (cdddr x) val))
    ((set!!! (cdaaar x) val)
     (set-cdr! (caaar x) val))
    ((set!!! (cdaadr x) val)
     (set-cdr! (caadr x) val))
    ((set!!! (cdadar x) val)
     (set-cdr! (cadar x) val))
    ((set!!! (cdaddr x) val)
     (set-cdr! (caddr x) val))
    ((set!!! (cddaar x) val)
     (set-cdr! (cdaar x) val))
    ((set!!! (cddadr x) val)
     (set-cdr! (cdadr x) val))
    ((set!!! (cdddar x) val)
     (set-cdr! (cddar x) val))
    ((set!!! (cddddr x) val)
     (set-cdr! (cdddr x) val))
    ((set!!! (string-ref x index) val)
     (string-set! x index val))
    ((set!!! (vector-ref x index) val)
     (vector-set! x index val))
    ((set!!! x val)
     (set!! x val))))

)

Name: Anonymous 2011-03-04 22:45

>>4
moto 6800

Name: Anonymous 2011-03-04 22:56

    ((set!!! (car x) val)
     (set-car! x val))
    ((set!!! (cdr x) val)
     (set-cdr! x val))
    ((set!!! (caar x) val)
     (set-car! (car x) val))
    ((set!!! (cadr x) val)
     (set-car! (cdr x) val))
    ((set!!! (cdar x) val)
     (set-cdr! (car x) val))
    ((set!!! (cddr x) val)
     (set-cdr! (cdr x) val))
    ((set!!! (caaar x) val)
     (set-car! (caar x) val))
    ((set!!! (caadr x) val)
     (set-car! (cadr x) val))
    ((set!!! (cadar x) val)
     (set-car! (cdar x) val))
    ((set!!! (caddr x) val)
     (set-car! (cddr x) val))
    ((set!!! (cdaar x) val)
     (set-cdr! (caar x) val))
    ((set!!! (cdadr x) val)
     (set-cdr! (cadr x) val))
    ((set!!! (cddar x) val)
     (set-cdr! (cdar x) val))
    ((set!!! (cdddr x) val)
     (set-cdr! (cddr x) val))
    ((set!!! (caaaar x) val)
     (set-car! (caaar x) val))
    ((set!!! (caaadr x) val)
     (set-car! (caadr x) val))
    ((set!!! (caadar x) val)
     (set-car! (cadar x) val))
    ((set!!! (caaddr x) val)
     (set-car! (caddr x) val))
    ((set!!! (cadaar x) val)
     (set-car! (cdaar x) val))
    ((set!!! (cadadr x) val)
     (set-car! (cdadr x) val))
    ((set!!! (caddar x) val)
     (set-car! (cddar x) val))
    ((set!!! (cadddr x) val)
     (set-car! (cdddr x) val))
    ((set!!! (cdaaar x) val)
     (set-cdr! (caaar x) val))
    ((set!!! (cdaadr x) val)
     (set-cdr! (caadr x) val))
    ((set!!! (cdadar x) val)
     (set-cdr! (cadar x) val))
    ((set!!! (cdaddr x) val)
     (set-cdr! (caddr x) val))
    ((set!!! (cddaar x) val)
     (set-cdr! (cdaar x) val))
    ((set!!! (cddadr x) val)
     (set-cdr! (cdadr x) val))
    ((set!!! (cdddar x) val)
     (set-cdr! (cddar x) val))
    ((set!!! (cddddr x) val)
     (set-cdr! (cdddr x) val))


You're using (arguably) the world's most extensible and dynamic language and you're too mentally lazy to generate those automatically? Come on.

Name: Anonymous 2011-03-04 22:59

>>7
I had emacs generate them for me

Name: Anonymous 2011-03-04 23:01

>>8
Fair enough. Carry on.

Name: Anonymous 2011-03-04 23:42

>>2
They both generate this code:

;

Anyone suggesting otherwise is trolling.

Name: Anonymous 2011-03-05 0:50

So what? No one talks about the concept of randomness on /b/. Fuck, /b/ isn't even random!

Name: Anonymous 2011-03-05 1:12

>>11
let's just assume that here I posted link to that stupid xkcd with random(){ret 4}]

Name: Anonymous 2011-03-05 2:01

-module(xkcd).
-export([rand/1]).

rand(all) -> 4;
rand(N) -> crypto:rand_uniform(0,N).

Name: Anonymous 2011-03-05 8:52

>>12
i luv xkcd!!!

Name: Anonymous 2011-03-05 8:53

>>13
0mg so randoooooom xD

Name: Anonymous 2011-03-05 8:55

>>13

Chop-sun by fair dice rorr, kekeke.

Name: Anonymous 2011-03-05 15:32

with this you may have fixed-length of array with numbers, that were transformed by some function. I don't know how this could be useful anyway.

>>> x = lambda u, p: map (u, xrange(p))
>>> x(str, 10)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Name: Anonymous 2011-03-05 16:57

In this game, two players sit in front of a pile of stones. They take turns, each removing a set amount of stones (up to a specified maximum). The person who removes the  last stone(s) wins. Half of the code is redundant as I don’t use a variable to track which player’s move it is.  Primarily because I didn’t know how to.

If you are lonely and want to play against the ‘computer’ change the second choice variable to choice=random.randint(1,max)be sure to import random, if you do


def nims(pile, max):
    while pile>0:
        choice=input("How many Stones do you want to remove, P1?")
        while choice not in range(1,max+1):
            choice=input("You have made an invalid selection P1.\
Please chose again.")
        pile=pile-choice
        print pile

        if pile<=0:
            print 'player 1 wins!'
            break

        choice=input("Player 2: How many Stones do you want to remove?")
        while choice not in range(1,max+1):
            choice=input("You have made an invalid selection P2.\
Please chose again.")
        pile=pile-choice
        print pile

        if pile<=0:
            print 'game over, player 2 is the winner!'

x=input('How big is the pile?\n')
y=input('How many stones can one person take each time?\n')
print 'Pile set to', x, ', maximum stone number set to', y,'!'

nims(x,y)

Name: Anonymous 2011-03-05 17:00

>>18
ONE WORD

Name: Anonymous 2011-03-05 17:07

A meager attempt at consuming resources:

#!/bin/bash
while [ 1 ]
do
  `touch /tmp/cpuhog/asdfqwer.$$`
done

Name: Anonymous 2011-03-05 17:08

>>19
if you like I can re-write it using list comprehensions

Name: Anonymous 2011-03-05 17:09

Putting cpuhog in check;

# !/bin/bash
#
function get_load() {
# gets cpu load from last minute
# man uptime
  load=$(uptime | cut -d, -f3 | grep -o "[0-9.]\{4\}$")
  echo $load
}

AVG_LOAD=$(get_load)
# if the avg load is greater than or equal to 5%
# then show us all processes using 5% or more of cpu
MAX_LOAD=$(echo "$AVG_LOAD/5" | bc)
if (($MAX_LOAD >= 1)); then
  printf "\nCurrent load average: $AVG_LOAD\n";
  FAT_LOAD=$(ps k +pcpu o pcpu,user,uid,pid,ppid,tty,stime,cmd -A  | grep "^.[5-9]\.[0-9]\{1\}*")
  printf "$FAT_LOAD\n"
  if [[ $1 = "-k" ]]; then
    # for each line delimted by newline get pid and uid or user
      # if uid or user is not root
        # kill pid
        echo "Kill option passed"
  fi;
fi;

#ps -Ao user,group,pid,pcpu,comm --sort +pcpu --cols 200 | tail -n10


Notice how well I'm doing it wrong?

Name: Anonymous 2011-03-05 17:12

How an expert newfag adds users;

#!/bin/bash
#
# lrn2bash
# ########
# prototype useradd using only primitive
# utilities and bash builtins
#
# add a new user account where uid is
# incremented and gid defaults to users
# group.
#
# overview
# ########
# this script takes 1 argument; user name
# this script takes 3 options; help, verbose, test
#
# the user id and group id is read from passwd
#
# each test will loop and read new input if fail
#
# the script will not write or execute any commands
# until all tests have passed
#
# rules/tests
# ###########
#
# the user name must be at least 3 characters in
# length and only alaphanumeric characters
# the user name must not be in use
#
# password must be at least 6 characters in length
# password must be verified
#
USERS=`ls /home`
#USERNAME=`for USER in $USERS; do echo $USER; done;`
username=$1
USERNAME_VALID=''
while [[ -z "$USERNAME_VALID" ]]; do
    if [ `echo "$username" | grep "$USERS"` ]; then
        echo -en "User exits.\nEnter new username: "
        read username
    elif [ `echo ${#username}` -le 1 ]; then
        echo -en "Need at least 2 characters.\nEnter new username: "
        read username
    elif [[ $username != [[:alpha:]][[:alnum:]]* ]]; then
        echo -en "Alpha numeric characters only. \nEnter new username: "
        read username
    else
        USERNAME_VALID=1
        echo "Your new user name: $username"
    fi;
done;

PASSWORD_VALID=''
echo -en "\nCreate new password: "
read -es password
while [[ -z "$PASSWORD_VALID" ]]; do
    if [[ ${#password} -lt 7 ]]; then
        echo -en "Password must be at least 6 characters.\nEnter new password: "
        read -es password
    else
        echo -en "Verify new password: "
        read -es verify_password
        if [[ "$password" = "$verify_password" ]]; then
            PASSWORD_VALID=1
        fi;
    fi;
done;

GROUP_USERS_STRING=`cat /etc/group | grep "^users*"`
while IFS=: read USR PASSWD GID; do
    gid=$GID
done <<< $GROUP_USERS_STRING

PASSWD_PREVIOUS_UID=tail etc/passwd -n10 | grep "\:10[0-9]\{2\}\:10[0-9]\{2\}\:" | tail -n1
while IFS=: read USR PASSWD UID; do
    uid=$(($UID+1))
done <<< $PASSWD_PREVIOUS_UID


# still need to read comment, set homepath, and define shell
# define passwd string
# encrypt password with crypt
# define shadow string
# chmod shadow (and passwd if necessary)
# append each string appropriately
# i.e. /etc/passwd
#    /etc/shadow
#   /etc/group
# chmod files with original permissions
# cp /etc/skel to /home/{$username}
#      


# echo "{$username}:x:{$uid}:{$gid}:"

Name: Anonymous 2011-03-05 17:14

>>23
newfag
[i][u][o]back to— eh, fuck it.

Name: Anonymous 2011-03-05 17:43

class m:
 def __init__(self):
  q=12000
  x=0
  while True:
    x+=1
    y=x%x*2
    z=x+y
    qp='                     '+str(q)
    if x % 1000 == 0:
     q=q-x
    qp=qp+'    '+qp.replace('3','three')
    print x,y,z,'                  ',x,z,y,'              ',z,qp
  def replacenumbers(self,numberstring)
 
if __name__=="__main__":
 m1=m()

Name: Anonymous 2011-03-05 17:52

A bit of CoffeeScript I'm working on, trying to distort the language without touching the compiler.


# Arbitrary iterator over a sequence using a stepper generator
# `seq' is any list.
# `stepper' is a generator that returns either an items array or $break
# `fn' is the function to call in each item
# `apply' is just a functional version of JavaScript's Function.apply
(defun iter: (seq, stepper, fn) ->
    (letb (step = (stepper seq)) ->
        (until (eq (call step), $break)
            (apply fn, step.item))))


`until' uses FIOC, so I'm going to replace this by something else later on

Name: Anonymous 2011-03-05 18:00

>>26
Is that Lisp?

Name: Anonymous 2011-03-05 18:14

>>27
It's some sort of macrofag function.

Name: Anonymous 2011-03-05 18:36

>>27
A bit of CoffeeScript I'm working on

So, nope.

Name: Gruff 2011-03-05 19:31

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :username
      t.string :password
      t.string :salt
     
      t.timestamps
    end
  end
 
  def self.down
    drop_table :users
  end
end

# Script For Creating Accounts, nust learn how to graphically implement this.

>> users = User.find(:all)

>> user = User.create(:username => "Tom", :password => "wertycool")

>>users = User.find(:all)

>>quit

#Atm im not sure if i need the terminal to activate this, shouldnt be a big problem though

#Well Im gonna need some help executing this,Uugh

class User < ActiveRecord::Base
end

#Some Tests to run in terminal, to check details of accounts, may be important to prevent bots joining the websitre
#This Also Enables changing of account inforamtion,good for people who forget theyre account details

user = User.find_by_username("john") # Type this in order, should receive the

>> user.username #Thats obvious

>> user.password

>> user.salt

>> user.password = "Example" #changes the users password= AWESOME.. shouldnt abuse

>>user.save #saves changes

>>user

#Thats that, This will hopefully allow for flexible data changes.

#Not sure what this is, Kinda worried about organiseing

>ruby rails generate controller account index registration # Run this in terminal?

ruby script/server

#Enter This When Testing site

class AccountController < ApplicationController
  def index
  end
 
  def registration
  end
end

#OK, I Need chases help with this, becasue im starting to lose track of this.
#A file In which the registration code will e loacated

<h1>Account#registration</h1>
<p>Find me in app/views/account/registration.html.erb</p>

#This will be what i need to change to when prompted by the website.

<% form_for :user, @user do |f| %>
  <%= error_messages_for :user %>
 
  Username: <%= f.text_field :username %><br/>
  Password: <%= f.password_field :password %><br/>
  Confirm Password: <%= f.password_field :password_confirmation %><br/>
  <%= submit_tag "Register!"  %>
<% end %>

#I need to add this to the user.rb in app/models

class User < ActiveRecord::Base
  validates_uniqueness_of :username
  validates_presence_of :password
  validates_confirmation_of :password
end

#This checks Information automatically when someone logs in or out.

#app/controllers/account_controller.rb This is the second fiel to modify with this code.

class AccountController < ApplicationController
  def index
  end
 
  def registration
    if request.post? and params[:user]
      @user = User.new(params[:user])
 
      if @user.save
        flash[:notice] = "User created."
        redirect_to :action => "index"
      end
    end
  end
end

#This is How new users are created, Still have no idea on how to do this.

#app/views/account/index.html.erb is the second one we need to modify with this code:

<% if flash[:notice] %>
  <%= h flash[:notice] %>
<% end %>
 
<h1>Welcome To The Game!</h1>

#Note: I havent thought of what to replace Welcome to the game! with,shit.

# Thats it,I think. Heres some extra stuff that maybe useful, not sure.
#This is a account protection system, stopping users from decrypting code within others accounts.
# Not sure it will work, best thing i could think of.
#For this t owork, ill need to change thi sfile app/models/user.rb

require "digest"
 
class User < ActiveRecord::Base
  validates_uniqueness_of :username
  validates_presence_of :password
  validates_confirmation_of :password
 
  # Setup the salt value and hash the password before we save everything to the
  # database.
  def before_save
    if (self.salt == nil)
      self.salt = random_numbers(5)
      self.password = Digest::MD5.hexdigest(self.salt + self.password)
    end
  end
 
  private
 
  # A sequence of random numbers with no skewing of range in any particular
  # direction and leading zeros preserved.
  def random_numbers(len)
    numbers = ("0".."9").to_a
    newrand = ""
    1.upto(len) { |i| newrand << numbers[rand(numbers.size - 1)] }
    return newrand
  end
end

#


Some Of the first script i ever did in rails

Name: Anonymous 2011-03-05 20:00

(define /prog/
  (if #t
      ("ANUS
ANUS ANUS ANUS ANUS ANUS ANUS
ANUS ANUS ANUS ANUS ANUS ANUS
HAVE YOU READ YOUR SICP TODAY
ANUS ANUS ANUS ANUS ANUS ANUS
ANUS ANUS ANUS ANUS ANUS
FUCKING HUGE TUNA FISH
ANUS ANUS ANUS ANUS ANUS
ANUS ANUS ANUS ANUS ANUS ANUS")
       ("faggot")))

Name: Anonymous 2011-03-05 20:05

>>29
Man this (http://jashkenas.github.com/coffee-script/) is some hot looking documentation. What are doing with CoffeeScript and how is it practical for what you're doing? I can't really wrap my head around why this would ever make sense. I've played with node.js a bit but CoffeeScript seems to take this idea of "oh JavaScript, everything's an object, and look closures and callbacks" to the extreme.

Name: Anonymous 2011-03-05 20:08

>>29
Although I have to confess that this is minimal and hot compared to some a lot of jQuery I've written.


    # Set up the compilation function, to run when you stop typing.
    compileSource = ->
      source = $('#repl_source').val()
      window.compiledJS = ''
      try
        window.compiledJS = CoffeeScript.compile source, bare: on
        el = $('#repl_results')[0]
        if el.innerText
          el.innerText = window.compiledJS
        else
          $(el).text window.compiledJS
        $('#error').hide()
      catch error
        $('#error').text(error.message).show()
 
    # Listen for keypresses and recompile.
    $('#repl_source').keyup -> compileSource()
 
    # Eval the compiled js.
    $('.minibutton.run').click ->
      try
        eval window.compiledJS
      catch error then alert error
 
    # Load the console with a string of CoffeeScript.
    window.loadConsole = (coffee) ->
      $('#repl_source').val coffee
      compileSource()
      $('.navigation.try').addClass('active')
      false
 
    # Helper to hide the menus.
    closeMenus = ->
      $('.navigation.active').removeClass 'active'
 
    # Bind navigation buttons to open the menus.
    $('.navigation').click (e) ->
      return if e.target.tagName.toLowerCase() is 'a'
      return false if $(e.target).closest('.repl_wrapper').length
      if $(this).hasClass('active')
        closeMenus()
      else
        closeMenus()
        $(this).addClass 'active'
      false
 
    $(document.body).click (e) ->
      return false if $(e.target).hasClass('minibutton')
      closeMenus()
 
    $('#open_webchat').click ->
      $(this).replaceWith $('<iframe src="http://webchat.freenode.net/?channels=coffeescript" width="625" height="400"></iframe>')
 
    compileSource()

Name: Anonymous 2011-03-05 21:03

>>32
CoffeeScript seems to take this idea of "oh JavaScript, everything's an object, and look closures and callbacks" to the extreme.
Didn't see anything extreme about that in the documentation. Is it possible that you're easily impressed? The syntax does seem to increase conciseness, though.

Name: Anonymous 2011-03-05 23:38

Tried my first bit of lisp just now; googled the syntax for ifs and functions then wrote this..
(defun factorial (num) (if (> num 1) (* num (factorial (- num 1))) 1))

Should probably work on my formatting, just don't really know the normal style for lisp. I imagine it's something like..
(defun factorial (num)
        (if (> num 1)
                (* num (factorial
                        (- num 1)))
                1))

Name: Anonymous 2011-03-05 23:45

>>35
(defun factorial (num)
    (if (> num 1)
        (* num (factorial (- num 1)))
        1
    )
)


Haters gonna hate.

Name: Anonymous 2011-03-06 0:18

>>35-36
Terrible!
Please get Emacs, or some Lisp IDE (if you really don't like Emacs). They are able to automatically indent Lisp code. Of course, after having used them for so long, I do know all the indentation rules myself (sometimes I even add rules for my own macros), but I wouldn't indent manually - indentation is there to help you quickly understand what some code is doing, it's quite helpful in lisp as indentation is used to serve as a visual cue as to the purpose/functionality of the code, things which pure syntactic sugar does for you in other languages.

Example proper indentation for your code:

(defun factorial (num)
  (if (> num 1)
      (* num (factorial
              (- num 1)))
      1))
;;; or
(defun factorial (num)
  (if (> num 1)
      (* num (factorial (- num 1)))
      1))


If you MUST know the rules explicitly, here's some hints for CL ( http://www.labri.fr/perso/strandh/Teaching/MTP/Common/Strandh-Tutorial/indentation.html ) and Scheme ( http://mumble.net/~campbell/scheme/style.txt )

Name: >>37 2011-03-06 0:21

Oh, and you don't need to type parens out either, use Paredit (Emacs mode) to structurally edit your code. If you're new to Emacs, you basically need (for CL): SLIME, Paredit, Redshank(optional), Hyperspec, and a few CL implementations installed and configured to work with SLIME. You may also choose some cosmetic changes like themes for Emacs and whatnot.

Name: 36 2011-03-06 0:25

>>37
If the editor can readily autoindent the code, then why do other Lispers get pissy with my code? If they don't like my indentation style, they can just configure their stupid editors to reindent it. And it goes the other way too -- if I don't like their indent style, I can re-indent everything the way I like. For example, I prefer closing parentheses associated with special forms on separate lines to make the distinction clear.

Name: Anonymous 2011-03-06 0:36

Read the first document I linked.
Basically, the answer is that all Lispers indent the code the same (or more or less very similar with very small differences depending on configuration details of their editor) and that we use the indentation as a cue to easily understanding the code. Randomly indented parens can be very messy as you're removing the few visual cues that we have (we have no real syntactic sugar). It also makes me look at the parens - I almost never look at the parens, I read the code by indentation, however the parens give you a lot of flexibility when WRITING the code.

When I see indentation like yours, it makes it hard for me to read the code and I have to either slowly read it and match parens (nasty), or copy it to Emacs and hit a key chord to indent it. Either way, you're making me spend some extra 10-20 seconds starting up Emacs and indenting your code when I could be reading it directly.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List