basic ML question

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.

Who is Participating?
fridomConnect With a Mentor Commented:
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

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 _ _ = []

my_zip (+) [1,2,3] [3,2,1]

DJ_AM_JuiceboxAuthor Commented:
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.

All Courses

From novice to tech pro — start learning today.