Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write this Lisp / Scheme code?

Tags:

lisp

scheme

A lambda expression which takes a function (of one argument) and a number, and applies the function to twice the number.

like image 828
dave Avatar asked Oct 23 '08 02:10

dave


People also ask

What is Lisp syntax?

The syntactic elements of the Lisp programming language are symbolic expressions, also known as s-expressions. Both programs and data are represented as s-expressions: an s-expression may be either an atom or a list. Lisp atoms are the basic syntactic units of the language and include both numbers and symbols.

Is Scheme a Lisp?

Scheme is a dialect of Lisp that stresses conceptual elegance and simplicity. It is specified in R4RS and IEEE standard P1178. (See the Scheme FAQ for details on standards for Scheme.) Scheme is much smaller than Common Lisp; the specification is about 50 pages, compared to Common Lisp's 1300 page draft standard.

How do you define a variable in a Scheme?

A variable is a box-like object that can hold any Scheme value. It is said to be undefined if its box holds a special Scheme value that denotes undefined-ness (which is different from all other Scheme values, including for example #f ); otherwise the variable is defined.

What language is Scheme?

Scheme is a multi-paradigm programming language. It is a dialect of Lisp which supports functional and procedural programming. It was developed by Guy L. Steele and Gerald Jay Sussman in the 1970s.


2 Answers

Applying the function to twice the number:

(lambda (f x) (f (* 2 x)))

Applying the function to the number twice (which is what you may have intended to ask):

(lambda (f x) (f (f x)))
like image 84
Greg Hewgill Avatar answered Sep 19 '22 04:09

Greg Hewgill


Greg's answer is correct, but you might think about how you might break apart this problem to find the answer yourself. Here is one approach:

; A lambda expression
;(lambda () )

; which takes a function (of one argument) and a number
;(lambda (fun num) )

; and applies the function
;(lambda (fun num) (fun num))

; to twice the number
;(lambda (fun num) (fun (* 2 num)))

((lambda (fun num) (fun (* 2 num))) + 12)
like image 45
grettke Avatar answered Sep 19 '22 04:09

grettke