Skip to content

Instantly share code, notes, and snippets.

@follesoe
Last active February 11, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save follesoe/26f08637b8d806bbac2e to your computer and use it in GitHub Desktop.
Save follesoe/26f08637b8d806bbac2e to your computer and use it in GitHub Desktop.
Forslag til kildekode på folie på glassvegg
(define factorial
(lambda (n)
(if (= n 0)
1
(* n (factorial (- n 1))))))
*-------------------------------
*
* Draw entire 10 x 3 screen from scratch
*
*-------------------------------
SURE
lda #1
sta genCLS ;clear screen
jsr setback ;draw on bg plane
jsr getprev ;get 3 rightmost blocks of screen to left
lda SCRNUM
jsr calcblue ;get blueprint base addr
/*--------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows" in client area
(c) Charles Petzold, 1990
--------------------------------------------------------*/
#include <windows.h>
long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
static char szAppName[] = "HelloWin" ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
if (!hPrevInstance)
{
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass) ;
}
hwnd = CreateWindow (szAppName, // window class name
"The Hello Program", // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, nCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, "Hello, Windows!", -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Aside from the antique C syntax and the weird idiosyncratic formatting, I think most people would agree that compared with Microsoft's original, mine is a model of clarity and brevity.
Eventually I got tired of people complaining how long my "Hello World" program was, so Programming Windows, 5th edition (1998) began with this program:
/*--------------------------------------------------------------
HelloMsg.c -- Displays "Hello, Windows 98!" in a message box
(c) Charles Petzold, 1998
--------------------------------------------------------------*/
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
#************************************************************************
# ENTRY POINTS: ?GUIDSUB FOR THE IGNITION ALGORITHM, LUNLAND FOR SERVOUT
#************************************************************************
# IGNITION ALGORITHM ENTRY: DELIVERS N PASSES OF QUADRATIC GUIDANCE
?GUIDSUB EXIT
CAF TWO # N = 3
TS NGUIDSUB
TCF GUILDRET +2
GUIDSUB TS NGUIDSUB # ON SUCCEEDING PASSES SKIP TTFINCR
TCF CALCRGVG
# NORMAL ENTRY: CONTROL COMES HERE FROM SERVOUT
LUNLAND TC PHASCHNG
OCT 00035 # GROUP 5: RETAIN ONLY PIPA TASK
TC PHASCHNG
OCT 05023 # GROUP 3: PROTECT GUIDANCE WITH PRIO 21
OCT 21000 # JUST HIGHER THAN SERVICER'S PRIORITY
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
@follesoe
Copy link
Author

follesoe commented Feb 2, 2016

let (|>) x f = f x

@mollerse
Copy link

mollerse commented Feb 2, 2016

DEFINE qsort == [small] [] [uncons [>] split] [enconcat] binrec.

Quicksort i Joy

@mollerse
Copy link

mollerse commented Feb 2, 2016

(defun Y (f)
  ((lambda (x) (funcall x x))
   (lambda (y)
     (funcall f (lambda (&rest args)
          (apply (funcall y y) args))))))

Y-combinator i CommonLisp

@bendiksolheim
Copy link

Fibonacci i Haskell

fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

@bendiksolheim
Copy link

Swap to varibaler uten en temp-variabel:

a^=b;
b^=a;
a^=b;

@kristoffer-dyrkorn
Copy link

Vent på vertical retrace:

    mov     dx,03dah
waitstart:      
    in      al,dx
    and     al,08h
    jnz     waitstart
waitend:            
    in      al,dx
    and     al,08h
    jz      waitend

@kristoffer-dyrkorn
Copy link

Bresenhams algoritme:

void line(int x0, int y0, int x1, int y1) {

  int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
  int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1; 
  int err = (dx>dy ? dx : -dy)/2, e2;

  for(;;){
    setPixel(x0,y0);
    if (x0==x1 && y0==y1) break;
    e2 = err;
    if (e2 >-dx) { err -= dy; x0 += sx; }
    if (e2 < dy) { err += dx; y0 += sy; }
  }
}

@kristoffer-dyrkorn
Copy link

FFT:

def fft(x):
    N = len(x)
    if N <= 1: return x
    even = fft(x[0::2])
    odd =  fft(x[1::2])
    T= [exp(-2j*pi*k/N)*odd[k] for k in range(N//2)]
    return [even[k] + T[k] for k in range(N//2)] + [even[k] - T[k] for k in range(N//2)]

@trondkla
Copy link

Robert'); DROP TABLE students; --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment