Solved

basic ML question

Posted on 2006-11-14
3
240 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
fizzArray2 challenge 1 58
factory design pattern vs abstract factoy design pattern 2 83
sumHeights  challenge 17 61
mapAB Challlenge 35 85
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now