<brainacid>
Using GreenTea Press How to Think Like a Computer Scientist
<brainacid>
Seerking assistance with an exercise
<brainacid>
Anyone awake that would like to help? Its a Chapter 3 exercise. Nothing hard but I cant seem to figure it out
<ggole>
Go ahead and ask
<ggole>
People may or may not answer: it is IRC, after all
<brainacid>
Ok
<brainacid>
let print_lyrics() =
<brainacid>
print_string "I'm a lumberjack, and I'm okay. \n";
<brainacid>
print_string "I sleep all night and I work all day. \n";;
<brainacid>
oops
<brainacid>
using String.length("monje") write a function that justifies the argument to the right 70 columns
<ggole>
Do you understand what "justify" means in this context?
<brainacid>
to move the text
<brainacid>
I can tell it to print_string (" ");;
<ggole>
So
<brainacid>
but that is not clean or programming
<brainacid>
i think
<ggole>
The first thing is to figure out how many spaces you need to insert at the front to do the job
<brainacid>
i have to get over the frustration that this causes me first in order to think clearly lol
<brainacid>
correct
<brainacid>
im working on it as we chat
<brainacid>
trying hard to figure it out
<ggole>
Well
<ggole>
How many spaces would you need to move a string of length 1 by?
<pippijn>
and next, teach him mathematical induction
<rks`>
ggole: for your issue on merlin, I'll keep waiting for a day or two while hoping lehy shows up, if he doesn't then I'll see if there's something I can do about it
<brainacid>
69 spaces
cdidd has joined #ocaml
<ggole>
rks`, thanks. If I get some time maybe I'll try to patch it myself.
<brainacid>
i just to understand how to do it with the little bit of code i have learned thus far.
<rks`>
ok :)
<ggole>
brainacid, ok, now what about a string of length 2?
<brainacid>
can i help debug anything? i would like to be useful
<brainacid>
68
<brainacid>
where u going? i dont quite see it yet
<ggole>
That should be enough to show you the pattern
<brainacid>
ok
<brainacid>
thanks (no sarcasm)
<brainacid>
:)
<brainacid>
cant get it in my skull
<brainacid>
you cant use operations +-*/ with strings, right?
<adrien>
you cannot
<adrien>
in ocaml they have one and only one meaning: integer addition/substraction/multiply/divide
<brainacid>
correct
<pippijn>
adrien: well done
<adrien>
sounds like I broke something? :P
<adrien>
(well, I have but the fix is coming ;p )
<pippijn>
:)
<brainacid>
im on Functions chapter 3....cant figure out this exercise...:(
<brainacid>
basiclly increase the length of a string so it looks right justified...lol
chrisdotcode_ has quit [Remote host closed the connection]
<brainacid>
# String.length "home";;
<brainacid>
- : int = 4
<brainacid>
needs to be something like int = 74
<pippijn>
why 74?
<brainacid>
how to add spaces to a string
* brainacid
ponders
<brainacid>
right justification
<pippijn>
but if you add 74 spaces, it's not right-justified
<adrien>
not 76?
<pippijn>
it's just indented
<brainacid>
pippijn, the exercise wants me to have the word start at column 70
<pippijn>
I see
<brainacid>
home
<pippijn>
do you know how to concatenate two strings?
<brainacid>
yes
<pippijn>
how?
<brainacid>
let word1 ="one";; let word2 ="two" ;; print_string word1^word2;;
<pippijn>
ok, you'd need () around those, but yes, right
* brainacid
typed it without looking at books
<pippijn>
print_string (a ^ b)
<brainacid>
let word1 = ("one")
<brainacid>
oh
<brainacid>
cuz its a statement
<brainacid>
print_string (word1^word2);;
<pippijn>
let a = "one";; let b = "two";; let together = a ^ b;;
<pippijn>
ok, so your idea was to have a string of 70 spaces as `a', right?
<brainacid>
they want a function that uses String.length some how to get it done
<brainacid>
yes that was my very first idea before getting on IRC...which I havent in years
<brainacid>
I just started this morning to learn programming
Drup has joined #ocaml
<brainacid>
/msg pippijn OCaml provides a standard module called String, which includes a useful function
<brainacid>
called length that returns the length of a string, so the value of
<brainacid>
String.length("monje")"
<brainacid>
is 5.
<brainacid>
Write a function named right_justify that takes a string named s as a parameter and prints the
<brainacid>
string with enough leading spaces so that the last letter of the string is in column 70 of the display.
<brainacid>
# right_justify('monje')
<brainacid>
monje
<brainacid>
I hope i didnt spame the room
<brainacid>
pippijn,did u receive that text?
<pippijn>
yes, in the room
<pippijn>
and don't message me
<brainacid>
oops
<brainacid>
ok
<pippijn>
this is ok, but use a paste site next time
<pippijn>
anyway
<brainacid>
ok got it
<pippijn>
your exercise wants you to have the *last* letter in column 70
<brainacid>
look I appreciate the help here
<pippijn>
that means, it *ends* in column 70
<pippijn>
not starts
<brainacid>
ok
<brainacid>
what confuses me is the module String
<brainacid>
how can I add spaces by know the length...i mean easy subtraction yes...but is it that simple and im over complicating
<pippijn>
it is that simple
<brainacid>
ok
<brainacid>
so
<pippijn>
you don't need to create a new string, though
<pippijn>
it just wants you to print the string
<pippijn>
so it suffices to have a loop that prints one space at a time
<pippijn>
N times, where N is the number of spaces you want to print
<gour>
brainacid: imagine you have box with 70 empty slots. first (70-length(string)) are supposed t obe empty and then in the remaining slots you put each char in one slot
<brainacid>
let right_just s = print_string " "^s;; right_just ("home");;
<pippijn>
no
<pippijn>
brainacid: the last character should be in column 70
<pippijn>
so 'e' should be in 70, 'm' should be in 69, 'o' in 68 and 'h' in 67
<brainacid>
yes yes
<brainacid>
but Im sorry I cant see how
<brainacid>
Give me some time
<brainacid>
I dont any of u to get aggrevated
<brainacid>
*want
<pippijn>
how many spaces do you have to print so that 'h' of "home" ends up in column 67?
<gour>
you 'create' (for printing) new string consisting of empty_spaces + string, all to fit in 70 slots
<pippijn>
gour: not necessary
<pippijn>
a loop printing the number of spaces is enough
<gour>
pippijn: that why i put create in quotes
<pippijn>
ok
<adrien>
(for slightly later there is a solution with String creation + copy to that string; I have to leave but it will be an interesting one I think)
<brainacid>
well heres the thing pippijn i havent learned loops yet and it wants me use the code learned in these first 3 chapters
<brainacid>
gour, the 'functional' pat of the book got me
<brainacid>
*part
<gour>
brainacid: ok, take a look at RWO
<brainacid>
How to think like a (functional) programmer
<brainacid>
ok
<orbitz>
i like to oat, oat, oat, opples and bononos
<brainacid>
Real World OCaml is aimed at programmers who have some experience with conventional programming languages
<brainacid>
pasted from book
<brainacid>
I my friends dont know anything
<brainacid>
lol
<orbitz>
that's a shame
<gour>
brainacid: well, the book is more oriented towards functional part, so not having experience with imperative programming might be advantage for you for npt being spoiled ;)
demonimin has quit [Ping timeout: 245 seconds]
<orbitz>
oh hai gour
<gour>
brainacid: just go through for a while and see
<adrien>
gildor: btw, I made a patch that fixes my issue with findutil's FileUtil.find and symlinks; I had to change quite a lot of things and I've updated the testsuite but I need to go over the changes once more because I'm not completely happy with some bits of it
Kakadu has quit [Quit: Konversation terminated!]
Kakadu has joined #ocaml
travisbrady has joined #ocaml
demonimin has quit [Remote host closed the connection]
demonimin has joined #ocaml
travisbrady has quit [Quit: travisbrady]
Yoric has joined #ocaml
UncleVasya has joined #ocaml
oriba has joined #ocaml
talzeus has quit [Remote host closed the connection]
talzeus has joined #ocaml
shinnya has quit [Ping timeout: 252 seconds]
talzeus has quit [Ping timeout: 264 seconds]
brainacid has joined #ocaml
<brainacid>
i still havent figured it out
<brainacid>
going to try another
ollehar has quit [Ping timeout: 252 seconds]
Kakadu has quit [Ping timeout: 246 seconds]
f[x] has quit [Ping timeout: 264 seconds]
<brainacid>
is this incorrect
<brainacid>
let x = let y = "\s"^x;;
<brainacid>
i want to put a space in front of....i meant let spc x = let y = "\s"^x;;
<Drup>
brainacid: you don't need \s, just put a space
<brainacid>
got it
<brainacid>
but how do i pass that to the parameter given to the function?
<brainacid>
its giving a Syntax error
<Drup>
why do you have the y variable ?
<brainacid>
dunt no
<brainacid>
ii thought thats how it was written
<brainacid>
so the function is to add space to its parameter
<Drup>
Sure, but you don't need an additional variable
<brainacid>
let spc x = ?
<Drup>
just return tje result
<Drup>
yes
<Drup>
the*
<brainacid>
ok thats where my thoughts fall
<Drup>
let spc x = " " ^ x
<brainacid>
that easy?
<Drup>
yes
<brainacid>
i was print.string (" "^x);;
Yoric has quit [Ping timeout: 264 seconds]
<Drup>
print is for printing, if you want a new string with a space on front of it, you don't print
<companion_cube>
or you can use Printf
<Drup>
companion_cube: calm down, don't use complicate things x)
<brainacid>
i have just read 3 chapters of this book
<brainacid>
so yeah ... im at elementary level
<brainacid>
so once the function is executed the variable x is erased right
<Anarchos>
but the syntax a;b implys that a is of type unit
<Drup>
brainacid: ";" is the sequence operator "a ; b" means "do a, then do b and return b"
<brainacid>
but the function requires only ; to seperate statements and ;; to end the function. Right?
<pippijn>
brainacid: in (a;b), the value of a is discarded
<brainacid>
ok
<brainacid>
Illumination
<pippijn>
if you want to keep the value, you need to bind it to a name
<pippijn>
let name = stuff in more stuff
<pippijn>
stuff is "2" ^ x
<pippijn>
let name = "2" ^ x in String.length name
ollehar has quit [Ping timeout: 248 seconds]
<pippijn>
you can also re-use the name "x", that will hide the previously bound x
<brainacid>
right
<pippijn>
let x = "2" ^ x in String.length x
<brainacid>
so any kind of expresion should be referenced by a variable
<brainacid>
ahh...in...mmm...need to read more
<pippijn>
if you want to do things to the value, yes
<pippijn>
let spc x = " " ^ x
<pippijn>
now, spc is a function that you can call with a string
<pippijn>
spc "hello"
<pippijn>
this will evaluate to " hello"
<pippijn>
the value of (spc x) is (" " ^ x), so the value of (spc "hello") is (" " ^ "hello")
<brainacid>
nice
<pippijn>
it's like in math, when you write f(x) = 2x
<brainacid>
u caould teach
<brainacid>
:)
<pippijn>
meaning f(x) = 2 * x
<pippijn>
so if you call f(4), it means you replace all occurrences of 'x' in the function with '4'
<pippijn>
giving you f(4) = 2 * 4 = 8
<pippijn>
in ocaml, it's the same
<pippijn>
let f x = 2 * x
<brainacid>
right
<pippijn>
f 4 = 2 * 4 = 8
<pippijn>
simple as that :)
<pippijn>
and let ... in is a way of saying "where"
<pippijn>
for example, if you say
<pippijn>
f(x) = y + 3 where y = x * 2
<pippijn>
in ocaml, you say
<pippijn>
let f x = let y = x * 2 in y + 3
<pippijn>
so a series of let..in are just like a series of "where"
<brainacid>
i see
<pippijn>
and the last thing you write is the value of the whole thing
<pippijn>
so you can make a series of intemediate computations, and you can print stuff in between, and in the end, the last expression is returned by the function
<pippijn>
let f x = 3; 2; "hello"; x + 4; x * 2
<pippijn>
all those things are discarded, and only x * 2 is returned
<brainacid>
interesting
<pippijn>
so if you want to keep any of them, you need to say "let some_name = 3 in" etc.
<brainacid>
let another =4 ;;
<brainacid>
in
<brainacid>
ok let me go practice
<pippijn>
;; ends a top-level "sentence"
<pippijn>
;; can never come after "in", because ocaml wants to know "in what?"
<pippijn>
let something in ... you need something else after "in"
<brainacid>
ok
<pippijn>
there are two different "let" things in ocaml, it's a little confusing
<brainacid>
let f x = let y = " " ^ x in String.length (y);;
<pippijn>
the toplevel let is not the same as the expression let..in
<brainacid>
?
<pippijn>
yes, this is correct
<pippijn>
in ocaml, we usually don't write () around things where it's not necessary
<pippijn>
just `String.length x' is fine
<brainacid>
where is it necessary?
<brainacid>
ok
<pippijn>
it's necessary to override operator precedence
<pippijn>
for example: String.length x + 4
<pippijn>
is the same as: (String.length x) + 4
<pippijn>
if you write: String.length x ^ " world"
<pippijn>
that's wrong, because it is the same as: (String.length x) ^ " world"
<pippijn>
you can see how it is wrong, if you start replacing the function applications by their value
<pippijn>
first, we assume that x = "hello"
<pippijn>
so, String.length x = String.length "hello" = 5
<brainacid>
i see
<pippijn>
now we can replace that in the expression: (String.length x) ^ " world" = (String.length "hello") ^ " world" = 5 ^ " world"
<pippijn>
so that doesn't work, because ^ concatenates a string and another string, but 5 is a number
<brainacid>
so got it
<pippijn>
so in order to make it work as we want, we need () around the ^ expression
<pippijn>
String.length (x ^ " world")
f[x] has quit [Ping timeout: 246 seconds]
<pippijn>
this does what we want, because now x is first replaced with "hello": String.length (x ^ " world") = String.length ("hello" ^ " world") = String.length "hello world" = 11
olliefr has joined #ocaml
demonimin has quit [Ping timeout: 264 seconds]
k4nar has quit [Read error: Connection reset by peer]
<rndm>
it looks like some kind of matched decomposition of a stream into head, tail?
<companion_cube>
it's a syntax extension for streams
<rndm>
if i wanted to find the documentation or other examples of that? what's the best place to look? it doesn't appear to be in the streams section of the ocaml tutorial
<rndm>
oh. but it is in the "streams expression" section. i see. sorry about that :)
<brainacid>
i might be better off learning python
<brainacid>
whats the word here? OCaml good for green newbies
<brainacid>
it seems im having such a difficult time understanding
<Drup>
brainacid: The first programming experience would probably be as difficult in most languages
<brainacid>
understood
travisbrady has quit [Quit: travisbrady]
<brainacid>
well...let me continue reading
<brainacid>
maybe my text is not worth it....
<brainacid>
Howt to Think like a (functional) programmer by greentea press
<Drup>
it's probably not a good intro to programming in general
<Drup>
(according to the title, I don't know the book)
<brainacid>
its actually supposed to be the best intro to programming
nikki93 has joined #ocaml
<brainacid>
How to Think Like a Computer Scientist is an introductory programming textbook based on the OCaml language. It is a modified version of Think Python by Allen Downey.
<brainacid>
It is intended for newcomers to programming and also those who know some programming but want to learn programming in the function-oriented paradigm, or those who simply want to learn OCaml.
<gour>
maybe it is Learn The OCaml Hard Way :-)
<brainacid>
yup
<brainacid>
anyone understand currying
wwilly has quit [Remote host closed the connection]
<technomancy>
(possibly ctypes-specific; I don't know much about C libraries)
<brainacid>
i see
<gour>
the book says: "This is called ‘currying, after Haskell Curry. It is also sometimes called partial application." according to that paste, but those are not really the same, iirc
<brainacid>
but i still dont get it ... on how to code it
<brainacid>
copy that
<mrvn>
brainacid: let bla x = fcon x "-"
<gour>
RWO also makes distinction...
zpe has joined #ocaml
zpe has quit [Remote host closed the connection]
olliefr has joined #ocaml
zpe has joined #ocaml
<mrvn>
gour: afaik currying is what the compiler does to make it work. partial application is what you do to take advantage of it.
Yoric has joined #ocaml
zpe has quit [Remote host closed the connection]
<gour>
mrvn: RWO puts it as: "The practice of applying some of the arguments of a curried function to get a new function is called partial application."
<mrvn>
gour: excatly.
<mrvn>
Somehow I don't even get why people make such a deal about currying in a functional language. Functions are first class so partial application comes totaly natural.
<mrvn>
The actual hard part, imho, is uncurrying functions.
<companion_cube>
depends on whether the syntax makes it easy to curry
<mrvn>
Optimizing the compiler output for when a full application is used, or just multiple arguments at once.
<mrvn>
companion_cube: fun x y z is just a syntactic suggar for function x -> function y -> function z. You can do that trivially while parsing.
<companion_cube>
mrvn: in OCaml, yes.
<mrvn>
in any functional language
<companion_cube>
languages like scala don't have such a nice syntax
<mrvn>
as soon as function are first class objects you can trivial do it
<companion_cube>
of course you can write it, but it's less convenient, the default is uncurried
<companion_cube>
my point is easyness, not doability
<mrvn>
companion_cube: My point is that you can always write the function in curried form manually so the compiler already needs to support curried functions. Actually supporting uncurried functions takes extra work.
<companion_cube>
unless you accept tuple arguments as uncurried :)
<mrvn>
companion_cube: fun (x,y,z) -> x + y + z. That takes extra work because you have to unpack the tuple.
nikki93 has quit [Remote host closed the connection]
<companion_cube>
OCaml's abstract machine doesn't do uncurried, but it optimizes for the case where all arguments are present
Yoric has quit [Remote host closed the connection]
<companion_cube>
if tuples are by value that's fine
Yoric has joined #ocaml
<brainacid>
well guys...im super green. never coded
<brainacid>
I appreciate the help
<mrvn>
brainacid: keep on reading. don't get discouraged.
<brainacid>
I will not. I shall push on trukin
<brainacid>
could u shed some light mrvn
<brainacid>
how to append a symbol multiple times?
<brainacid>
i just dont see it...with the limited tool box they gave me
<mrvn>
with a for loop or recursion
<brainacid>
well im in chapter 3 and its very basic
<brainacid>
i think its recursion
nikki93 has joined #ocaml
<brainacid>
but the only string maipulation tools they showed so far is ^
<brainacid>
thats it
<mrvn>
something like: let rec times a b = function 0 -> a | n -> fcon (times a b (n-1)) b
<brainacid>
it would have to be the simple basic code
<gour>
brainacid: again, i recommend you to read RWO 'cause it is long time available for review and commenters were taking care that the material is not presented prematurely not covering everything what is required which does not seem to be the case with 'your' book
<mrvn>
let rec times a b n = if n = 0 then a else fcon (times a b (n-1)) b
<brainacid>
i see
<brainacid>
its just a terse read
<brainacid>
so far
<brainacid>
im in the guided tour
<mrvn>
brainacid: have you done a factorial function yet?
<brainacid>
no
<mrvn>
then how did they introduce recursion?
<Drup>
mrvn: I think he didn't saw recursion yet ...
<gour>
that's good, you will get feeling for the language without the need to understand everything. then, starting with chapter two, you will get 'aha moments' ;)
<brainacid>
it was simply mentioned that during recursion i would love currying. Excuse my mistake
<brainacid>
Sweet! gour
<brainacid>
I will start and quit botherin you nice folk
<brainacid>
;)
<gour>
brainacid: do you have ocaml compiler installed to try examples
<mrvn>
brainacid: odd. I don't see partial application as relevant for recursions that much.
<brainacid>
I installed the whole package from the site ... for winblows 8
<gour>
don't just read, but try as well ;)
<brainacid>
I also have ubuntu linux
<gour>
wih linux it would be easier
<gour>
as far as rWO book is concerned
* brainacid
fires up the Pentium 4
<gour>
bear in mind to check Appendix in RWO to install some extra stuff required
<mrvn>
brainacid: for most concepts it is hard to find a good standalone examples that is just a few lines.
* gour
is ocaml noob as well
<mrvn>
some things you just have to learn blindly and accept and over time you will start to value them.
<mrvn>
(or not, that is ok too)
<mrvn>
Some things come down to taste.
<brainacid>
I wish i kept going when I was 14. I started Perl back then on RedHat 6 or 4...cant remember
<brainacid>
I wanted to be 31337 hax0r...white hat of course
<brainacid>
lol
<brainacid>
ocaml installing on my loud Western Digital.
* gour
--> sleep
<gour>
'night folks
<pippijn>
brainacid: I started programming at 19
gour has left #ocaml []
<pippijn>
so yeah
<pippijn>
no need to wish for having done something
Cypi has quit []
Cypi has joined #ocaml
UncleVasya has quit [Remote host closed the connection]
<technomancy>
it looks like running `ocamlbuild -use-ocamlfind mycode.byte` with opam hard-codes the opam-installed location of ocamlrun into the bytecode?
Cypi has quit [Client Quit]
Cypi has joined #ocaml
demonimin has joined #ocaml
<adrien>
technomancy: yes, it's a shebang
<technomancy>
ah, I can just fix it in emacs
<adrien>
but you can still /whichever/ocamlrun/you/want foo.byte
<technomancy>
is there a way to get ocamlbuild to emit portable .byte files though?
<adrien>
there's also the custom mode which is basically "cat ocamlrun foo.byte > foo.exe" but it's best avoided nowadays
<technomancy>
if I have to do it by hand I'll surely forget eventually
<adrien>
they're portable
<adrien>
you'd like a different shebang?
<technomancy>
interesting definition of portable =)
<technomancy>
I'd like something that works on someone else's machine, yeah
<adrien>
technomancy: but generally-speaking, don't mix different compiler bits
<adrien>
you could emit bytecode which use primitives that aren't available with another ocamlrun
<technomancy>
according to the Debian OCaml guide, bytecode versions are portable
<adrien>
I think it's also something you'd chose with -dllpath or a name that is similar
<pippijn>
(assuming you're on amd64)
<technomancy>
adrien: thanks; I think I'm in way over my head with ctypes... the docs don't really cover any of this stuff
<brainacid>
Pentium 4
<brainacid>
lol
<brainacid>
thanks bro
<brainacid>
got 'em
olliefr has quit [Ping timeout: 260 seconds]
<adrien>
technomancy: check "man ocamlc", there's a -dllpath option and a -use-runtime option too
<adrien>
and there are command-line options and environment variables for ocamlrun too
<technomancy>
adrien: thanks. I guess I need to figure out how ocamlbuild passes args to ocamlc?
<adrien>
technomancy: btw, the typical way to share binaries across distributions is to have a wrapper script anyway (to set LD_LIBRARY_PATH, PATH, env vars, ...)
<adrien>
(web browsers love to abuse that)
<technomancy>
yeah, I do that on the JVM for clojure; it's kind of a pain =\
<adrien>
for ocamlbuild, I don't remember but I'm sure that others here do
<adrien>
yeah, annoying but you should only need a couple lines
<adrien>
going to bed, good night
olliefr has joined #ocaml
Yoric has quit [Remote host closed the connection]
snearch has quit [Quit: Verlassend]
zpe has joined #ocaml
Kakadu has joined #ocaml
Yoric has joined #ocaml
olliefr has quit [Ping timeout: 260 seconds]
Kakadu has quit [Client Quit]
zpe has quit [Ping timeout: 248 seconds]
zpe has joined #ocaml
Yoric has quit [Ping timeout: 264 seconds]
shinnya has joined #ocaml
zpe_ has joined #ocaml
zpe has quit [Ping timeout: 245 seconds]
csakatoku has joined #ocaml
zpe_ has quit [Remote host closed the connection]
tane has quit [Quit: Verlassend]
<brainacid>
what kind of programs are coded in OCaml?
<brainacid>
and what potential does it have?
<brainacid>
anone
<brainacid>
& n e 1
csakatoku has quit [Remote host closed the connection]
csakatoku has joined #ocaml
nikki93 has quit [Remote host closed the connection]