# Lisp提取任务

19

``````(list 1 2 3)
``````

``````(1 2 3)
``````

``````(car (1 2 3))
==> 1
``````

``````(cdr (1 2 3))
==> (2 3)
``````

``````(cdr (1 2))
==> (2)
(car (cdr (1 2)))
==> 2
``````

``````(cdr (1 2 3 (4 5 6)))
==> (2 3 (4 5 6))
``````

@MartinBüttner好的。

`(1 2 3) 16`我们该回来`()`吗？
coredump

@coredump好问题。您可以假设目标整数将始终位于表达式中，因此`(1 2 3) 16`永远不会出现这样的情况。

Blackhole 2015年

1

# 果酱（59）

``````q"()""[]"er~{:AL>{0jA1<e_-_A(?j'l/"(car l)"@{2'dt}&*}"l"?}j
``````

``````q                 read the input
"()""[]"er        replace parentheses with square brackets
~                 evaluate the string, pushing an array and a number
{…}j              calculate with memoized recursion using the array as the argument
and the number as the memozied value for argument 0
:A              store the argument in A
L>              practically, check if A is an array
if A is a (non-empty) array, compare with an empty array
(result 1, true)
if A is a number, slice the empty array from that position
(result [], false)
{…}           if A is an array
0j          get the memoized value for 0 (the number to search)
A1<         slice A keeping only its first element
e_          flatten array
-           set difference - true iff the number was not in the array
_           duplicate the result (this is the car/cdr indicator)
A(          uncons A from left, resulting in the "cdr" followed by the "car"
?           choose the cdr if the number was not in the flattened first item,
else choose the car
j           call the block recursively with the chosen value as the argument
'l/         split the result around the 'l' character
"(car l)"   push this string
@           bring up the car/cdr indicator
{…}&        if true (indicating cdr)
2'dt      set the character in position 2 to 'd'
*           join the split pieces using the resulting string as a separator
"l"           else (if A is not an array) just push "l"
(we know that when we get to a number, it is the right number)
?             end if
``````

10

# 普通Lisp，99

``(defun g(l n &optional(o'l))(if(eql n l)o(and(consp l)(or(g(car l)n`(car,o))(g(cdr l)n`(cdr,o))))))``

``(lambda(L x &aux(p'l))(labels((f(S &aux e)(cons(or(position x S)(position-if(lambda(y)(if(consp y)(setf e(f y))))S)(return-from f()))e)))(dolist(o(print(f L))p)(dotimes(i o)(setf p`(cdr,p)))(setf p`(car,p)))))``

### 展开式

``````(lambda
(l x &aux (p 'l))
(labels ((f (s &aux e)
(cons
(or (position x s)
(position-if
(lambda (y)
(if (consp y)
(setf e (f y))))
s)
(return-from f nil))
e)))
(dolist (o (print (f l)) p)
(dotimes (i o) (setf p `(cdr ,p)))
(setf p `(car ,p)))))``````

### 例

``(funcall *fun* '(4 5 (1 2 (7) 9 (10 8 14))) 14)``

``(CAR (CDR (CDR (CAR (CDR (CDR (CDR (CDR (CAR (CDR (CDR L)))))))))))``

``(LAMBDA (#:G854) (CAR (CDR (CDR (CAR (CDR (CDR (CDR (CDR (CAR (CDR (CDR #:G854))))))))))))``

[1] `(caddar (cddddr (caddr l)))`也会很好

2

DanTheMan

4
@DanTheMan Lisp-ception几乎定义了Lisp ;-)
coredump

9

# 视网膜，170 142 125 115 114 87 84 83 75 73 70 69 68 67字节

``````\b(.+)\b.* \1\$
(
^.
l
\(
a
+`a *\)|\d

d
+`(.*[l)])(\w)
(c\$2r \$1)
``````

5

# Pyth，62个字节

``````JvXz"() ,][")u?qJQG&=J?K}Quu+GHNY<J1)hJtJ++XWK"(cdr "\d\aG\)\l
``````

5

## 方案（R5RS），102字节

``````(let g((l(read))(n(read))(o'l))(if(pair? l)(or(g(car l)n`(car,o))(g(cdr l)n`(cdr,o)))(and(eq? n l)o)))
``````

1

## PHP-177字节

``````function f(\$a,\$o,\$n){foreach(\$a as\$v){if(\$n===\$v||\$s=f(\$v,\$o,\$n))return
'(car '.(\$s?:\$o).')';\$o="(cdr \$o)";}}function l(\$s,\$n){echo f(eval(strtr
("return\$s;",'() ','[],')),l,\$n);}``````

``````function extractPhp(\$list, \$output, \$number)
{
foreach (\$list as \$value)
{
if (is_int(\$value))
{
if (\$value === \$number) {
return '(car '. \$output .')';
}
}
else
{
\$subOutput = extractPhp(\$value, \$output, \$number);
if (\$subOutput !== null) {
return '(car '. \$subOutput .')';
}
}

\$output = '(cdr '. \$output .')';
}
}

function extractLisp(\$stringList, \$number)
{
\$phpCode = 'return '. strtr(\$stringList, '() ','[],') .';';
\$list = eval(\$phpCode);
echo extractPhp(\$list, 'l', \$number);
}``````

1

`l "(4 5 (1 2 (7) 9 (10 8 14)))" 8`

`"(car (cdr (car (cdr (cdr (cdr (cdr (car (cdr (cdr l))))))))))"`

``````l(h:s)n=c\$i(show n)s""""
i n(h:s)t l|h>'/'&&h<':'=i n s(t++[h])l|t==n='a':l|h=='('=j\$'a':l|h==')'=j\$tail\$dropWhile(=='d')l|0<1=j\$'d':l where j=i n s""
c[]="l"
c(h:s)="(c"++h:"r "++c s++")"
``````

1

NIMI

Leif Willerts 2015年

0

# 普通Lisp 168字节

``````(lambda(l e)(labels((r(l o)(setf a(car l)d(cdr l)x`(car,o)y`(cdr,o))(if(equal e a)x(if(atom a)(r d y)(if(find e l)(r d y)(if d(r d y)(r a x)))))))(r l'l)))
``````

``````(lambda (l e)
(labels ((r (l o)
(setf a (car l) d (cdr l)
x `(car ,o) y `(cdr ,o))
(if (equal e a) x
(if (atom a)
(r d y)
(if (find e l)
(r d y)
(if d
(r d y)
(r a x)))))))
(r l 'l)))
``````
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.