last day (15 days later) » 

7:58 PM
@boatSoap I think Javascript has some pretty functional features
 
@Caridorc Thanks for offering this, I was unaware SO had chat. Do you often write scala and what language did you transition from?
 
Scala does not work on my system but I often play with Ruby that is similar on the surface
I know some Haskell that I would suggest if you want to make the leap to FP
Writing imperative code in scala is possible, and that makes Functional thinking harder to achieve
Haskell is absolutely without imperative support
Scala is more practical in other ways though. I suggest I bit of Haskell and going back to Scala when you are confortable with it
Are you in for a practical example of Haskell? A mind bender one?
 
That does make sense. One issue is the sheer number of built in functions. I was planning on going with Haskell and Scala was suggested by a friend
Sure, lets see the example
 
@boatSoap Haskell built-ins are super easy to find
just Hoogle them
Scala has a weak type system, Java-like types
In Haskell all you need to write a function is figure out its type
then you can search it in hoogle by type
The practical example I had promised you: map
map goes from a function and a list to a list
perfect, on a first approximation, the type is
 
yes
or am looking at it in hoogle now
 
8:13 PM
map :: (a -> b) -> [a] -> [b]
Sooo
 
yes
 
(a -> b)
is a function from a to b
manyA n = replicate n 'a'
^ a simple example
a in this case is Int
and b is String
map is very general
a may be anything
and b may be anything
This generality is fundamental to writing general functions in Haskell
@boatSoap Is what I mean by generality clear?
 
yes it is
how would you go about invoking your example
 
map manyA [1, 5, 6, 10]
 
List a.map.toString()
 
8:18 PM
wait, How can you call map with no argument?
 
You need a function as an argument
 
Exactly, but you called map with no arg in your example
Interactive Haskell shell:
Prelude> let manyA n = replicate n 'a'
Prelude> map manyA [1, 5, 6, 10]
["a","aaaaa","aaaaaa","aaaaaaaaaa"]
let is necessary because it is Haskell way of faking imperativeness in a REPL
Is my interactive shell session clear?
 
a lot goes on behind the scenes
yes it is clear, thank you
 
Another thing is that Haskell is executable math
How many arguments can a function have in Math?
1
And function in Haskell can only ever take one argument
Prelude> (map manyA) [1, 5, 6, 10]
["a","aaaaa","aaaaaa","aaaaaaaaaa"]
 
i see
 
8:23 PM
Prelude> (map manyA)

<interactive>:5:1:
    No instance for (Show ([Int] -> [[Char]]))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for (Show ([Int] -> [[Char]]))
    In a stmt of an interactive GHCi command: print it
Error, a function from a list of ints to a list of strings cannot be printed
Haskell errors are hard, maybe the hardest part of Haskell
All my previous languages had duck typing
Haskell is type-driven
Almost every Haskell error is a type Error, Haskellers are proud of this
@boatSoap Is there any specific part of Haskell or FP you are interested in?
 
FP for me is a way to learn something new. I don't think I will be using it work anytime soon, but who knows
I really appreciate the time you put into this
And understand how much I must change my approach to a problem when shifting from imperative to functional
 
@boatSoap You are welcome, Haskell is interesting for me, but it is still weird
main = print "Welcome to Haskell and FP"
 
To me, it reads as a string of verbs
 
Exactly!
 
but, it is still foreign
and I can't infer what happens between them yet
 
8:29 PM
OO programming is a list of nouns with some verb here and there
FP is a list of verbs with possibly no nouns at all
(people still use nouns to make code easier to read, do not worry)
Look at how much this seems imperative:
main = do
  putStrLn "Please enter your name:"
  name <- getLine
  putStrLn ("Hello, " ++ name ++ ", how are you?")
But there is a trick, what you see are really only functions and 'monads` (things that say what should be done between the functions)
@Hosch250 hi
 
Hi.
 
It is remarkable that the above code just pretends to be imperative but really is Haskell
Also Haskell favors pure functions
Quiz:

Which function of this is pure?

foo = print 1
bar = 1

(Yes bar is a function, a function that takes no arguments, but still a function)
pure = Mathematical
 
does pure have nothing to do with only "doing one thing"
 
@boatSoap pure means Mathematical
 
i guess foo only does one thing as well
it doesn't return a value, it simply prints
 
8:38 PM
Having a strong math background really helps with Haskell
Imagine your math class
 
yea..... the days I wish I studied more math are countless
Hence Euler and other mathematical algorithm problems
 
Is the function LaunchARocket purely mathematical and abstract?
 
likely not
 
Exactly
Because it interacts with the real word
In Haskell, >90% of the functions should be pure
with no communication to the real world, just pure math
@boatSoap In your opinion, why this reccomandation?
 
side effects will be minimized, troubleshooting will be localized
 
8:46 PM
@boatSoap Exactly. A functional will give the same output for the same input everytime, just like, yes a Mathematical function
f x = x + 1
f x = 1 + x
By the way In haskell you can simplify
f = (+ 1)
works as well
 
functions are methods?
1.(+1)
 
no way
. is the composition operator
 
does that work?
 
Prelude> let f = (+ 1)
Prelude> f 5
6
f . g means applying g and then f
Prelude> let last' = head . reverse
Prelude> last' [1,2,3]
3
@boatSoap there are no Objects in Haskell, Objects means to be Noun-centric
The error your code triggers is kind of verbose:
Prelude> 1.(+1)

<interactive>:26:1:
    No instance for (Num (b0 -> c0)) arising from the literal `1'
    Possible fix: add an instance declaration for (Num (b0 -> c0))
    In the first argument of `(.)', namely `1'
    In the expression: 1 . (+ 1)
    In an equation for `it': it = 1 . (+ 1)

<interactive>:26:4:
    No instance for (Num b0) arising from a use of `+'
    The type variable `b0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
You can actually define . (DOT) if you want, it is easy
And I consider myself a beginner in Haskell
so I am just scratching the surface
@boatSoap as in Math, it is all about understanding it, playing with it and gaining familiarity with it.
@boatSoap I wish you good luck with it, and of course you are welcome to post your programs to CodeReview for feedback
 
9:07 PM
@Caridorc Thanks again for all the information!
 
@boatSoap You are welcome.
 

last day (15 days later) »