[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: my first lisp function... give me some comments plz..

William James

2/14/2016 4:02:00 AM

Barry Margolin wrote:

> >Two simple pieces of advice to make your program easier to read:
> >- follow the usual rules for indentation (very important)
> >- give meaningful names to variables and functions (if secondAtom can be
> >a list, its name is wrong)
> >
> >(defun prefix->infix (list)
> > (let ((second (second list))
> > (first (first list)))
> > (cond ((> (length list) 2)
> > (append (list (if (listp second)
> > (infix second)
> > second))
> > (list first)
> > (infix (append (list first)
> > (rest (rest list))))))
> > ((= (length list) 2)
> > (list (if (listp second)
> > (infix second)
> > second))))))
>
> You forgot to change the function name from "infix" to "prefix->infix" in
> the recursive calls.
>
> This can be further simplified by allowing the function to take atoms, as
> well as lists, so that the check for it being a list is done once, rather
> than with the redundant "if" statements.
>
> (defun prefix->infix (expression)
> (cond ((atom expression) expression)
> ((> (length expression) 2)
> (let ((operator (first expression))
> (first-arg (second expression))
> (rest-args (cddr expression)))
> `(,(prefix->infix first-arg)
> ,operator
> ,@(prefix->infix `(,operator ,@rest-args)))))
> ((= (length expression 2))

Error! (length expression)


> (list (prefix->infix (second expression))))
> (t (error "Invalid expression: %S" expression))))

Error! ~S

He probably didn't test it because he probably didn't even have
CL (COBOL-Like) installed. He probably didn't believe that the
language was really worth using.

MatzLisp (Ruby):

def intersperse item, array
array[0,1] + array.drop(1).flat_map{|x| [item,x]}
end

def prefix_to_infix expr
return expr unless expr.is_a? Array
case expr.size
when 0, 1
raise "Invalid expression: #{expr}"
when 2
[prefix_to_infix(expr[1])]
else
intersperse(expr[0], expr.drop(1).map{|x| prefix_to_infix(x)})
end
end


prefix_to_infix 2
==>2
prefix_to_infix [:+,2]
==>[2]
prefix_to_infix [:+,2,3,4]
==>[2, :+, 3, :+, 4]
prefix_to_infix [:+,2,[:*,3,4],[:/,64,4,2]]
==>[2, :+, [3, :*, 4], :+, [64, :/, 4, :/, 2]]


--
I do know that Jews are G-d's Chosen People (we say that in Kiddush every
Friday nite). Does that make us different from Gentiles? Clearly. If that's a
problem, deal with it.
https://web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx...