Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

basic ML question

Posted on 2006-11-14
3
Medium Priority
?
248 Views
Last Modified: 2008-03-10
Can someone please explain to me what this statement is doing?

    fun zip f nil nul = nul |
        zip f (h::t)(i::s) = f(h,i)::zip f t s

I know fun zip means function zip() but I'm not sure what the rest of the ML syntax is doing here.

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 17945134
Well zip is  function taking three parameter the first line tells what happens if the parameter match
one of the lista are empty. I would be suprised however that nil = nul but that mayb  possible the second thing does a
destruction on the list if you have some_list then (h::rest) means h is the first element followed by the "rest" of the list
it does this for both parameters and after that it applies f to the both header elements and prepends them to the rest of the list applying this zip function
I have not ML installed here but Haskell and there you could use it this way:
my_zip f (x:xs) (y:ys) = f x y  : my_zip f xs ys
my_zip f _ _ = []

applied:
my_zip (+) [1,2,3] [3,2,1]
[4,4,4]

Regards
Friedrich
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 17948722
Hi fridom,

Where is zip being defined as taking three parameters? On the first line?

    fun zip f nil nul

What does 'f' stand for - and are 'nil' and 'nul' the second and third parameters, or are those reserved keywords?

I have a few more basic questions on this statement which I'll ask shortly. I come from C++ so I'm not new to programming just ML syntax.

Thanks
0
 
LVL 24

Accepted Solution

by:
fridom earned 2000 total points
ID: 17953741
fun is for declaring hey a function is comming f is obviously a function because you can see it from how it it used.
nil and null seems to stand for  an empty list, you can see that it must be lists from the other usage
(zip f (h::t)(i::s) = f(h,i)::zip f t s

so you can se f is applied two elements so it must be a function taking two arguments and returning one another
the h::t destructs a list into first element and the rest of list.

In Haskell you can type :type my_zip and it tells you what the parameters are I guess somethign like that exists in ML also

Regards
Friedrich
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question