473,656 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help understanding Scheme's syntax, procedures and calls

I'm trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals.

What in heaven's name is this code fragment intending? (In English
prose if possible).

It looks like a fragment from a language called "scheme"

(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
(define (that n)
(f1 n1)

a) Describe the processing that occurs during the evaluation of the
expression (this 4)
b) Explain why the expression (=(this n)(that n) always evaluates to
true when n is a positive integer.
c) Write a fragment of code in the above language that adds up all the
integers within a given range, not including the two numbers
specified. For example, if the specified range was 4 à 9 then code
should add 5à 8.

Suggested answers:

a)

This 4 call started
As n-1=3 a recursive This 3 call is started
As n-1=2 a This 2 call starts
As n-1=1 a This 1 call starts
As n-1=0 a This 0 call is started and is returned as n=0
This 1 call is resolved by adding 1+0
This 2 call is resolved by adding 2+1
This 3 call is resolved by adding 3+3
Finally 10 is returned when This 4 call is resolved by adding 4 + 6.

I no more grasp the pattern of the suggested answer than the question,
and am much less in a position to explain it to anyone.

b)

Both the This and the That functions have the same output, and
furthermore both functions result in infinite recursion if n<0. When n
is a positive integer, the This function calculates
(n+…(3+(2+(1+(0 )))) and the that function calculates
(1+(2+(3+…(n=(0 )))). Both will always result in the same answer. The
list (=a b) only evaluates to true when a=b, as a does equal b the
list always evaluates to true for n>0.

Perhaps this answer will make more sense when I understand the code
fragment.

c)

Solution 1 (without existing functions)

(define (internal-range a b)
(if(>=(+ a 1)b)
0
(+(= a 1)(internal-range(+ a 1)b))))

Solution 2 using existing functions. And assuming a<b

(define (internal-range2 a b)
(-(this b) (this a)b))

Solution 3 using existing functions and dealing with a>b case
(define (internal-range3 a b)
(if (< a b)
(-(this b) (this a)b)
(-(this a) (this b)a)))

What is the role of the "0" character in solution 1 and the initial
fragment? What is the syntax rule being followed by the parentheses?

They note that the code was tested by "Dr Scheme" at
www.plt-scheme.org


Thanks
Jul 18 '05 #1
12 1361
Fran wrote:
I'm trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals.

What in heaven's name is this code fragment intending? (In English
prose if possible).

It looks like a fragment from a language called "scheme"

(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
(define (that n)
(f1 n1)

[silly disclaimer] I don't know Scheme but I'd suppose the above to be
equivalent to the following python (you seem to have some problems counting
the brackets :-)

def this(n):
if n == 0:
return 0
else:
return n + this(n-1)

def f1(a, b):
if b > a:
return 0
else:
return b + f1(a, b+1)

def that(n):
return f1(n, 1)

# try it out
print this(4)
print 'experimental "proof"'
ALL_POSITIVE_IN TEGERS = range(1, 10)
for i in ALL_POSITIVE_IN TEGERS:
print "%s:" % i, this(i), that(i)

Otherwise I'd suggest that you download Scheme and just try it out. Perhaps
they even have a debugger so you can step through the instructions one at a
time...

Peter

PS: More help would certainly lead to disqualificatio n.

Jul 18 '05 #2
fr********@mail .com (Fran) writes:
I'm trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals. What in heaven's name is this code fragment intending? (In English
prose if possible). It looks like a fragment from a language called "scheme" (define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
As given this is almost certainly wrong. The first problem is possibly just a
transcription error in that (=n 0) should probably be (= n 0). The second one
is the that the last line doesn't make sense. It looks like someone is
confused about how if statements work. Since this looks suspiciously like
homework I'm only giving a hint. If statements work like
(if expr1
(expr to return if exp1 is true)
(expr to return if exp1 is false))
Since each arm is an expression to evaluate it means you evaluate '=' as a
function in the last line hence it returns a boolean, which is going to cause
you grief after a short while.
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))


This is wrong syntactically (hint: the first expression for the if statement)

The questions wouldn't make sense until you fixed the functions.

There is a comp.lang.schem e incidentally.

Eddie
Jul 18 '05 #3
Unfortunately it looks more like 'broken scheme'.

Fran wrote:
(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
That looks fine, however:
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
Has 6 (s and 7 )s. I expect that the seconds line should read
(if (> b a)
(define (that n)
(f1 n1)


Again there is an imbalance in the ( and ), I think the second line should read
(f1 n 1)), note the space between then 'n' and the '1'.

Is this someone's homework by any chance?
Jul 18 '05 #4
On 2004-08-12, Peter Hickman <pe***@semantic o.com> wrote:
Is this someone's homework by any chance?


According to the OP, it's part of a final exam.

--
Grant Edwards grante Yow! Yow! Those people
at look exactly like Donnie
visi.com and Marie Osmond!!
Jul 18 '05 #5
Grant Edwards wrote:
On 2004-08-12, Peter Hickman <pe***@semantic o.com> wrote:

Is this someone's homework by any chance?

According to the OP, it's part of a final exam.


So they are completely shot then?
Jul 18 '05 #6
fr********@mail .com (Fran) wrote in message news:<95******* *************** ****@posting.go ogle.com>...
<Scheme question>

The right place where to ask this question is comp.lang.schem e.
They are pretty gentle with newbies, so don't worry ;)

Michele Simionato
Jul 18 '05 #7
ed***@holyrood. ed.ac.uk (Eddie Corns) wrote in message news:<cf******* ***@scotsman.ed .ac.uk>...
fr********@mail .com (Fran) writes:
I'm trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals.

What in heaven's name is this code fragment intending? (In English
prose if possible).

It looks like a fragment from a language called "scheme"

(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))


As given this is almost certainly wrong. The first problem is possibly just a
transcription error in that (=n 0) should probably be (= n 0). The second one
is the that the last line doesn't make sense. It looks like someone is
confused about how if statements work. Since this looks suspiciously like
homework I'm only giving a hint. If statements work like
(if expr1
(expr to return if exp1 is true)
(expr to return if exp1 is false))
Since each arm is an expression to evaluate it means you evaluate '=' as a
function in the last line hence it returns a boolean, which is going to cause
you grief after a short while.
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))


This is wrong syntactically (hint: the first expression for the if statement)

The questions wouldn't make sense until you fixed the functions.

There is a comp.lang.schem e incidentally.

Eddie


Thanks for the help. It's not homework but from an old exam paper, but
the girl's English isn't absolutely fluent and I'm looking for a
simple way to explain the expressions and functions.

FRAN
Jul 18 '05 #8
Peter Hickman <pe***@semantic o.com> wrote in message news:<41******* *************** *@news.easynet. co.uk>...
Unfortunately it looks more like 'broken scheme'.

Fran wrote:
(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))


That looks fine, however:
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))


Has 6 (s and 7 )s. I expect that the seconds line should read
(if (> b a)
(define (that n)
(f1 n1)


Again there is an imbalance in the ( and ), I think the second line should read
(f1 n 1)), note the space between then 'n' and the '1'.

Is this someone's homework by any chance?


Thanks for the help. It's not homework but from an old exam paper, but
the girl's English isn't absolutely fluent and I'm looking for a
simple way to explain the expressions and functions.

FRAN
Jul 18 '05 #9
Grant Edwards <gr****@visi.co m> wrote in message news:<41******* *************** *@newsreader.vi si.com>...
On 2004-08-12, Peter Hickman <pe***@semantic o.com> wrote:
Is this someone's homework by any chance?


According to the OP, it's part of a final exam.


It's not a final exam (that won't come until October-November) but
from an old exam paper. The girl's English isn't absolutely fluent and
I'm looking for a simple way to explain the expressions and functions.

FRAN
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33863
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
3
1912
by: slash | last post by:
Hi , Im new to MySql and i have no idea how to convert a sql server user defined function to MySql. Can any one help me.. CREATE FUNCTION ChildCategories (@categoryId int, @taxId int, @dummyTaxId int) RETURNS @retTable TABLE (path varchar(2048), parent_id int, category_id int,
0
1326
by: Philippe Poulard | last post by:
I don't know if anybody already designed an URN scheme for XML; here is a proposal: I call it the "XML URN Scheme", or XUS. ========= URNs are logical names used to identify resources. XUS defines an URN Scheme for XML documents that works on a delegation model. As XUS (XML URN Scheme) is used exclusively for XML documents, it uses namespace URIs to identify this scheme and the delegate scheme.
1
1535
by: Simon Matthews | last post by:
Hope someone can help an Access beginner! I've just started keeping my surgical logbook on access and it's a simple flat-file affair. I have created several queries that will list cases performed at different hospitals and reports based on the queries to print out the relavent details. What I would like to do is have a summary sheet in the Report Footer section that lists a grid of each type of procedure performed as well as the...
6
6303
by: Scott Nixon | last post by:
New to Postgres 7.3 from 7.0. Am having some trouble with a query that worked in 7.0 but not in 7.3.....can't seem to figure out the syntax or find info about how to do this anywhere. Consider for the following query: - 'number' is an integer - 'procedures' is the table name - 'date' is a timestamp
0
5557
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.