How to parse a string as a list structure? - lisp

I have a string, which holds a a list structure like ((p X) (q (f X))) and I would really like to find a function that interprets/converts this string as a list of lists just like if it was a '((p X) (q (f X))).
(list "((p X) (q (f X)))") just makes it a single element list.
(intern "((p X) (q (f X)))") encloses it in | symbols.

How does
(read-from-string "((p X) (q (f X)))")
work for you? Documentation found here.

"((p X) (q (f X)))" is a string in Lisp. Strings are enclosed in ".
LIST creates a list with its arguments as elements.
So (list "((p X) (q (f X)))") creates a list with the string as the element.
INTERN creates a symbol.
(intern "((p X) (q (f X)))") creates a symbol with the string argument as its name. In Common Lisp symbols can have arbitrary names, even including characters like ( and ). Such symbols are printed enclosed in |. Example: |((p X) (q (f X)))| is a symbol with such a strange name.
Parsing an s-expression is called reading in Lisp. The functions to do so are for example READ and READ-FROM-STRING.
There are two basic ways to read the s-expression:
CL-USER 1 > (read-from-string "((p X) (q (f X)))")
((P X) (Q (F X)))
17
But you can also open an input stream based on the string using WITH-INPUT-FROM-STRINGand then use the usual READ:
CL-USER 2 > (with-input-from-string (stream "((p X) (q (f X)))")
(read stream))
((P X) (Q (F X)))

Related

LISP function fails and returns NIL when making a list of Nth items of other lists

