Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# printf paddings

Tags:

f#

I'm beginner in F#, What does %-5s and %5s do in the following code? I thought it gives space paddings but I'm not sure how it pads?

    printfn "%-5s %5s" "a" "b"

When I tried printf "%-5s %5s" "a" "b" "v" or printf "%-5s %-5s" "a" "b" "c" That gives me an type match error, I don't understand the syntax, It dosen't seem that it is printing the concatenation of the three strings "%-5s %5s", "a" and "b". It seems to put "a" and "b" between the %5s. so why can't I add "c" after? Also, I'm following tutorial on Youtube: https://www.youtube.com/watch?v=c7eNDJN758U

If There is any other good source of learning the essence of functional language by F#, please give some suggesting!

like image 729
user614287 Avatar asked Feb 07 '26 04:02

user614287


1 Answers

The %5s and %-5s are formatting codes along with width and alignments specified.

The hyphen in your first code aligns the value to the left.

As an example, I have the following 2 lines:

printfn "%-5s %5s" "a" "b"
printfn "%-5s %5s" "yo" "hey!"

What gets printed is the following:

a        b
yo    hey!

Here is a page that explains more about the printfn function. The entire site is an excellent resource for learning to use F#.

Good luck!

PS - as for your question about the error when adding the 3rd parameter, the printfn function will check for both the types and number of parameters you supply based on the format codes in the string. This is explained in the linked page I included earlier.

like image 152
Eric Olsson Avatar answered Feb 09 '26 13:02

Eric Olsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!