How to proper read this SML function that uses foldl? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do you print inside a case statement in SML?SML Function Argument Pattern MatchingSML - Incrementing a value in a tuple during foldl that needs to be returnedImplementing Radix Sort in SMLSignature of higher order functions in SMLError: Operator is not a function - SMLHow to acess argument from a function passed as argument in SMLWhy am I getting “unbound variable or constructor” in my SML programUsing records as function arguments in SMLWhat is error of unresolved flex record in SML?

Delete free apps from library

Sally's older brother

Why is it faster to reheat something than it is to cook it?

Rationale for describing kurtosis as "peakedness"?

Why is the change of basis formula counter-intuitive? [See details]

What initially awakened the Balrog?

Did pre-Columbian Americans know the spherical shape of the Earth?

Why is a lens darker than other ones when applying the same settings?

Why weren't discrete x86 CPUs ever used in game hardware?

How does light 'choose' between wave and particle behaviour?

Asymptotics question

How many time has Arya actually used Needle?

Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?

Is multiple magic items in one inherently imbalanced?

Relating to the President and obstruction, were Mueller's conclusions preordained?

What is the difference between a "ranged attack" and a "ranged weapon attack"?

Simple Http Server

In musical terms, what properties are varied by the human voice to produce different words / syllables?

A term for a woman complaining about things/begging in a cute/childish way

Is it possible for an event A to be independent from event B, but not the other way around?

Project Euler #1 in C++

Weaponising the Grasp-at-a-Distance spell

Getting out of while loop on console

New Order #6: Easter Egg



How to proper read this SML function that uses foldl?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do you print inside a case statement in SML?SML Function Argument Pattern MatchingSML - Incrementing a value in a tuple during foldl that needs to be returnedImplementing Radix Sort in SMLSignature of higher order functions in SMLError: Operator is not a function - SMLHow to acess argument from a function passed as argument in SMLWhy am I getting “unbound variable or constructor” in my SML programUsing records as function arguments in SMLWhat is error of unresolved flex record in SML?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am new to SML and I am having trouble understanding what are arguments and what is being passed to the inner function in this code.



fun print_mat mat = 
let
val _ = (Array.foldl (
fn (arr, _) =>
let val _ = (print_arr arr) in () end
) () mat)
in () end;


Which is meant to be used in:



val mat = 
Array.fromList[
(Array.fromList [0, 1, 1, 0, 1]),
(Array.fromList [1, 0, 1, 0, 0])
]

val _ print_mat mat


What I am failing to see is how the arr gets selected from my mat and used inside the closure function.










share|improve this question






















  • Do you understand what foldl does?

    – melpomene
    Mar 8 at 23:14











  • It applies the function (fn (arr, _) => ... to every element of the Array data structure.

    – nico
    Mar 8 at 23:20






  • 3





    This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

    – Andreas Rossberg
    Mar 9 at 10:09


















0















I am new to SML and I am having trouble understanding what are arguments and what is being passed to the inner function in this code.



fun print_mat mat = 
let
val _ = (Array.foldl (
fn (arr, _) =>
let val _ = (print_arr arr) in () end
) () mat)
in () end;


Which is meant to be used in:



val mat = 
Array.fromList[
(Array.fromList [0, 1, 1, 0, 1]),
(Array.fromList [1, 0, 1, 0, 0])
]

val _ print_mat mat


What I am failing to see is how the arr gets selected from my mat and used inside the closure function.










share|improve this question






















  • Do you understand what foldl does?

    – melpomene
    Mar 8 at 23:14











  • It applies the function (fn (arr, _) => ... to every element of the Array data structure.

    – nico
    Mar 8 at 23:20






  • 3





    This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

    – Andreas Rossberg
    Mar 9 at 10:09














0












0








0








I am new to SML and I am having trouble understanding what are arguments and what is being passed to the inner function in this code.



fun print_mat mat = 
let
val _ = (Array.foldl (
fn (arr, _) =>
let val _ = (print_arr arr) in () end
) () mat)
in () end;


Which is meant to be used in:



val mat = 
Array.fromList[
(Array.fromList [0, 1, 1, 0, 1]),
(Array.fromList [1, 0, 1, 0, 0])
]

val _ print_mat mat


What I am failing to see is how the arr gets selected from my mat and used inside the closure function.










share|improve this question














I am new to SML and I am having trouble understanding what are arguments and what is being passed to the inner function in this code.



fun print_mat mat = 
let
val _ = (Array.foldl (
fn (arr, _) =>
let val _ = (print_arr arr) in () end
) () mat)
in () end;


Which is meant to be used in:



val mat = 
Array.fromList[
(Array.fromList [0, 1, 1, 0, 1]),
(Array.fromList [1, 0, 1, 0, 0])
]

val _ print_mat mat


What I am failing to see is how the arr gets selected from my mat and used inside the closure function.







sml smlnj mlton






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 23:11









niconico

322217




322217












  • Do you understand what foldl does?

    – melpomene
    Mar 8 at 23:14











  • It applies the function (fn (arr, _) => ... to every element of the Array data structure.

    – nico
    Mar 8 at 23:20






  • 3





    This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

    – Andreas Rossberg
    Mar 9 at 10:09


















  • Do you understand what foldl does?

    – melpomene
    Mar 8 at 23:14











  • It applies the function (fn (arr, _) => ... to every element of the Array data structure.

    – nico
    Mar 8 at 23:20






  • 3





    This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

    – Andreas Rossberg
    Mar 9 at 10:09

















Do you understand what foldl does?

– melpomene
Mar 8 at 23:14





Do you understand what foldl does?

– melpomene
Mar 8 at 23:14













It applies the function (fn (arr, _) => ... to every element of the Array data structure.

– nico
Mar 8 at 23:20





It applies the function (fn (arr, _) => ... to every element of the Array data structure.

– nico
Mar 8 at 23:20




3




3





This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

– Andreas Rossberg
Mar 9 at 10:09






This code is overcomplicated. You should be able to shorten it to fun print_mat mat = Array.foldl (fn (arr, _) => print_arr arr) () mat, which might make it easier to read. Or in fact just fun print_mat mat = Array.app print_arr mat, because the fold is not needed here.

– Andreas Rossberg
Mar 9 at 10:09













1 Answer
1






active

oldest

votes


















2














foldl does something more than apply fn (arr, _) => ... to every element. It accumulates a result, which in your case is discarded with the wildcard pattern, _, in favor of the unit value, (). So, as Andreas Rossberg points out, you're not actually accumulating anything, or even generating any result value, so Array.app is better suited.



To understand what



fun print_mat mat = Array.app print_arr mat


does, you can look at its implementation:



fun app f a =
let val a = from_array a
val stop = length_ a
fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
else ()
in lr 0 end


Meaning, it calls f (in your case print_arr) on the 0th row of a (in your case mat), then on the 1st row, then on the 2nd until there are on more rows. When there are no more rows, return (). In the meantime, you have done nothing superfluous such as accumulate some value, since it will always be () that you return when done anyway.



I wonder if you have seen that Array2 exists. It should be pretty ideal for matrices.



As for understanding folding, see ML for the Working Programmer, ch. 5: Functions and infinite data.



I recommend that you understand folding in a list context first.



This is also what's best covered in learning material.






share|improve this answer


















  • 1





    Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

    – nico
    Mar 11 at 23:21







  • 1





    Yes, exactly. Good job!

    – Simon Shine
    Mar 12 at 8:04











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072274%2fhow-to-proper-read-this-sml-function-that-uses-foldl%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














foldl does something more than apply fn (arr, _) => ... to every element. It accumulates a result, which in your case is discarded with the wildcard pattern, _, in favor of the unit value, (). So, as Andreas Rossberg points out, you're not actually accumulating anything, or even generating any result value, so Array.app is better suited.



To understand what



fun print_mat mat = Array.app print_arr mat


does, you can look at its implementation:



fun app f a =
let val a = from_array a
val stop = length_ a
fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
else ()
in lr 0 end


Meaning, it calls f (in your case print_arr) on the 0th row of a (in your case mat), then on the 1st row, then on the 2nd until there are on more rows. When there are no more rows, return (). In the meantime, you have done nothing superfluous such as accumulate some value, since it will always be () that you return when done anyway.



I wonder if you have seen that Array2 exists. It should be pretty ideal for matrices.



As for understanding folding, see ML for the Working Programmer, ch. 5: Functions and infinite data.



I recommend that you understand folding in a list context first.



This is also what's best covered in learning material.






share|improve this answer


















  • 1





    Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

    – nico
    Mar 11 at 23:21







  • 1





    Yes, exactly. Good job!

    – Simon Shine
    Mar 12 at 8:04















2














foldl does something more than apply fn (arr, _) => ... to every element. It accumulates a result, which in your case is discarded with the wildcard pattern, _, in favor of the unit value, (). So, as Andreas Rossberg points out, you're not actually accumulating anything, or even generating any result value, so Array.app is better suited.



To understand what



fun print_mat mat = Array.app print_arr mat


does, you can look at its implementation:



fun app f a =
let val a = from_array a
val stop = length_ a
fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
else ()
in lr 0 end


Meaning, it calls f (in your case print_arr) on the 0th row of a (in your case mat), then on the 1st row, then on the 2nd until there are on more rows. When there are no more rows, return (). In the meantime, you have done nothing superfluous such as accumulate some value, since it will always be () that you return when done anyway.



I wonder if you have seen that Array2 exists. It should be pretty ideal for matrices.



As for understanding folding, see ML for the Working Programmer, ch. 5: Functions and infinite data.



I recommend that you understand folding in a list context first.



This is also what's best covered in learning material.






share|improve this answer


















  • 1





    Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

    – nico
    Mar 11 at 23:21







  • 1





    Yes, exactly. Good job!

    – Simon Shine
    Mar 12 at 8:04













2












2








2







foldl does something more than apply fn (arr, _) => ... to every element. It accumulates a result, which in your case is discarded with the wildcard pattern, _, in favor of the unit value, (). So, as Andreas Rossberg points out, you're not actually accumulating anything, or even generating any result value, so Array.app is better suited.



To understand what



fun print_mat mat = Array.app print_arr mat


does, you can look at its implementation:



fun app f a =
let val a = from_array a
val stop = length_ a
fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
else ()
in lr 0 end


Meaning, it calls f (in your case print_arr) on the 0th row of a (in your case mat), then on the 1st row, then on the 2nd until there are on more rows. When there are no more rows, return (). In the meantime, you have done nothing superfluous such as accumulate some value, since it will always be () that you return when done anyway.



I wonder if you have seen that Array2 exists. It should be pretty ideal for matrices.



As for understanding folding, see ML for the Working Programmer, ch. 5: Functions and infinite data.



I recommend that you understand folding in a list context first.



This is also what's best covered in learning material.






share|improve this answer













foldl does something more than apply fn (arr, _) => ... to every element. It accumulates a result, which in your case is discarded with the wildcard pattern, _, in favor of the unit value, (). So, as Andreas Rossberg points out, you're not actually accumulating anything, or even generating any result value, so Array.app is better suited.



To understand what



fun print_mat mat = Array.app print_arr mat


does, you can look at its implementation:



fun app f a =
let val a = from_array a
val stop = length_ a
fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
else ()
in lr 0 end


Meaning, it calls f (in your case print_arr) on the 0th row of a (in your case mat), then on the 1st row, then on the 2nd until there are on more rows. When there are no more rows, return (). In the meantime, you have done nothing superfluous such as accumulate some value, since it will always be () that you return when done anyway.



I wonder if you have seen that Array2 exists. It should be pretty ideal for matrices.



As for understanding folding, see ML for the Working Programmer, ch. 5: Functions and infinite data.



I recommend that you understand folding in a list context first.



This is also what's best covered in learning material.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 11 at 12:38









Simon ShineSimon Shine

10.2k13050




10.2k13050







  • 1





    Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

    – nico
    Mar 11 at 23:21







  • 1





    Yes, exactly. Good job!

    – Simon Shine
    Mar 12 at 8:04












  • 1





    Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

    – nico
    Mar 11 at 23:21







  • 1





    Yes, exactly. Good job!

    – Simon Shine
    Mar 12 at 8:04







1




1





Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

– nico
Mar 11 at 23:21






Thank you for this answer. I was not aware of the type of ( ). I also appreciate the links. Here my fn does not return anything, thus it is best for me to use Array.app as I am not doing any folds. With regards to (Array.foldl (fn) () mat) From the implementation, now I understand that it requires 3 arguments. For future reference the second argument - that I passed ( ) - has to match the return type of fn, and it is the value being aggregated after the fold function fn is applied to each element of the Array data strucuture.

– nico
Mar 11 at 23:21





1




1





Yes, exactly. Good job!

– Simon Shine
Mar 12 at 8:04





Yes, exactly. Good job!

– Simon Shine
Mar 12 at 8:04



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072274%2fhow-to-proper-read-this-sml-function-that-uses-foldl%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved