I am learning Lisp. I have implemented a Common Lisp function that merges two strings that are ordered alphabetically, using recursion. Here is my code, but there is something wrong with it and I didn't figure it out.
(defun merge (F L)
(if (null F)
(if (null L)
F ; return f
( L )) ; else return L
;else if
(if (null L)
F) ; return F
;else if
(if (string< (substring F 0 1) (substring L 0 1)
(concat 'string (substring F 0 1)
(merge (substring F 1 (length F)) L)))
(
(concat 'string (substring L 0 1)
(merge F (substring L 1 (length L)) ))
))))
Edit :
I simply want to merge two strings such as the
inputs are string a = adf
and string b = beg
and the result or output should be abdefg
.
Thanks in advance.
(L)
will callL
with 0 arguments. Remove all your extraneous parentheses as a first step. Also, there's no need to have a space after opening parenthesis. It makes your code very hard to read. If you want to returnL
, call(return L)
, not justL
, if not in terminal position.null
is meaningful to call on lists, to check whether they are empty or not, not on strings.(null "")
returnsNIL
.(length "")
returns 0. The full name of the function you're using isconcatenate
, notconcat
. But before all, please format your code!