LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2012, 04:21 AM   #1
muggabug
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Rep: Reputation: Disabled
Scheme -> Lisp. How can I pass a function as parameter to another function ?


Hi
I am trying to find how to do the following in Lisp:

( (lambda (f) ( f '(b c ))) (lambda (x) (cons 'a x)))

(answer ;Value 2: (a b c) )

This (directly from Graham's Roots of Lisp) works in Scheme.
According to Graham, in Lisp I should do:

( (lambda (f) ( f '(b c ))) '(lambda (x) (cons 'a x)))

But there is no way I can get this to work in Common Lisp (emacs lisp+cl, or CMU Common Lisp). Using 'quote' or 'function ' does not help at all.
emacs gives me

*** Eval error *** Symbol's function definition is void: f

CMU gives me

; In: (LAMBDA (F) (F '#)) '(LAMBDA (X) (CONS 'A X))
; ((LAMBDA # #) '(LAMBDA # #))
; Note: Variable F defined but never used.
; ;
; Warning: This function is undefined:
; F
Error in KERNEL:%COERCE-TO-FUNCTION: the function F is undefined.
[Condition of type UNDEFINED-FUNCTION]
Splitting the two by defining (defun fo (x) (cons 'a x)) first, does not help either.


I am clueless, what is the way to do such a simple thing ?

Cheers
 
Old 12-26-2012, 08:38 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Short answer is do this:
Code:
((lambda (f) (funcall f '(b c ))) (lambda (x) (cons 'a x)))
Common Lisp is a "Lisp-2" which means the namespace for functions is separate from the value namespace. For (a lot) more detail: Technical Issues of Separation in Function Cells and Value Cells, Kent Pitman
 
1 members found this post helpful.
Old 12-26-2012, 10:23 AM   #3
muggabug
LQ Newbie
 
Registered: Aug 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Red face solved

Thanks,


Very short, indeed. I remember to have tried apply and funcall, but clearly, that has been in the wrong place .
All of Graham's article stuff works now.
 
  


Reply

Tags
lisp



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can i pass an entire Array into a function parameter by value? (no pointer pass) esgol Programming 26 07-31-2012 11:34 AM
Function parameters in Lisp. zaxonus Programming 2 11-05-2011 08:27 AM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM
Successor function in LISP AceKnocks Programming 12 04-08-2008 12:00 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration