Learn how to do write simple functional application like calculating the area of triangle using square root, and new functions like let and in, and print out result. This video includes some basic explanation and step by step guide to create simple


More DIY videos at 5min.com

Video Transcription

Ayumilove Haskell tutorial 2 (A) Ayumiloves’s Day: Tutorial tow is split into two sections due to 5min,com duration limitation(five minutes). Part A discuss on writing scripts using notepad Part B shows how to run scripts using WinHugs/GHCI Hi guys! Ayumilove here ^_^ (nickname: Ayu) Ok, today I’ll be teaching the 2nd part of Haskell Programming Tutorial. 26/07/2008 Let’s recap on the previous tutorial: 1. We learn how to create a paragraph comment! How to create a paragraph comment? Use these 2 symbols {- -} and place your text/comment in between them. If you have forgotten, I’ll refresh you! There you have it! Ok… so how do we go about with 1 line comment? We add 2 dashes before our text! Like this -- Example of 1 line comment -- 2nd line of comment -- 3rd line of comment NOTE: If you have many lines of codes, use this { - -} So, how do we first start creating out function? Oh yeah, I need to tell you about some “rule” of creating function name… Function name starts with small letter (small caps) Like this: addNumber : : Int -> Int -> Int -- It’s not appropriate to use ADDNUMBER or AddNumber -- What else XD. Ok. We now move on with our next exercise 2 Below is the question. An interesting math question! Don’t be afraid, I’m with you. Exercise 2: Area of Triangle The area of a triangle with sides a,b,c is given by the formula: square_root [s(s-a)(s-b)(s-c)] whereby s – (a+b+c)/2 Do you still remember how math multiply? (10)(20) is same as 10 multiply by 20 or 10 x 20 = 200 This expression can be represented by 10.20 or 10*20 Design a Haskell Program to calculate area of triangle. Today, we will be using 2 special operators/function from Haskell Library and they are “let” and “in”. (p.s. without quotes) The problem now is how do we use these “let” and “in” into the equation/expression if square root? Step 1: We define our function name. we called it as areaTriangle. This function will store the formula for calculating the area of triangle! We are required to define the data type. To define, we use double semicolons :: and proceed with the data type (example: Integer, Float, etc) Since we need 3 input values, we need 3 datatype input so… 3 floats (for input0 + 1 float (for output) areaTriangle : : Float -> Float -> Float -> Float Step 2: Now we write our function’s action! NOTE: Indentation/Spaces in Haskell plays a vital role. Why? Indentation tells Haskell/our compiler/etc that which line to do first. So, if you have 4 lines under 1 line then, it will do those first before proceeding to the other line. Priority << Another thing is, without proper indentation, your code will not work and the compiler will be having a very hard time thinking how your code work. When all fails, it simply post an error message! areaTriangle : : Float -> Float -> Float -> Float areaTriangle a b c = let in -- let and in comes together in a pair, it does not work if both of them are separated! Couple lol, let comes before in. to create indentation, simply press “TAB” on your keyboard, above “CAPS Lock” button. Basically, what we are telling the complier is let s = (a+b+c)/2 into the square_root equation below square_root[s(s-a)(s-b)(s-c)] Which means, replace all those s with (a+b+c)/2 in it!!! Sqrt is short for square root areaTriangle : : Float -> Float -> Float -> Float areaTriangle a b c = let s = (a+b+c)/2 in sqrt (s*(s-a)(s-b)(s-c)) In Haskell programming, the brackets are used to group numbers, not multiplelike in mathematics. In math… (1+2)(2+3) = (3x5) = 3 multiply by 5 = 15. In Haskell… it interprets as 2 different values, 3 and 5. So to tell Haskell that we want 2 or more values to multiply with another, we use this asterisk symbol ********* < Float -> Float -> Float areaTriangle a b c = let s = (a+b+c)/2 in sqrt (s*(s-a)*(s-b)*(s-c)) There you have it! Our code is completed! Run WInhugs or GHCI to execute your .hs script. Save this notepad file as .hs (extension file) NOTE: let and in starts off with small caps (no capital letter) Let/In = wrong/incorre