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 - lyl

#1
I use a function "f" to store data by calling another function "g" like this:
(define (g (x y)) x)
(define (f data)
  (setf (nth '(0 0 1) g) data)
  )
 
;;test:
(f 1)
(g) ;; -> 1  Right. This is what I want.
(g) ;; -> 1  Right. This is what I want.
(f '(+ 1 2))
(g) ;; -> 3  This is not what I want. I can't understand why the quoted list '(+ 1 2) is evaluated?. I just want to get the list itself '(+ 1 2)


By contrast,
(define (g x) x)
(define (f data) (g data))
(f '(+ 1 2)) ;; -> (+ 1 2)   The list is not evaled.


Is there a better way to prevent this kind of unwanted eval during the parameter transfer?
#2
newLISP in the real world / Setup functions by setq
August 27, 2020, 10:54:17 PM
I'd like to make a series of functions whose names come from a list, as shown by the following codes:
(setq a '(a1 a2))
(dolist (x a)
  (let (z $idx)
    (setq x (lambda(y) z))
    ))

what I want is to get two functions

a1: (lambda (y) 0)

a2: (lambda (y) 1)

But I fail.

What's wrong with my code, and how to solve it?
#3
Whither newLISP? / "place" in the function "inc"
August 26, 2020, 02:56:32 AM
The meaning of "place" in the syntax: (inc place [num]) is:

either a symbol or a place in a list structure holding a number, or a number returned by an expression.(from the newlisp manuel)



I still don't quite understand the real meaning of "place", as in the following code coming from 《Code Patterns in newLISP》:
;; sum accumulator
(define (sum (x 0)) (inc 0 x))

(sum 1)    → 1
(sum 2)    → 3
(sum 100)  → 103
(sum)      → 103

sum  → (lambda ((x 0)) (inc 103 x))


In this example, what is the "place, and why"?
#4
I wonder where or how I can get the value which is assign by setq to a symbol like this:
(setq 'f 100)
'f ;;-> f
''f ;;->'f

How can I get the value 100?
#5
(setq a "time")

(setq b '(set xlabel a))



My question is:

how to design a function to transfer a list("b" in this example) into a string. That is to say, replace the parentheses of b with quotation marks to get "set xlabel "time"".
#6
First example:
(setq var "b")
(apply append '("a" var)) ;; I believe the result will be "ab", but what I get is error message.


Second example:
(setq var "b")
(append "a" var);; -> "ab"


I think the two examples are equivalent, but it seems not. Why? And why the first example is a wrong way to use "append" and how to make "apply append" work?
#7
A key value(integer) can be obtained by the built-in function "char".

For example,
(char "a")          → 97
My question is: How to  obtain the value of combinated keys like this:

(myfunc "ctrl a") which means "CTRL + a" . And also  "CTRL+Alt+a" , "CTRL+SHIFT+a" are wanted.

Thank you.
#8
(setq a '(1 2 3)) ;; This list can not be evaled
(setq b '(1 a)) ;; This list can be evaled, (eval b) --> (2 3)
(setq c '(+ 1 2)) ;; This list can be evaled, (eval c) --> 3


My quesstion is:

Is there an universal method to determine whether a list can be evaluated or not?



In the following example:
(setq a '(1 2 3))
(setq d '((1 a) (3 4)))
(define (f lst) (+ (last lst) 1))
(f (d 0)) ;;->ERR: value expected : a
          ;;  called from user function (f (d 0))
(f a) ;;-> 4


The error in (f (d 0)) can be corrected by (f (eval (d 0))).

But "eval" can not used like this: (f (eval a)).

As the structure of the argument "lst" can not be forseen, is there a way to determine whether a list can be evaluated or not?
#9
I think the following two examples of using "let" say the same thing but they get different result:

Example #1:
(setq l '((a 1) (b 2)))
(let l (+ a b)) ;; ERR: invalid let parameter list in function let : l

Example #2:
(let ((a 1) (b 2)) (+ a b)) ;; get 3 as expected

Why is the first example a wrong way using "let"? And is there a way to give initializer expressions of "let" with a predefined list as shown in the first example?
#10
I construct a hash-table like this:
(setq mytable '(
     ((char "a") "a: choiceA" (+ 0 101))
     ((char "b") "b: choiceB" (+ 0 102))
     ))

In this example, I want the first element of each sublist to be evaluated while other elements not. That is to say, (char "a") should be 97, (char "b") should be 98.

Or, in common, how to  partially evaluate elements in a hash-table? Is there an easy to achieve this?
#11
I construct the following function by the primitive "case"
(define (f obj a da b db)
  (case obj
(a da)
(b db)
(true obj)
))
(f 1 1 "a" 2 "b") ;;=> I get 1, but what I want is "a".


What is the cause? And how to get what I want in the above code by the use of "case"?
#12
QuoteSource code and the newLISP executable can be linked together to build a self-contained application by using the -x command line flag.

My script file(named "0.lsp" in Windows sys) contains the following code at the begining:
(load "1.lsp")
(load "2.lsp")

So in this condition how to use "newlisp -x" to build my application which can be used independently in any directory?

I mean, linking all files(0.lsp, 1.lsp, 2.lsp) into a .exe file. Thus, this only one .exe file can work anywhere without any other file to support it.
#13
I want to run the dos command in win7 console by newlisp "exec" to clear secreen like this:
(exec "cls")
but fails with a feedback ("f").

Why? And how to deal with this?



PS: Also (exec "color 0b") does not work.
#14
I think the following two examples say the same thing, but they give different results, why?



Example No. 1:
(define-macro (m lst)
  (eval lst))
(m '(+ 4 5)) ;; -> (+ 4 5)


Example No. 2:
(eval '(+ 4 5)) ;; -> 9
#15
I don't quite understand how the function slice works with negative "int-length".

The following example comes from the manual:
(slice '(a b c d e f) 2 -2)  → (c d)
As explained in manual:
QuoteIf int-length is negative, slice will take the parameter as offset counting from the end and copy up to that offset

As my view, -2 seems to point to "e"(-1 points to "f"), so why is not the result  (c d e)?



And how to design a function like this:
(myslice '(0 1 2 3 4 5) 2 -2) ->(1 2)
(myslice '(0 1 2 3 4 5) 4 -3) ->(2 3 4)
(myslice '(0 1 2 3 4 5) 4 -1) ->(4)
(myslice "newLISP" 4 -2) ->"LI"

That means the negative symbol will cause myslice select elements in opposite direction.
#16
I have a table like this:
(setq lst '(("header1" "header2" "header3") ( "11" "12" "13") ("21" "22" "23") ("31" "32" "33") ("41" "42" "43" )))

I want to get some continueous lines e.g. from the first line to the third line by this:
(filter (and (>= $idx 1) (<= $idx 3)) lst)  ;; '(( "11" "12" "13") ("21" "22" "23") ("31" "32" "33") ) wanted.

But I find the function "filter" does not support "$idx".

So, is there a better way to achieve this?
#17
newLISP in the real world / non-ascii characters in path?
December 20, 2018, 07:08:22 PM
I have the following files in a directory:

- newlisp.exe   (newlisp v10.7.4(Win64-UTF8))

- main.lsp

- 01-寻找当前目录下是否有所需文件.lsp



In the main.lsp, the first line of code is:
(load "01-寻找当前目录下是否有所需文件.lsp")

When I run "newlisp main.lsp" in this directory of console, error message given:
QuoteERR: problem accessing file in function load : "01-寻找当前目录下是否有所需文件.lsp"


My OS is win7(64), and I guess non-ascii characters in path cause this problem. But why is the newlisp v10.7.4(Win64-UTF8) not able to recognize these non-ascii charactors? And how to solve this problem?
#18
The function "select" can be used to select multi-elements of a list at one time, for example:
(select '(0 1 2 3 4) '(1 2)) ->(1 2)



Yet what I want is to get all elements other than those "selceted", that is to say, I want to obtain (0 3 4) from above example.

I tried with the function "filter" like this:
(filter (not (find $idx '(1 2))) '(0 1 2 3 4))
but it fails because "filter" does not support $idx.



So, is there a better way to anti-select elements in a list at one time by another list which assign positions those elements to be deleted?
#19
To understand the meaning of function arguments, I give there arguments long names. However, it's so boring to use these long names in function body.

I ttied like this:


(define (f converse-rate-list (define rate converse-rate) )
   body in which "rate" instead of converse-rate-list is use....)


But it fails.

Is there a better way to achievable this goal? That is to say, how to bind a symbol to a argument of function?
#20
newLISP in the real world / string evaculat in (if ...)
December 06, 2018, 05:04:10 PM
Here is my code:


(setq file-name-extension ".csv")
(if file-name-extension file-name-extension "")


I think the "if" expression should return a string ".csv", but I get nil.

Why is file-name-extension in the true part of the "if" expression not evaculated?