(defun atom_nth (l a / i tmp)
(setq i 0)
(while l
(if (equal a (car l) 1e-14)
(setq tmp (cons i tmp)))
(setq l (cdr l) i (1+ i)))
(reverse tmp))
(atom_nth '(1 2 3 4 5) 1)
(0)
(atom_nth '(1 2 3 4 5 1) 1)
(0 5)
Are there more elegant code?;; CAB 10/18/2005
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (itm lst / idn nlst)
(setq idx -1)
(while (< (setq idx (1+ idx)) (length lst))
(and (= itm (nth idx lst))
(setq nlst (cons idx nlst))))
(reverse nlst)
)
;; (print (GetPos 3 '(1 2 3 1 2 3 1 3 5)))
;; returns (2 5 7)
;; CAB 12/17/2009
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (pat lst / i plst)
(setq i 0)
(mapcar
(function
(lambda (x)
(and (wcmatch x pat) (setq plst (cons i plst)))
(setq i (1+ i))))
lst)
(reverse plst))
(defun c:test()
(princ (getpos "*25" '("0425" "123" "345" "625")))
(princ)
)
Some oldiesGot it!Thankkk you Alan!Code: [Select];; CAB 10/18/2005
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (itm lst / idn nlst)
(setq idx -1)
(while (< (setq idx (1+ idx)) (length lst))
(and (= itm (nth idx lst))
(setq nlst (cons idx nlst))))
(reverse nlst)
)
;; (print (GetPos 3 '(1 2 3 1 2 3 1 3 5)))
;; returns (2 5 7)
;; CAB 12/17/2009
;; returns a list of pointers (base 0) of the
;; item found in the list
(defun getpos (pat lst / i plst)
(setq i 0)
(mapcar
(function
(lambda (x)
(and (wcmatch x pat) (setq plst (cons i plst)))
(setq i (1+ i))))
lst)
(reverse plst))
(defun c:test()
(princ (getpos "*25" '("0425" "123" "345" "625")))
(princ)
)
(defun nth_ps (lst itm / i llst)
(setq i -1)
(mapcar '(lambda (x) (setq i (1+ i))(if (= itm(nth i lst))(setq llst(cons i llst)))) lst)
(reverse llst)
)
Regards
Hi,Thanks Gile!
Going a little further with functional programming features.
Rather than checking only the equality (using = or equals), the positions function requires a prédicate function as argument.
NOTA: the loop tail recursive function can easily be replaced with an imperative while statement.
_$ (positions 2 '(1 2 3 4 5 1) '<=)
(0 1 5)
_$ (positions 2 '(1 2 3 4 5 1) <=)
(0 1 5)
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_Position - 11/03/2006
;
; Version 1.00
;
; Description:
; returns the position of the first TstItm found
; in a list (nth based) - Lisp version
; > in Visual Lisp use: vl-position
;
; Arguments:
; TstItm = Any AutoLISP symbol
; In_Lst = A true list
;
; Return Values:
; Integer or nil if TstItm is not member of the list
;
(defun ALE_Position (TstItm In_Lst / TmpLst)
(if (setq TmpLst (member TstItm In_Lst))
(- (length In_Lst) (length TmpLst))
)
)
; Function: ALE_PositionS - 01/09/2005
;
; Version 1.00
;
; Description:
; returns the positions of a item in a list (nth based)
;
; Arguments:
; TstItm = Any AutoLISP symbol
; In_Lst = A true list
;
; Return Values:
; List or nil if TstItm is not member of the list
;
; Examples:
; (setq alist '(0 1 2 3 4 3 5 3 6 3 3 7))
;
; (ALE_PositionS 3 alist)
; Returns: (3 5 7 9 10)
;
;
(defun ALE_PositionS (TstItm In_Lst / LstLen OutLst)
(setq LstLen (1- (length In_Lst)))
(while (vl-position TstItm In_Lst)
(setq
In_Lst (cdr (member TstItm In_Lst))
OutLst (cons (- LstLen (length In_Lst)) OutLst)
)
)
(reverse OutLst)
)
; slower
(defun ALE_PositionS2 (TstItm In_Lst / LstLen OutLst)
(setq LstLen (1- (length In_Lst)))
(while (setq In_Lst (member TstItm In_Lst))
(setq
In_Lst (cdr In_Lst)
OutLst (cons (- LstLen (length In_Lst)) OutLst)
)
)
(reverse OutLst)
)
; recursive
(defun ALE_PositionS_Rec (TstItm In_Lst / LstLng)
(setq LstLng (1- (length In_Lst)))
(ALE_PositionS_Rec_Aux
TstItm (cdr (member TstItm In_Lst)) NthPos LstLng
)
)
(defun ALE_PositionS_Rec_Aux (TstItm In_Lst NthPos LstLng)
(if (vl-position TstItm In_Lst)
(setq
NthPos
(ALE_PositionS_Rec_Aux
TstItm (cdr (member TstItm In_Lst)) NthPos LstLng
)
NthPos (cons (- LstLng (length In_Lst)) NthPos)
)
(setq NthPos (cons (- LstLng (length In_Lst)) NthPos))
)
)
Command: (ALE_PositionS 1 '(1 2 3 4 5 1))
(0 5)
Benchmarking ..... done for 32768 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(ALE_POSITIONS2 3 ALIST) 32768 1404 1404 1.47
(ALE_POSITIONS 3 ALIST) 32768 1482 1482 1.39
(ALE_POSITIONS_REC 3 ALIST) 32768 1529 1529 1.35
(IB:POSITIONS= 3 ALIST) 32768 1701 1701 1.21
(GILE:POSITIONS= 3 ALIST) 16384 1029 2058 1.00
--------------------------------------------------------------------------------
_$ (setq alist (apply 'append (list alist alist alist alist alist alist)))
(0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7)
_$ (QuickBench '((IB:Positions= 3 alist) (ALE_PositionS 3 alist) (ALE_PositionS2 3 alist) (ALE_PositionS_Rec 3 alist) (Gile:positions= 3 alist)))
Benchmarking ..... done for 16384 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(ALE_POSITIONS2 3 ALIST) 16384 1762 1762 1.68
(ALE_POSITIONS 3 ALIST) 16384 1934 1934 1.53
(IB:POSITIONS= 3 ALIST) 8192 1031 2062 1.44
(ALE_POSITIONS_REC 3 ALIST) 8192 1046 2092 1.42
(GILE:POSITIONS= 3 ALIST) 8192 1481 2962 1.00
--------------------------------------------------------------------------------
_$ (setq alist (apply 'append (list alist alist alist alist alist alist)))
(0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7 0 1 2 3 4 3 5 3 6 3 3 7)
_$ (QuickBench '((IB:Positions= 3 alist) (ALE_PositionS 3 alist) (ALE_PositionS2 3 alist) (ALE_PositionS_Rec 3 alist) (Gile:positions= 3 alist)))
Benchmarking ..... done for 2048 iterations. Sorted from fastest.
Statement Increment Time(ms) Normalize Relative
--------------------------------------------------------------------------------
(IB:POSITIONS= 3 ALIST) 2048 1154 1154 1.68
(ALE_POSITIONS2 3 ALIST) 2048 1311 1311 1.48
(ALE_POSITIONS 3 ALIST) 2048 1451 1451 1.33
(ALE_POSITIONS_REC 3 ALIST) 2048 1530 1530 1.26
(GILE:POSITIONS= 3 ALIST) 2048 1935 1935 1.00
--------------------------------------------------------------------------------
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
(setq alist nil) (setq alist2 '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
(setq alist (repeat 500 (setq alist (append alist alist2))))
Elapsed milliseconds / relative speed for 8192 iteration(s):
(ALE_POSITIONS "ATOI" ALIST)........2044 / 36.15 <fastest>
(IB:POSITIONS "ATOI" ALIST =)......51668 / 1.43
(GILE:POSITIONS= "ATOI" ALIST).....73898 / 1 <slowest>
;----------------------------------------------------------------
(setq alist (atoms-family 1))
(setq alist (append alist alist alist alist))
Elapsed milliseconds / relative speed for 256 iteration(s):
(ALE_POSITIONS "ATOI" ALIST).......1779 / 4.31 <fastest>
(IB:POSITIONS "ATOI" ALIST =)......7223 / 1.06
(GILE:POSITIONS= "ATOI" ALIST).....7659 / 1 <slowest>
F# shares a core language with the programming language OCaml, and in some ways it can be considered an "OCaml for .NET". F# would not exist without OCaml, which in turn comes from the ML family of programming languages which dates back to 1974, F# also draws from haskell particulary with regard to two advanced features called sequence expressions and workflows. [...]
Despite the similariries to OCaml and Haskell, programming with F# is really quite different, in particular, the F# approach to type inference, OO programming, and dynamic language techniques is substantially different from all other mainstream functional languages.
F# originated as a variant of ML and has been influenced by OCaml, C#, Python, Haskell, Scala and Erlang.