I need to make a named function, which makes a new list from the 3rd item from three master lists. I made this code
(defun func (nth n))
(lambda (l1 l2 l3) '(H G (U J) (T R)) '(2 1 (+ 4 5)) '(TYPE CHAR REAL (H G)))
(write (func (lambda (l1 l2 l3) '(H G (U J) (T R)) '(2 1 (+ 4 5)) '(TYPE CHAR REAL (H G))) 2))
but it returns NIL. What do I do wrong?
You need to indent and format your code. Otherwise it's an unreadable blob of characters.
Here is what you have:
Your function func takes two arguments and does nothing. It always returns NIL.
Then there is a lambda expression which takes three arguments. It uses none of them. It has three body expressions. The first two are not used and the third is being returned.
The third expression calls the function func, which always returns NIL. WRITE then prints this NIL.
Your code, but formatted readably for humans, looks like this:
(defun func (nth n)
; no functionality, does nothing and returns NIL
)
(lambda (l1 l2 l3)
'(H G (U J) (T R))
'(2 1 (+ 4 5))
'(TYPE CHAR REAL (H G)))
(write (func (lambda (l1 l2 l3)
'(H G (U J) (T R))
'(2 1 (+ 4 5))
'(TYPE CHAR REAL (H G)))
2))
Hint: you would need to write actual functionality.

Closure inside a Common Lisp Macro

In the following code how can i have the x and y variables to reflect the expressions given at macro call time?
(defmacro defrule (init-form &rest replication-patterns)
(let (rule-table)
`(destructuring-bind (p x y) ',init-form
#'(lambda (w h) (list x y)))))
When expanding a call like:
(defrule (70 (* 1/2 w) (+ h 3)))
it returns:
(DESTRUCTURING-BIND (P X Y) '(70 (* 1/2 W) (+ H 3))
#'(LAMBDA (W H) (LIST X Y)))
where the original expressions with W and H references are lost. I tried back-quoting the lambda function creation:
(defmacro defrule (init-form &rest replication-patterns)
(let (rule-table)
`(destructuring-bind (p x y) ',init-form
`#'(lambda (w h) (list ,x ,y)))))
But a same call:
(defrule (70 (* 1/2 w) (+ h 3)))
expands to:
(DESTRUCTURING-BIND
(P X Y)
'(70 (* 1/2 W) (+ H 3))
`#'(LAMBDA (W H) (LIST ,X ,Y)))
which returns a CONS:
#'(LAMBDA (W H) (LIST (* 1/2 W) (+ H 3)))
which can not be used by funcall and passed around like a function object easily. How can i return a function object with expressions i pass in as arguments for the x y part of the init-form with possible W H references being visible by the closure function?
You're getting a cons because you have the backquotes nested.
You don't need backquote around destructuring-bind, because you're destructuring at macro expansion time, and you can do the destructuring directly in the macro lambda list.
(defmacro defrule ((p x y) &rest replication-patterns)
(let (rule-table)
`#'(lambda (w h) (list ,x ,y))))
Looking at your code:
(defmacro defrule (init-form &rest replication-patterns)
(let (rule-table)
`(destructuring-bind (p x y) ',init-form
#'(lambda (w h) (list x y)))))
You want a macro, which expands into code, which then at runtime takes code and returns a closure?
That's probably not a good idea.
Keep in mind: it's the macro, which should manipulate code at macro-expansion time. At runtime, the code should be fixed. See Barmar's explanation how to improve your code.

Implement every, some in Lisp [duplicate]

I want a predicate as a parameter of a function.
(DEFUN per (F L)
(cond ((F L) 'working)
(T 'anything)))
(per 'numberp 3)
as result it raises an error:
Undefined operator F in form (F L).
As explained in Technical Issues of Separation in Function Cells and Value Cells,
Common Lisp is a Lisp-2, i.e., you
need funcall:
(defun per (F L)
(if (funcall F L)
'working
'other))
(per #'numberp 3)
==> WORKING
(per #'numberp "3")
==> OTHER
See also apply.
Late to the party, but here's another example:
(defun strip-predicate (p list)
(cond ((endp list) nil)
((funcall p (first list)) (strip-predicate (rest list)))
( T (cons (first list) (strip-Predicate p (rest list))))))
This could be used on predicates such as atom or numberp:
(strip-predicate 'numberp '(a 1 b 2 c 3 d))
(a b c d)
or:
(strip-predicate 'atom '(a (a b) b c d))
((a b))

Function composition in Scheme

I'm trying to modify the function below to compose two functions in Scheme.
(define (compose F1 F2)
(eval F1 (interaction-environment))
)
rather than
(define (compose f g)
(λ (x) (f (g x))))
But I'm not sure about how to use eval.
From your suggestion, I guess you want to use Scheme's macros / preprocessing capabilities. eval isn't meant for code transformation. Composition ∘ can be defined in Scheme as
(define (∘ f g)
(lambda (x) (f (g x))) )
or
(define-syntax ∘
(syntax-rules ()
((∘ f g)
(lambda (x) (f (g x))) )))
where the arity of expressions f and g is 1.
(define (plus-10 n) (+ n 10))
(define (minus-3 n) (- n 3))
(display
(map (∘ plus-10 minus-3)
(list 1 2 3 4) ))
The map expression at compile-time becomes
(map (lambda (x) (plus-10 (minus-3 x)))
(list 1 2 3 4) )
equal?s
(list 8 9 10 11)

How would I express this Scheme function more clearly?

(define (repeated f n)
if (= n 0)
f
((compose repeated f) (lambda (x) (- n 1))))
I wrote this function, but how would I express this more clearly, using simple recursion with repeated?
I'm sorry, I forgot to define my compose function.
(define (compose f g) (lambda (x) (f (g x))))
And the function takes as inputs a procedure that computes f and a positive integer n and returns the procedure that computes the nth repeated application of f.
I'm assuming that (repeated f 3) should return a function g(x)=f(f(f(x))). If that's not what you want, please clarify. Anyways, that definition of repeated can be written as follows:
(define (repeated f n)
(lambda (x)
(if (= n 0)
x
((repeated f (- n 1)) (f x)))))
(define (square x)
(* x x))
(define y (repeated square 3))
(y 2) ; returns 256, which is (square (square (square 2)))
(define (repeated f n)
(lambda (x)
(let recur ((x x) (n n))
(if (= n 0)
args
(recur (f x) (sub1 n))))))
Write the function the way you normally would, except that the arguments are passed in two stages. It might be even clearer to define repeated this way:
(define repeated (lambda (f n) (lambda (x)
(define (recur x n)
(if (= n 0)
x
(recur (f x) (sub1 n))))
(recur x n))))
You don't have to use a 'let-loop' this way, and the lambdas make it obvious that you expect your arguments in two stages.
(Note:recur is not built in to Scheme as it is in Clojure, I just like the name)
> (define foonly (repeat sub1 10))
> (foonly 11)
1
> (foonly 9)
-1
The cool functional feature you want here is currying, not composition. Here's the Haskell with implicit currying:
repeated _ 0 x = x
repeated f n x = repeated f (pred n) (f x)
I hope this isn't a homework problem.
What is your function trying to do, just out of curiosity? Is it to run f, n times? If so, you can do this.
(define (repeated f n)
(for-each (lambda (i) (f)) (iota n)))