Code Select
(let (a 1 b 2 c 3 d '(4 5 6 7) e (flat (list a b c d)))
(println (list a b c d e)))
;−> (1 2 3 (4 5 6 7) (1 2 3 4 5 6 7))
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
(context 'Point)
(set 'x 0 'y 0)
(context MAIN)
(new Point 'p1)
(set 'p1:x 3 'p1:y 4)
(new Point 'p2)
(set 'p2:x 3 'p2:y 4)
(println (= p1 p2)) ; => nil : p1 and p2 are two distinct objects
(set 'p3 p1)
(println (= p1 p3)) ; => true : p1 and p3 refer to the same object
; this demonstrates that
(set 'p1:x 7)
(println p3:x) ;=> 7
;; with FOOP :
(define (Point:Point x y)
(list Point x y))
(set 'p1 (Point 3 4))
(set 'p2 (Point 3 4))
(println (= p1 p2)) ; => true
(set 'p3 p1)
(println (= p1 p3)) ; => true
(set 'p1 (Point 7 4))
(println p1) ;=> (Point 7 4)
(println p3) ;=> (Point 3 4)
(context 'Point)
(set 'x 0 'y 0)
(context MAIN)
(new Point 'p1)
(set 'p1:x 3 'p1:y 4)
(println p1:x) ; => 3
(define (Point:Point x y)
(list Point x y))
(set 'p1 (Point 3 4))
(println p1) ;=> (Point 3 4)
#!/usr/bin/newlisp
(context 'rectangle)
; Class 'rectangle' with a constructor method
(define (rectangle:rectangle (width 30) (height 15))
(set 'w width)
(set 'h height)
(set 'nam "rectangle"))
(define (perimeter)
(string "(" w " + " h ") x 2 = " (mul (add w h) 2)))
(define (area)
(string w " x " h " = " (mul w h)))
(define (measure)
(println "A " nam " of " w " by " h)
(println "has a surface area of " (area) ",")
(println "and a perimeter of " (perimeter) ".n"))
(context MAIN)
(context 'square)
; Class 'square' inherits from 'rectangle'
(new rectangle)
; Constructor of 'square'
(define (square:square (side 10))
(set 'w side)
(set 'h side)
(set 'nam "square"))
(context MAIN)
; main program
(new rectangle 'fig1)
(fig1 27 12)
(fig1:measure)
(new square 'fig2)
(fig2 13)
(fig2:measure)
;---------------------
; initialize GUI-server
;---------------------
(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init)
;-------------------------
; constants and variables
;-------------------------
; GUI size:
(constant 'WIDTH 500 'HEIGHT 300)
;----------------
; create the GUI
;----------------
(gs:frame 'WIN 100 100 WIDTH (+ HEIGHT 32) "NewLISP 9.2.6 on Win32")
;; the console
(gs:text-area 'Console 'action WIDTH HEIGHT)
(gs:set-editable 'Console nil)
(gs:set-background 'Console gs:white)
(gs:set-font 'Console "Monospaced" 12 "plain")
(gs:set-foreground 'Console gs:black)
(gs:add-to 'WIN 'Console)
(gs:set-visible 'WIN true)
;-----------------
; define actions
;-----------------
(define (action))
;------------------
; events loop
;------------------
(gs:listen true)
#!/usr/bin/newlisp
;=======================================
; Turtle graphics (NewLISP + GUI-server)
; tree.lsp
;
; NewLISP v9.2.4 B. Carette - nov. 2007
;=======================================
;; initialization GUI-server
(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init)
;; constants & variables
(constant 'PI2 (acos 0) 'DP2 (div PI2 90)) ; degre -> radian
(constant 'WIDTH 400 'HEIGHT 400) ; size of the window
(set 'coorX 0 'coorY 0) ; coordinates of the "turtle"
(set 'dir 0) ; orientation (heading to the north)
(set 'back-color '(1 1 1)) ; backcolor = white
(set 'pen-color '(0 0 0)) ; pen = black
(set 'pen true) ; pen is down
;; building the GUI
(gs:frame 'WIN 100 100 WIDTH (+ HEIGHT 34) "NewLISP Turtle")
(gs:canvas 'Canvas)
(gs:set-size 'Canvas WIDTH HEIGHT)
(gs:set-background 'Canvas back-color)
(gs:add-to 'WIN 'Canvas)
(gs:set-visible 'WIN true)
;; graphic procedures (commands for the turtle)
(define (home)
; center of the screen, heading to north
(pen-up)
(set 'coorX 0 'coorY 0)
(set 'dir PI2)
(pen-down))
(define (clear-screen)
(gs:delete-tag 'L)
(home))
(define (pen-down)
(set 'pen true))
(define (pen-up)
(set 'pen nil))
(define (pendown?)
pen)
(define (set-pos x y)
; set the new positionof the turtle
(set 'coorX (add (div WIDTH 2) coorX) 'coorY (sub (div HEIGHT 2) coorY))
(set 'newX (add (div WIDTH 2) x) 'newY (sub (div HEIGHT 2) y))
(if (pendown?)
; draw if pen is down
(gs:draw-line 'L (int coorX) (int coorY) (int newX) (int newY) pen-color ))
(set 'coorX x 'coorY y))
(define (pos)
; return current position
(cons coorX coorY))
(define (forward dist)
(set-pos (add coorX (mul dist (cos dir)))
(add coorY (mul dist (sin dir)))))
(define (backward dist)
(forward (- dist)))
(define (set-heading angle)
; set the orientation
(set 'dir (mod angle 360)))
(define (orientation)
; return current heading
dir)
(define (right degre)
; set orientation to the right (in degre)
(set-heading (sub dir (mul degre DP2))))
(define (left degre)
; set orientation to the left (in degre)
(set-heading (add dir (mul degre DP2))))
;; main program (a Turtle Graphics program)
(define (tree size)
(gs:set-stroke (div size 10))
(set 'pen-color (amb gs:red gs:green gs:blue))
(forward size)
(if (> size 5)
(begin
(right 30)
(tree (/ size 2))
(right 30)
(tree (/ size 2))
(left 90)
(tree (/ size 2))
(left 30)
(tree (/ size 2))
(right 60)
(gs:update)))
(pen-up)
(backward size)
(pen-down)
(gs:set-stroke 0.0))
(clear-screen)
(pen-up)
(backward 100)
(pen-down)
(tree 150)
;; boucle d'attente d'événements
(gs:listen)
#!/usr/bin/newlisp
;================================================
; Context and GUI
; ----- newlisp 9.1.12 --------- août 2007 -----
; Bertrand Carette (aka newBert)
;================================================
[text]
Resistors are small tubular parts rimmed with colored stripes (usually 3).
These stripes show the numerical value of the resistor according to the
following code :
Each color corresponds to one of the numerals from zero to nine:
black=0, brown=1, red=2, orange=3, yellow=4, green=5, blue=6, purple=7,
gray=9, white=10.
Resistor is positioned so as to put the colored stripes on the left.
The value of the resistor, expressed in ohm, is obtained by reading the
stripes from the left : the first two stripes show the first two numerals
of the value, next the third stripes show the amount of zeros.
Example:
yellow, purple and green stripes => 4700000 Ohm or 4700 kOhm or 4.7 MOhm.
[/text]
(context 'Application)
(define (Application:Application)
; Constants and variables
; -> color codes (cc) for the values from zero to nine :
; black = 0 brown = 1 red = 2
; orange = 3 yellow = 4 green = 5
; blue = 6 purple = 7 grey = 8
; white = 9
(set 'cc '( (0.00 0.00 0.00) (0.68 0.31 0.00) (1.00 0.00 0.00)
(1.00 0.54 0.09) (1.00 1.00 0.00) (0.00 1.00 0.00)
(0.00 0.00 1.00) (0.39 0.00 0.78) (0.50 0.50 0.50)
(1.00 1.00 1.00)))
; -> list of the three colors (black by default)
(set 'coul (list (cc 0) (cc 0) (cc 0)))
; Building the main window
(gs:frame 'Fen 100 100 280 240 "Color Codes")
(gs:set-flow-layout 'Fen "center")
; -> creating then canvas where we'll draw the resistor
(gs:panel 'PanCanevas 250 120)
(gs:set-color 'PanCanevas '(0.98 0.98 0.98))
(gs:set-bevel-border 'PanCanevas "lowered")
(gs:canvas 'Can)
(gs:set-size 'Can 250 120)
(gs:add-to 'PanCanevas 'Can)
; -> drawing the resistor
(dessine-resistance (coul 0) (coul 1) (coul 2))
; -> creating the entry field
(gs:panel 'PanEntree )
(gs:set-border-layout 'PanEntree )
(gs:label 'Text "Value of the resistor (Ohm):")
(gs:text-field 'Entree 'change-couleurs 20)
(gs:add-to 'PanEntree 'Text "north" 'Entree "south")
; -> buttons
(gs:panel 'PanBouton)
(gs:set-flow-layout 'PanBouton "center" 70 2)
(gs:button 'BMontre 'evalue-entree "Show")
(gs:button 'BQuitte 'quitte "Quit")
(gs:add-to 'PanBouton 'BMontre 'BQuitte)
; -> laying out the main frame
(gs:add-to 'Fen 'PanCanevas 'PanEntree 'PanBouton)
(gs:set-visible 'Fen true)
; Event loop
(gs:listen) )
(define (dessine-resistance c1 c2 c3)
; Draw a resistor with three colored stripes
(gs:set-canvas 'Can)
; -> drawing
(gs:set-stroke 2.0)
(gs:draw-line 'Fil 10 50 240 50 gs:orange) ;wire
(gs:set-stroke 1.0)
(gs:fill-rect 'Res 65 30 120 40 gs:lightGray)
; -> three stripes (black by default)
(gs:set-stroke 0.0)
(gs:fill-rect 'R1 80 30 17 40 c1)
(gs:fill-rect 'R2 104 30 17 40 c2)
(gs:fill-rect 'R3 128 30 17 40 c3)
; -> updating the canvas
(gs:update))
(define (evalue-entree id text)
(gs:get-text 'Entree 'change-couleurs))
(define (change-couleurs id text)
; Displays the colors which correspond to the input values
; -> getting and evaluating entries
(set 'vlch (base64-dec text))
(set 'v (float vlch))
(if (or (not v) (<v> v 1e+011))
(signale-erreur)) ; bad input
(set 'li (dup 0 3)) ; list of the 3 codes to display
(set 'logv (int (log v 10))) ; integer part of the logarithm
(set 'ordgr (pow 10 logv)) ; idea of the size
; -> extracting the first significant numeral :
(nth-set (li 0) (/ v ordgr)) ; integer part
(set 'decim (sub (div v ordgr) (li 0))) ; decimal part
; -> extracting the second significant numeral :
(nth-set (li 1) (round (mul decim 10)))
; -> amount of zeros to place beside the 2 significant numerals :
(nth-set (li 2) (- logv 1))
; -> coloring the 3 stripes :
(dotimes (n 3)
(nth-set (coul n) (cc (li n))))
(dessine-resistance (coul 0) (coul 1) (coul 2))
; -> focus on entry field
(gs:request-focus 'Entree))
(define (signale-erreur)
; Displays "error" then gets back to entry field
(gs:clear-text 'Entree)
(gs:set-text 'Entree "Error !")
(sleep 1000); pause 1 seconde
(gs:clear-text 'Entree)
(gs:request-focus 'Entree)
(gs:listen))
(define (quitte)
(exit))
(context MAIN)
;; initialisation GUI-Server
(if (= ostype "Win32")
(load (string (env "PROGRAMFILES") "/newlisp/guiserver.lsp"))
(load "/usr/share/newlisp/guiserver.lsp"))
(gs:init)
;; main program
(Application)
(exit)
;; end of script
(print "Please, enter a number : ")
(set 'nn (eval-string (read-line)))
Please, enter a number : 2Quote
newLISP v.8.8.8 on Win32 MinGW, execute 'newlisp -h' for more info.> nn
2
>
newLISP v.8.8.8 on Win32 MinGW.Quote
> Please, enter a number : 2
2> nn
nil
>
; NULLsoft Scriptable Install System
; make a newLISP Script executable
; Name of the installer (don't really care here because of silent below)
Name "Demo"
; Don't want a window, just unpack files and execute
SilentInstall silent
; Set a name for the resulting executable
OutFile "tcltk-app.exe"
; Set an icon (optional)
Icon "C:Program Filesnewlispnewlisp.ico"
; The installation directory
InstallDir "$TEMPtemp_NSIS"
; The stuff to install
Section ""
; Set output path to the installation directory.
SetOutPath $INSTDIR
; put here requiered files
File "C:Program Filesnewlispnewlisp-tk.exe" ; newLISP-tk interpreter
File "C:Program Filesnewlisptcltk-app.lsp" ; put the newLISP script
; Execute and wait for the newLISP script to end
ExecWait '"$INSTDIRnewlisp-tk.exe" "-s" "tcltk-app.lsp"'
; Delete unpacked files from hard drive
RMDir /r $INSTDIR
SectionEnd