Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ACL - secntion 4.4 example

126 views
Skip to first unread message

arnuld

unread,
Feb 6, 2012, 1:41:54 AM2/6/12
to
Here is what ACL has created on function called "second-word" which
returns 2nd word of a string. I have edited it to use variable p2 but
that does not work.

PROBLEM: Why p2 is undefined ?


(defun second-word (str)
(let ((p1 (+ 1 (position #\ str))))
(subseq str p1 (position #\ str :start p1))))


(defun second-word2 (str)
(let ((p1 (+ 1 (position #\ str)))
(p2 (position #\ str :start p1)))
(subseq str p1 p2)))

====================== OUTPUT =====================

[35]> (load "second-word.lisp")
;; Loading file second-word.lisp ...
;; Loaded file second-word.lisp
T
[36]> (defvar jkd "Bruce Lee")
JKD
[37]> (second-word jkd)
"Lee"
[38]> (second-word2 jkd)

*** - LET: variable P1 has no value
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of P1.
STORE-VALUE :R2 Input a new value for P1.
ABORT :R3 Abort main loop
Break 1 [39]> ABORT
[40]> (second-word "Pascal")

*** - +: NIL is not a number
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
Break 1 [41]>





--
arnuld
http://LispMachine.Wordpress.com

Pascal J. Bourguignon

unread,
Feb 6, 2012, 2:06:19 AM2/6/12
to
arnuld <sun...@invalid.address> writes:

> Here is what ACL has created on function called "second-word" which
> returns 2nd word of a string. I have edited it to use variable p2 but
> that does not work.
>
> PROBLEM: Why p2 is undefined ?
>
>
> (defun second-word (str)
> (let ((p1 (+ 1 (position #\ str))))
> (subseq str p1 (position #\ str :start p1))))
>
>
> (defun second-word2 (str)
> (let ((p1 (+ 1 (position #\ str)))
> (p2 (position #\ str :start p1)))
> (subseq str p1 p2)))
>
> ====================== OUTPUT =====================
>
> [35]> (load "second-word.lisp")
> ;; Loading file second-word.lisp ...
> ;; Loaded file second-word.lisp
> T
> [36]> (defvar jkd "Bruce Lee")
> JKD
> [37]> (second-word jkd)
> "Lee"
> [38]> (second-word2 jkd)
>
> *** - LET: variable P1 has no value

Read the reference!
http://www.lispworks.com/documentation/HyperSpec/Body/s_let_l.htm#let

"first evaluates the expressions init-form-1, init-form-2, and so on,
in that order, saving the resulting values. Then all of the
variables varj are bound to the corresponding values; each binding
is lexical unless there is a special declaration to the contrary. "

In English, it means that p1 is not visible in the initializing
expressions of p2.

You should write:

(defun second-word2 (str)
(let ((p1 (+ 1 (position #\ str))))
(let ((p2 (position #\ str :start p1)))
(subseq str p1 p2))))

instead.


Or if you're lazy Read the reference!, and use LET*.


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Kaz Kylheku

unread,
Feb 6, 2012, 2:49:26 AM2/6/12
to
On 2012-02-06, arnuld <sun...@invalid.address> wrote:
> Here is what ACL has created on function called "second-word" which
> returns 2nd word of a string. I have edited it to use variable p2 but
> that does not work.
>
> PROBLEM: Why p2 is undefined ?

> (defun second-word2 (str)
> (let ((p1 (+ 1 (position #\ str)))
> (p2 (position #\ str :start p1)))
> (subseq str p1 p2)))

Research the difference between let and let*

WJ

unread,
Feb 16, 2012, 9:36:00 PM2/16/12
to
MatzLisp:

second_word = lambda{|str| str.split[1]}

second_word[ "the fever called living" ]
==>"fever"

Marco Antoniotti

unread,
Feb 17, 2012, 11:13:30 AM2/17/12
to
Oh yeah! How did you know I needed a sleeping aid? Yawn.

Cheers
--
MA

WJ

unread,
Mar 6, 2012, 5:12:53 AM3/6/12
to
NewLisp:

(define (second-word str) ((parse str " ") 1))

WJ

unread,
Mar 24, 2012, 12:24:11 AM3/24/12
to
Clojure:

user=> (second (.split "the fever called living" " "))
"fever"

user=> (-> "the fever called living" (.split " ") second)
"fever"

WJ

unread,
Apr 15, 2013, 8:02:56 PM4/15/13
to
Instead of CL, tCL.

% lindex "the second word" 1
second


WJ

unread,
Jul 6, 2013, 3:10:44 AM7/6/13
to
Julia:

julia> split("the second word")[2]
"second"

WJ

unread,
Oct 28, 2013, 1:53:24 AM10/28/13
to
Arc:

arc> ((tokens "the second word") 1)
"second"

WJ

unread,
Jun 18, 2014, 9:00:11 PM6/18/14
to
elisp:

: (nth 1 (split-string "the second word"))
"second"

WJ

unread,
Nov 9, 2014, 2:50:01 PM11/9/14
to
Gauche Scheme:

gosh> (cadr (string-split "the second word" #/\s+/))
"second"

--
If Lisp is to live, then CL must die.

WJ

unread,
Nov 24, 2015, 7:39:04 PM11/24/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Ocaml:

#load "str.cma";;

open Str;;

List.nth (split (regexp " +") " foo bar what ") 1;;
===>
"bar"

--
[Jesse Jackson] would spit into the food of white patrons he hated and then
smilingly serve it to them. He did this, he said, "because it gave me
psychological gratification." -- Life Magazine, 1969-11-29
0 new messages