Solved

basic ML question

Posted on 2006-11-14
3
243 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
  • 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 500 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
constructor while deserilizing object 16 59
sameEnds challenge 25 116
zeroFront challenge 7 121
count11 challenge 6 84
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 …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 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