<jeremy_c>
How do I say something like person#set_age -1 ; ? I am currently doing person#set_age (0 - 1) ; ...
<Smerdyakov>
(-1)
<jeremy_c>
Smerdyakov: thanks.
CosmicRay has left #ocaml []
fab_ has joined #ocaml
_fab has quit [Read error: 110 (Connection timed out)]
KrispyKringle is now known as Kringelina
Smerdyakov has quit ["Leaving"]
Kringelina is now known as KrispyKringle
pango__ has joined #ocaml
pango_ has quit [Read error: 110 (Connection timed out)]
<jeremy_c>
Can someone tell me what's wrong with this?
<jeremy_c>
let int_from_opt ?(default : int = (-1)) value =
<jeremy_c>
match value with
<jeremy_c>
None -> default
<jeremy_c>
| Some v -> v;;
<jeremy_c>
# int_from_opt ?default:10 a ;;
<jeremy_c>
This expression has type int but is here used with type int option
<jeremy_c>
10 in underlined on the console.
<jeremy_c>
Do you know what type inference is?
<jeremy_c>
opps.
<jeremy_c>
wrong console. I was teaching my brother about a few things about ocaml :-)
<flux__>
jeremy_c, ~default:10
<flux__>
jeremy_c, or ?default:(Some 10)
<jeremy_c>
flux__: but the return value of that function is an int ... not int option
<jeremy_c>
oh.
<jeremy_c>
wait, mis read.
<jeremy_c>
hm. ~ instead of ?
<flux__>
yes
<flux__>
and that's one thing that's been done right ;)
<jeremy_c>
I thought ? was optional and ~ was by name ?
<flux__>
so you can actually contain in a variable then optionality of the parameter
<flux__>
I gave you two options
<flux__>
s/options/alternatives/
<jeremy_c>
?default:(Some 10) would return a int option , no?
<flux__>
no
<jeremy_c>
hm, I don't understand that.
<flux__>
int_from_opt ?default:None 42 would be the same as int_from_opt 42
<jeremy_c>
but that would fail, wouldn't it?
<jeremy_c>
The function takes a int option
<flux__>
oh, you have a bug anyway in that function I think
<flux__>
actually, wait, I'm rethinking what you're doing :)
<flux__>
ok, so that's called like int_from_opt (Some 42)
<jeremy_c>
right
<jeremy_c>
or int_from_opt None
<flux__>
you can call it like int_from_opt ~default:42 None;;, which is what you wanted, right?
<jeremy_c>
yes
<jeremy_c>
you confused me with the ?default:(Some 10) ...
<flux__>
but you can also call it like int_from_opt ?default:(Some 42) None or int_from_opt ?default:None None which is the same as int_from_opt None
<flux__>
the first one would be the same as int_from_opt ~default:42 None
<jeremy_c>
How could that be since it returns an int ?
<jeremy_c>
?default:None None would return None which is not an int ?
<flux__>
nope
<flux__>
it would say there is no optional parameter default
<flux__>
and it would hence default to -1
<jeremy_c>
How would it default to -1 ?
<jeremy_c>
Oh.
<jeremy_c>
nm.
<jeremy_c>
Because that's my default on the ?(default=(-1))
<flux__>
yes
<pango__>
(btw unary - is ~-, but using parenthesis is at least as readable ;) )
<pango__>
# min 5 ~-3 ;;
<pango__>
- : int = -3
ppsmimou has joined #ocaml
pango__ has quit [Remote closed the connection]
pango has joined #ocaml
Skal has joined #ocaml
joshcryer has quit [Read error: 110 (Connection timed out)]
CLxyz has quit []
m3ga has joined #ocaml
ramkrsna has quit [Read error: 110 (Connection timed out)]
ramkrsna has joined #ocaml
m3ga has quit ["disappearing into the sunset"]
Tachyon76 has joined #ocaml
Raziel has quit [Remote closed the connection]
Raziel has joined #ocaml
Tachyon76 has quit ["Leaving"]
malc_ has joined #ocaml
ppsmimram has joined #ocaml
ppsmimram has quit [Client Quit]
ramkrsna has quit [Read error: 110 (Connection timed out)]
ramkrsna has joined #ocaml
Schmurtz has joined #ocaml
mkhl has joined #ocaml
mkhl has left #ocaml []
__DL__ has joined #ocaml
<jeremy_c>
I have a unit test: let test_to_drg_length _ = assert_equal 400 (FixedWidth.length FixedWidthModel.to_drg) ... when I compile, it states "Unbound value FixedWidth.length" ... however, if at the top of the file I add open FixedWidth all compiles fine. I do not understand why that is happening, I thought open put the code into the local scope (removing the necessity to prefix it with the module name), but if a module is compiled in, it ca
<jeremy_c>
with FixedWidthModel, which is a module that is tested here and I do not have to add an open line for it.
<pango>
unless you defined a module FixedWidth within fixedWidth.ml file, I have no idea
<jeremy_c>
pango: arg. that was dumb of me, I should have known that was the problem.
<jeremy_c>
That had me baffled. I have many other modules, but I have not moved them to "real" modules yet until the interface is a bit better defined, then I was going to create .mli files.
<jeremy_c>
Is there benefit to using let ... and ... and ... and instead of let ... in let ... in let ... in ? I know sometimes in my code let ... in is required, because the next let requires the previous definition, but in other cases?
<pango>
simultaneous definitions... mostly useful in the let rec ... and ... and ... form, to defined cross-recursive functions
<pango>
but let ... and ... and ... also exists
<mellum>
Also, it's less to type.
__DL__ has quit [Remote closed the connection]
<pango>
I avoid using let and without strong need, because it can add unwanted type dependancies
Snark has joined #ocaml
<pango>
(like some function becoming monomorphic, a while ago in this channel)
__DL__ has joined #ocaml
Smerdyakov has joined #ocaml
__DL__ has quit [Read error: 104 (Connection reset by peer)]
__DL__ has joined #ocaml
Schmurtz is now known as Schmurtz10964321
Schmurtz10964321 is now known as Schmurtz
smimou has joined #ocaml
Schmurtz is now known as Schmurtz28356
<jeremy_c>
Am I missing something or is there no easy way to detect a existence of a sub string in another string? String.contains_string "and" "Jack and Jill" ; ?
<Smerdyakov>
Str provides that for regular expressions.
<jeremy_c>
:-/ I have been searching String ... I forgot about Str.
<jeremy_c>
Thanks Smerdyakov
pango has quit ["Leaving"]
pango has joined #ocaml
Schmurtz28356 is now known as Schmurtz
vodka-goo has joined #ocaml
gim has joined #ocaml
<jeremy_c>
For nothing more than learning purposes, anyone care to comment on http://pastebin.com/488990 ? I had troubles trying to think about this process in functional terms. It's a series of if ... else if ... else if ... else's
<Smerdyakov>
It would be easy to write a generic function that is parameterized by a (string * string) list.
<jeremy_c>
it just seems to me that there's repetitive code and that it could be reduced some how.
<jeremy_c>
and the function name should get get_payer_code or something better than is_in_list .
<jeremy_c>
Then again, I believe you were speaking of making it a generic function, prob taken totally out of the context of to_payer_code which would make it more useful and make that function look quite a bit better.
<flux__>
List-module has all kinds of fun functions, albeit I can't think of a one that would short circuit as fficiently
<jeremy_c>
It's not a speed critical task, nor am I going through many options. I'm just happy I am understanding some things like this and am able to make them work!
<Smerdyakov>
You'll get a matching exception if the input matches none of the options.
<flux__>
smerdyakov, actually no, "" matches everything?
<Smerdyakov>
Oh, right.
<flux__>
consider this: List.hd (List.concat (List.map (fun (k, v) -> if is_in k then [v] else []) [..list here..]))
<Smerdyakov>
But, still, using List.hd and List.tl is considered bad style.
<flux__>
yes, it pattern matching would be one improvement
<flux__>
but it easily produces warnings
<jeremy_c>
Smerdyakov: it will always match because my last option is blank.
<flux__>
(I was originally writing a match List.concat .. with -version but then I noticed that)
<flux__>
anyway, had I written it in that way, it would've become apparent that the empty case would need to be handled someway
<jeremy_c>
but, that depends on the list sent, I guess. I did that so I didn't have to do matching on the tail to see if it was empty, and then return a default value.
<flux__>
that's one thing ml-like typed languages are great at :)
<flux__>
(well I suppose that's not because of the type system, but rather because functional languages encourage recursion, and some support pattern matching)
<dylan>
flux__: (yes, because one can do the same in Erlang, which isn't typed like ml)
<jeremy_c>
pango: I see what (v, c) :: q is doing although I've never learned that syntax before. That's pretty cool.
<pango>
:: is list contructor, here used in pattern matching context...
<jeremy_c>
pango: it's also obviously ripping off the head (putting it into v, c), and the tail (q) right?
<pango>
that's what pattern matching does
<Smerdyakov>
jeremy_c, if you haven't learned that syntax before, then you are being lazy about reading about OCaml, and it is rude for you to expect us to help you.
<jeremy_c>
Smerdyakov: It's one of the first pages in my book but it is using it in regards to 1 :: 2 :: 3 :: 4 :: etc...
<Smerdyakov>
I don't know what that means.
<jeremy_c>
Smerdyakov: ah, here it is. Your right pg 40.
<jeremy_c>
Smerdyakov: they spend about 1 page on it. I am digesting quite a bit of information this last week learning a functional language for the first time. I have read this information, but I guess I don't have 100% retiontion on it.
<Smerdyakov>
I don't know what book you're reading, but I'd suggest reading the tutorial in the OCaml manual through before continuing.
<jeremy_c>
I have been reading Developing Applications with Objective Caml by Oreilly
<dylan>
learning is like compiling, it takes multiple passes to extract all the required data.
<jeremy_c>
Has a copyright date of 2000 .. I was a bit concerned about that, but havn't been having problems yet due to old code or anything.
<jeremy_c>
dylan: yes, I know. I've been programming for a little over 15 years now but I have never ventured into a functional language. I was actually quite pleased at how I am doing and things I am solving on my own.
<jeremy_c>
Smerdyakov: I'll go spend time with the tutorial you have mentioned.
<dylan>
jeremy_c: Hehe. I've been coding for only about five or six, but learned perl, C, and scheme before ocaml. It took about six attempts to learn it... :)
<dylan>
After lots of cdr, cdddr, caddr, etc, pattern matching is like sweet, sweet zombie jesus.
<pango>
jeremy_c: as you can check, in the book, lists are mentionned in the chapter about pattern matching
<jeremy_c>
pango: Are you speaking of the Oreilly book? I don't see a chapter on lists. or are you talking of the OCaml manual?
<pango>
the previous URL I gave refers to the online version of the book... Is this missing in the printed version ?
<jeremy_c>
It's certainly different than the book I have. hm.
<jeremy_c>
hm. maybe not, I'm just confused about where that is in my book.
<jeremy_c>
It's not a chapter on lists, it's a small section of chapter 2 that your refering to. I looked in my table of contents for a chapter on lists, that's where I was confused.
<jeremy_c>
There we go... It's the same thing.
* jeremy_c
has converted quite a few of his functions away from List.hd List.tl, now using pattern matching. He is also going to go spend some time with the OCaml Manual.
Snark has joined #ocaml
Snark_sad has quit [Read error: 110 (Connection timed out)]
zigong has joined #ocaml
Snark has quit ["Leaving"]
vodka-goo has quit ["Connection reset by by pear"]
_DL_ has joined #ocaml
__DL__ has quit [Read error: 110 (Connection timed out)]
<jeremy_c>
pango: that's looking much cleaner than my original one. I knew there had to be a better way of doing it.
<jeremy_c>
pango: thank you for showing me these few examples.
<jeremy_c>
pango: hm, although Str.string_match will not work in my case because I am matching just a substring. It could be MEDICARE EAST or EAST MEDICARE ... Str.string_match tries to match at an exact position, I have to use Str.search_forward which searches from a position forward until it finds it, returns the position or raises Not_found
<pango>
that's why I prepended .* to the regexp string
<jeremy_c>
pango: ah.
<pango>
but it's orthogonal to the use of a HOF, I was just experimenting for a way to avoid exceptions
<jeremy_c>
yeah, but it's much cleaner, which was actually my original question. I had a series of if else if for each condition :-/
<jeremy_c>
I am told (by propaganda) that a functional langauge provides a much cleaner, enjoyable, easier to maintain and debug methodology of programming. I'm trying my best to see if that's true or not. What do you think?
<malc_>
bollocks
_DL_ has quit [Remote closed the connection]
<pango>
that you can write crap in any language ;)
<jeremy_c>
malc_: ?
<malc_>
okay hard way
<malc_>
that statement is false
<jeremy_c>
malc_: I thought that's what you meant, just wanted to make sure :-)
<jeremy_c>
pango: One thing that attracted me to OCaml was it's speed, memory consumption and native binaries on the platforms I use the most.
<jeremy_c>
should say compilation not binaries.
<jeremy_c>
I will say that I *am* enjoying OCaml and it's ability to solve problems very cool ways.
<jeremy_c>
in very cool ways
<pango>
same here, but the question on how to write maintainable code in ocaml is still open in my mind...
<pango>
they're so many choices...
<malc_>
pango: how long do you code in caml?
<jeremy_c>
How do you handle all the accessory functions you acquire, such as my StringHelper.strip function that removes trailing spaces, tabs, new lines, etc... ? I've kind grouped them together by function (hense StringHelpers) and make a library containing all my "Helpers" (StringHelper, FileHelper, etc...)
<pango>
I only wrote few lines of ocaml at uni, so I'll say I only use it since mid 2002... mainly to hack mldonkey, and recently for much smaller personal projects
<malc_>
ok
<pango>
I don't find mldonkey code specially maintainable, but have a hard time seeing how to improve the situation
<pango>
many (most ?) datastructures are not encapsulated, so invariants that are not covered by types alone are hard to find/enforce
smimou has quit ["bli"]
<pango>
pattern matching is cool, but it's not a close friend of data abstraction