PDA

View Full Version : Help with an SML function (very basic, I guess)


kvajnto
04-06-2004, 11:31 AM
'm just beginning to learn SML (been learning from a book for like a week or so now). I don't have anyone to ask questions, so hopefully someone could help me here.

I'm having a hard time understanding this function found in the book I'm using. I don't really understand it fully.

fun countLEG [] = (0,0,0)
| countLEG(x::rest) =
let val (y1,y2,y3) = countLEG rest in
case Int.compare(x,0) of
LESS => (y1+1,y2 ,y3 )
| EQUAL => (y1 ,y2+1,y3 )
| GREATER => (y1 ,y2 ,y3+1)
end;

Can you use a value such as a tupel as an identifier for an expression (countLEG rest)? Also, let's say the first value in the list is less than 0, then you add 1 to y1. Will this not change the way that countLEG rest is calculated? Why not? if you have something like

a = b * c + d

and then add 1 to a, the right hand side will change as well. I do understand that I'm thinking wrong, I'm not trying to convince anyone that something is wrong with SML =)

I understand what the function does (presents a three tupel that represent how many values in a list are less, equal or greater than 0), but not how it does it.

If anyone could explain it to me I would be REALLY greatfull.

Thanks in advance and sorry for my bad english.

jemfinch
04-06-2004, 02:35 PM
I don't understand your side questions, but I can still explain the function.

First, it defines a "base case." When the list is empty, it returns (0, 0, 0).

When the list is not empty (that is, when there's a head and a tail, in this case called x and rest), it first finds the result of countLEG applied to rest, and then, based on whether x is less than, equal to, or greater than 0, returns a properly incremented tuple.

Jeremy

Smerdyakov
04-06-2004, 04:51 PM
General advice for all programming language questions: Ask on Internet Relay Chat. I prefer Freenode, which to my knowledge is the only IRC network with an SML channel. Come to #sml on irc.freenode.net if you would like to ask other questions. More info on Freenode: http://www.freenode.net/

kvajnto
04-06-2004, 05:22 PM
Okay. First I'd like to thank you both for helping me out. I will certinly check out #sml on freenode.

I'll try to be a bit more specific in what I don't understand: Where does the recursion call take place?
-----------
val (y1,y2,y3) = countLEG rest

In this part of the function, the tuple (y1, y2,y3) is bound to countLEG rest, right? Then if x < 0 this part is executed (if that is the proper word =)):

LESS => (y1+1,y2 ,y3 )

Is this where the recursive call takes place? Isn't "countLEG rest" bound to (y1,y2,y3)?

Also. If you don't set a value to an identifier is it then set to 1:int?

If I don't make any sense at all, then tell me so I can try even harder to explain what I mean =)

jemfinch
04-07-2004, 06:17 AM
Originally posted by kvajnto
I'll try to be a bit more specific in what I don't understand: Where does the recursion call take place?
-----------
val (y1,y2,y3) = countLEG rest

Right there :)

In this part of the function, the tuple (y1, y2,y3) is bound to countLEG rest, right? Then if x < 0 this part is executed (if that is the proper word =)):

No, that part is always executed if the list is not empty.

LESS => (y1+1,y2 ,y3 )

Is this where the recursive call takes place? Isn't "countLEG rest" bound to (y1,y2,y3)?

No, all this is doing here is returning a 3-tuple with the first number incremented because x was less than 0.

Also. If you don't set a value to an identifier is it then set to 1:int?

You can never have an unbound identifier in an SML program that compiles.

Jeremy