Menu

Show posts

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

Topics - cameyo

#61
Maximum Product Sublist

Given a list that contains both positive and negative integers, find the product of the maximum product sublist.
(define (maxProd lst)
  (local (n maxprod pos neg)
    (setq n (length lst))
    (setq pos (array n '(0)))
    (setq neg (array n '(0)))
    (setq (pos 0) (lst 0)) ; pos[i] contains the positive product up to lst[i]
    (setq (neg 0) (lst 0)) ; neg[i] contains the negative product up to lst[i]
    (setq maxprod (lst 0))
    (for (i 0 (- n 1))
      ; the maximum of the three values
      (setq (pos i) (max (max (* (pos (- i 1)) (lst i)) (* (neg (- i 1)) (lst i))) (lst i)))
      ; the minimum of the three values
      (setq (neg i) (min (min (* (pos (- i 1)) (lst i)) (* (neg (- i 1)) (lst i))) (lst i)))
      (setq maxprod (max maxprod (pos i)))
    )
    maxprod
  )
)

(setq lst '(6 -3 -10 0 2))
(maxProd lst)
;-> 180
Sublist: (6 -3 -10)

(setq lst '(-1 -3 -10 0 60))
(maxProd lst)
;-> 60
Sublist: (60)

(setq lst '(-2 -3 0 -2 -40))
(maxProd lst)
;-> 80
Sublist: (-2 -40)

(setq lst '(-1 -2 -3))
(maxProd lst)
;-> 6

(setq lst '(0 -1))
(maxProd lst)
;-> 0

(setq lst '(0 0 0 0))
(maxProd lst)
;-> 0

Please, post your version :-)

cameyo



p.s. The "Maximum Sum sublist" problem is solved with the Kadane algorithm
#62
I use this function to group the elements of a list:
(define (take n lst) (slice lst 0 n))
(define (drop n lst) (slice lst n))

(define (group-by-num n lst)
   (if (null? lst) '()
      (cons (take n lst) (group-by-num n (drop n lst)))
   )
)

(setq lst '(0 1 2 3 4 5 6 7 8 9))

(group-by-num 2 lst)
;-> ((0 1) (2 3) (4 5) (6 7) (8 9))

(group-by-num 3 lst)
;-> ((0 1 2) (3 4 5) (6 7 8) (9))

(setq lst '(1 2 3 4 5 6 7 8 9 10 11 12))

(group-by-num 2 (group-by-num 2 lst))
;-> (((1 2) (3 4)) ((5 6) (7 8)) ((9 10) (11 12)))

Please, post your version.
#63
I use the following function to calculate the maximum nesting deep of a list:
(define (nesting lst)
  (cond ((null? lst) 0)
        ((atom? lst) 0)
        (true (max (+ 1 (nesting (first lst)))
                   (nesting (rest lst))))
  )
)

(nesting '(a (((b c (d)))) (e) ((f)) g))
;-> 5

Is there a better/faster way to do this?

Thanks.
#64
I have the following problem:
; create two contexts (A1, A2)
> (context 'A1)
;-> A1
A1> (context MAIN)
;-> MAIN
> (context 'A2)
;-> A2
A2> (context MAIN)
;-> MAIN
; create a variable "a"
> (setq a 2)
;-> 2
> (context 'A1)
;-> A1
; context A1 do not see the variable "a"
A1> a
;-> nil
; context A1 do not see the variable "a"
A1> (context 'A2)
;-> A2
A2> a
;-> nil
A2> (context MAIN)
;-> MAIN
> a
;-> 2

; set variabile "a" to global
(global 'a)

; but A1 and A2 do not see the variable "a"
> (context 'A1)
;-> A1
A1> a
;-> nil
A1> (context 'A2)
;-> A2
A2> a
;-> nil

But this works (do not check variable value on contexts before globalize it:

A2> (context MAIN)
;-> MAIN
> (context 'B2)
;-> B2
B2> (context MAIN)
;-> MAIN
> (context 'B1)
;-> B1
B1> (context MAIN)
;-> MAIN
> (setq b 3)
;-> 3
> (global 'b)
;-> b
> (context B1)
;-> B1
B1> b
;-> 3
B1> a
;-> 2
B1> (context B2)
;-> B2
B2> b
;-> 3
B2> a
;-> 2
B2>

B2> (context MAIN)
MAIN
; the context A1 see only "b" ("a" is nil)
> (context A1)
A1
A1> a
nil
A1> b
3
A1>

; change variable value
A1> (context MAIN)
MAIN
> (setq a 10)
10
> a
10
> (context B1)
B1
B1> a
10
B1> (context A1)
A1
A1> a
nil
A1>

Why A1 and A2 do not see the variable "a" ?

what am i doing wrong?

Is there a rule to define global variables and contexts?
#65
newLISP in the real world / Italian notes on newlisp
April 19, 2019, 02:26:31 AM
I'm writing some newlisp notes (in italian language):

https://github.com/cameyo42/newLISP-Note">//https://github.com/cameyo42/newLISP-Note

It is a work in progress and I am a beginner.

Advice, suggestions and corrections are welcome.

cameyo
#66
newLISP and the O.S. / Build newLISP for win10 64bit
February 16, 2019, 06:37:28 AM
Which tools i need to build newLISP from source (windows 10 - 64 bit) ?

MinGW or mingw-w64 ?

Which version of gcc ?

Can someone point me on the right direction?

Thanks
#67
Whither newLISP? / Retrieving the value of a symbol
February 11, 2019, 10:33:07 AM
These expressions generate an error:
(set '"name") -> ERR: symbol expected in function set : '"name"
(set (quote "name") 3) -> ERR: symbol expected in function set : '"name"

But the following are valid (then "name" is a valid symbol):
(setf '"name" 3) -> 3
(setq "name" 3) -> 3

Now the problem: how to retrieve the value of the symbol "name"?
(println "name") -> name
(setq a "name")
(println a) -> "name"

Thanks
#68
newLISP and the O.S. / Windows 10 ANSI color
February 07, 2019, 03:22:02 AM
The console in Windows 10 support VT (Virtual Terminal) / ANSI escape sequences but it is disable by default.

To activate it:

in registry key [HKEY_CURRENT_USERConsole], create or set the VirtualTerminalLevel DWORD value to 1.

(or from cmd.exe : reg add HKCUConsole /v VirtualTerminalLevel /t REG_DWORD /d 1)

To deactivate it:

in registry key [HKEY_CURRENT_USERConsole], create or set the VirtualTerminalLevel DWORD value to 0.



You must open a new console window for changes to take effect.

For example use the ANSI color when debugging:
(trace-highlight "27[0;31m" "27[0;0m") ;red text color
(trace-highlight "27[0;7m" "27[0;0m")  ;negative

cameyo
#69
newLISP newS / Ask info about newLISP
January 03, 2019, 04:57:10 AM
I'd like to get some infos about the development of newLISP.

When will be out a new version?

Thanks.
#70
newLISP in the real world / factor-group function
November 25, 2018, 01:45:59 AM
To solve a project euler problem i have written this factor-group function:
(define (factor-group x)
  (letn (fattori (factor x)
         unici (unique fattori))
    (transpose (list unici (count unici fattori)))))

(factor-group 2000)
;-> ((2 4) (5 3))
(factor-group 232792560)
;-> ((2 4) (3 2) (5 1) (7 1) (11 1) (13 1) (17 1) (19 1))

Do you known a better/faster way?

Thanks.



cameyo
#71
newLISP in the real world / Understand "curry" function
November 25, 2018, 01:28:10 AM
This is what I understand ...



I can map a function with multiple arguments in this way:
(map pow '(2 1) '(3 4))
;-> (8 1)

where: 8 = 2^3, 1 = 1^4

But, if the list of arguments are within a list:
(setq lst '((2 1) (3 4)))
I got an error:
(map pow lst)
ERR: value expected in function pow : '(2 1)

I use "curry" to solve this problem:
(map (curry apply pow) lst)
;-> (2 81)

where: 2 = 2^1, 81 = 3^4

Ok, transpose the list of arguments:
(map (curry apply pow) (transpose lst))
;-> (8 1)

This is equivalent to:
(map (lambda(x) (apply pow x)) (transpose lst))
;-> (8 1)

We can define a user function too:
(define (mypow lst)
  (if (null? lst) '()
      (cons (pow (nth '(0 0) lst) (nth '(0 1) lst)) (mypow (rest lst)))
  )
)

(setq lst '((2 1) (3 4)))
(mypow (transpose lst))
;-> (8 1)

Another example:
(map max '(3 5) '(2 7))
;-> (3 7)
(map (curry apply max) '((3 5) (2 7)))
;-> (5 7)
(map (curry apply max) (transpose '((3 5) (2 7))))
;-> (3 7)


Did I get it right?

cameyo

p.s. sorry for poor english...
#72
newLISP in the real world / Notepad++ bundle
November 21, 2018, 08:09:00 AM
Download: https://github.com/cameyo42/notepadpp-newlisp">//https://github.com/cameyo42/notepadpp-newlisp



Add newlisp syntax highlighting

Copy all the text of the file: newlisp-udl.xml

and paste it inside the section:<NotepadPlus> ... </NotepadPlus>

of the file: userDefineLang.xml (located at: c:Users<username>AppDataRoamingNotepad++)
Example
<NotepadPlus>
    <UserLang name="newLISP" ext="lsp" udlVersion="2.1">
    ...
    </UserLang>
</NotepadPlus>
   

The newlisp keywords are from primes.h (newlisp source).

The actual highlight colors are for "obsidiane" theme of notepad++.

You can change (easily) the colors as you like.



Open newlisp help from notepad++

Add the line: <Command name="newLISP Help" Ctrl="yes" Alt="yes" Shift="no" Key="112">chrome file:///C:/Program%20Files%20(x86)/newlisp/newlisp_manual.html#$(CURRENT_WORD)</Command>
inside the section: <UserDefinedCommands> ... </UserDefinedCommands>

of the file: shortcut.xml (located at: c:Users<username>AppDataRoamingNotepad++)
Example
<UserDefinedCommands>
    <Command name="newLISP Help" Ctrl="yes" Alt="yes" Shift="no" Key="112">chrome file:///C:/Program%20Files%20(x86)/newlisp/newlisp_manual.html#$(CURRENT_WORD)</Command>
   
</UserDefinedCommands>

Note: change the path to point to your newlisp help file

Now you can select a word and press Ctrl+Alt+F1 to open newlisp help file.

The shortcut is Ctrl+Alt+F1, but you can change it.



Execute newlisp code from notepad++

Download and install autohotkey (http://www.autohotkey.com">http://www.autohotkey.com).

Run the script npp-newlisp.ahk (double click it).

Run notepad++

Press Win+F12 to start newlisp REPL

Now, from notepad++, you can:

1) Execute the expression of current line pressing: Left-Shift + Enter

2) Execute a selected block of expression pressing: Right-Shift + Enter

After the execution of the expressions, notepad++ is the active application.

Note:

When selecting a block of expression be sure to begin and end the selection

with a blank line (or use [cmd] [/cmd]).

Note:

The script npp-newlisp.ahk exchange the brackets () and [] in the keyboard.

You can edit the file to disable this (you must comment two lines).

The script also enable other shortcuts... see the source.

Happy coding



cameyo
#73
newLISP in the real world / Apply error message
November 17, 2018, 07:03:06 AM
The following expression give me an error:
(apply map list '((a 1) (b 2) (c 3)))
QuoteERR: list expected in function apply : list@4095B0

Instead, I wish the output was:
Quote((a b c) (1 2 3))

Can someone help me?

Thanks



cameyo
#74
newLISP in the real world / FUNCALL and GETDEF
October 26, 2018, 12:29:24 AM
Hi all,

i'm looking for the functions FUNCALL and GETDEF.

Are there equivalent functions in newLisp?

Sorry for dumb question...i'm a newbie.

cameyo
#75

;=====================================
; edit2newlisp.ahk
; Connect newLisp and editor
; AutoHotkey:     1.x
; Language:       English
; Platform:       Windows7
; Author:         cameyo - 2017
; License:        MIT license
;=====================================
; Hotkeys:
; Win-F12 -> Start newLisp
; Left_Shift-Enter -> Evaluate current line
; Right_Shift-Enter -> Evaluate selected block

;=====================================
; How to set your editor:
; 1: run winspy utility (bundled with autohotkey)
; 2: run your editor
; 3: copy the ahk_class text value
; 4: set the global variable editor to "<ahk_class>"
; 5: That's all.
; EXAMPLES:
; (notepad++):
;global editor = "notepad++"
; (SciTE):
;global editor = "SciTEWindow"
; (Notepad)
;global editor = "Notepad";
; (PSPad)
;global editor = "TfPSPad.UnicodeClass"
; (Programmer's Notepad)
;global editor = "ATL:006AD5B8"
; (Sublime Text)
;global editor = "PX_WINDOW_CLASS"
;-------------------------------------
global editor = "notepad++"
;-------------------------------------
; To use (and view) the greek letters the editor must be set to UTF-8 encoding.
;=====================================

;=====================================
; Basic hotkeys symbol
;=====================================
; # Win key
; ! Alt key
; ^ Control key
; + Shift key

;=====================================
; General statements
;=====================================
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force ; Allow reload script
#WinActivateForce ; activate window with forceful method

;=====================================
; Global variables
;=====================================
#EscapeChar
global hasRunnewLisp = 0
global newLispPID = 0
global editorPID = 0

;---------------------------
OpennewLisp()
{
  if %hasRunnewLisp% = 0
  {
    Run newlisp.exe,,,newLispPID
    hasRunnewLisp = 1
    ReturnToEditor()
    return
  }
  Process, Exist, %newLispPID%
  if %ErrorLevel% = 0
  {
    Run newlisp.exe,,,newLispPID
    hasRunnewLisp = 1
    ReturnToEditor()
    return
  }
}
;---------------------------
ReOpennewLisp()
{
  WinClose, ahk_pid %newLispPID%
  OpennewLisp()
}
;---------------------------
SendTonewLisp()
{
  OpennewLisp()
  WinWait, ahk_pid %newLispPID%
  WinActivate, ahk_pid %newLispPID%
  WinWaitActive, ahk_pid %newLispPID%

  ; Past via menu (must disable SendMode Input)
  ;Send {Alt Down}{Space}{Alt up}ep{Enter}

  ; Past with send clipboard
  StringReplace clipboard2, clipboard, rn, n, All
  SendInput {Raw}%clipboard2%n
}
;---------------------------
ReturnToEditor()
{
  WinActivate, ahk_pid %editorPID%
  SendInput {End}
}
;---------------------------
PassLine()
{
  WinGetCLASS, currentClass, A
  If currentClass = %editor%
  {
    WinGet, editorPID, PID, A
    SendInput {Home}{Shift Down}{End}{Shift Up}{Ctrl Down}c{Ctrl Up}{End}
    SendTonewLisp()
    ReturnToEditor()
  }
}
;---------------------------
PassBlock()
{
  WinGetCLASS, currentClass, A
  If currentClass = %editor%
  {
    WinGet, editorPID, PID, A
    SendInput {Ctrl Down}c{Ctrl Up}
    SendTonewLisp()
    ReturnToEditor()
  }
}
;---------------------------
; Eval current line (Left Shift - Enter)
LShift & Enter:: PassLine()
;---------------------------
; Eval selected block (Right Shift - Enter)
RShift & Enter:: PassBlock()
;---------------------------
; Run newLisp (Win-F12)
#$F12:: ReOpennewLisp()

;================================================
; REPL enhancements
; Work only on console window
#IfWinActive ahk_class ConsoleWindowClass

;=============================
; Scroll command window back and forward
; Ctrl+PageUp / PageDown
;=============================
^PgUp:: SendInput {WheelUp}

^PgDn:: SendInput {WheelDown}

;=============================
; Paste in REPL
; Ctrl-V
; Better version
;=============================
^V::
StringReplace clipboard2, clipboard, rn, n, All
SendInput {Raw}%clipboard2%
return

;=============================
; Paste in REPL
; Ctrl-V
; Old version (must disable SendMode Input)
;=============================
;^V::
; English menu (Edit->Paste)
;Send !{Space}ep
;return

#IfWinActive
;================================================

;=============================
; Greek Letters (lowercase)
; Control-Win-<char>
; char 'j' can't be used (REPL)
; char 'q' is free...
; To use (and view) the greek letters the editor must be set to UTF-8 encoding.
;=============================
^#a:: SendInput {U+03B1} ; alpha
^#b:: SendInput {U+03B2} ; beta
^#g:: SendInput {U+03B3} ; gamma
^#d:: SendInput {U+03B4} ; delta
^#y:: SendInput {U+03B5} ; epsilon (y)
^#z:: SendInput {U+03B6} ; zeta
^#e:: SendInput {U+03B7} ; eta
^#h:: SendInput {U+03B8} ; theta (h)
^#i:: SendInput {U+03B9} ; iota
^#k:: SendInput {U+03BA} ; kappa
^#l:: SendInput {U+03BB} ; lambda
^#m:: SendInput {U+03BC} ; mu
^#n:: SendInput {U+03BD} ; nu
^#x:: SendInput {U+03BE} ; xi
^#o:: SendInput {U+03BF} ; omicron
^#p:: SendInput {U+03C0} ; pi
^#r:: SendInput {U+03C1} ; rho
^#s:: SendInput {U+03C3} ; sigma
^#t:: SendInput {U+03C4} ; tau
^#u:: SendInput {U+03C5} ; upsilon
^#f:: SendInput {U+03C6} ; phi (f)
^#c:: SendInput {U+03C7} ; chi
^#v:: SendInput {U+03C8} ; psi (v)
^#w:: SendInput {U+03C9} ; omega (w)
#76
Has anyone got a newLISP syntax file for notepad++?



Thanks