POV-Ray : Newsgroups : povray.off-topic : Try Haskell : Re: Try Haskell Server Time
4 Sep 2024 15:23:09 EDT (-0400)
  Re: Try Haskell  
From: Invisible
Date: 3 Mar 2010 09:20:10
Message: <4b8e701a@news.povray.org>
scott wrote:
> The tutorial was good up until step 7.

Hmm, maybe I should write my own?



 > 2 + 2
That was easy, eh?

 > 2 + 3 * 4
The usual operator precedence rules apply.

 > (2 + 3) * 4
You can override operator precedence manually in the usual way.

 > sqrt 144
This is a function call. (Notice the lack of brackets!)

 > div 10 3
This is a 2-argument function. (Again, notice the lack of any brackets.) 
It performs integer division (i.e., division to the nearest whole number.)

 > 10 / 3
Does the same calculation, but with floating-point arithmetic.

 > 10 `div` 3
This does the same thing as the first one, but it looks more natural. 
The backticks work for any 2-argument function.

 > sin 1 + cos 1
This is sin(1) + cos(2).

 > sin (1 + cos 1)
This does a different calculation.

 > sin -1
This gives an error, because it thinks you mean "sin - 1", which is 
invalid. (Yes, we all think it's very annoying.)

 > sin (-1)
This works properly.

 > "This is a string."

 > 'X'
This is a single character.

 > "X"
This is a string that only contains 1 character.

 > ""
This is an empty string.

 > [8, 3, 11, 2]
This is a list of numbers.

 > ['X', 'Y', 'Z']
This is a list of characters. Note that a string *is* a list of characters!

 > ['X', 3]
A list can contain elements of any type, but all elements must be of 
*the same* type. This example is therefore invalid. (Nice clear error 
message, eh?)

 > sort [8, 3, 11, 2]
 > reverse [8, 3, 11, 2]
 > sum [8, 3, 11, 2]
 > 3 `elem` [8, 3, 11, 2]
 > 4 `elem` [8, 3, 11, 2]
Haskell has many, many list functions.

 > [1, 2, 3] ++ [4, 5, 6]
This joins two lists together.

 > [1..10]
This is a shorthand for writing lists.

 > sum [1..100]
Add up these numbers.

 > product [1..100]
Multiply these numbers. (Notice that even though the answer is huge, 
Haskell computes it exactly.)

 > take 3 [5,4,6,3,7,2,8,1,9]
Get the first 3 elements.

 > drop 3 [5,4,6,3,7,2,8,1,9]
Remove the first 3 elements.

 > [5,4,6,3,7,2,8,1,9] !! 5
Get the 6th element. (The first element is index zero, not index one!)

 > filter odd [5,4,6,3,7,2,8,1,9]
Filter a list according to a condition.

 > filter (> 5) [5,4,6,3,7,2,8,1,9]
Filter with a different condition.

 > map (*2) [5,4,6,3,7,2,8,1,9]
Double all the numbers in the list.

 > map toUpper "This is a string."
Since strings are just lists that contain characters, all list functions 
work on strings.

 > sin
You can't print this out, so you get an error.

 > :t sin
This tells the interpretter to only print the type signature, not 
*execute* the code. (This is an interpretter command, not a Haskell 
command.)

 > (5, 'x')
This is a "tuple". Tuples can contain any number of elements, and they 
can be of different types. Their main use is in returning several 
results from a single function call.

 > partition odd [5,4,6,3,7,2,8,1,9]
This splits the list into two lists, containing odd and not-odd numbers. 
The lists are returned as a size-2 tuple.

 > :t fst
Take a look at this type signature. Can you figure out what it means?

 > fst (5, 'x')
Did that do what you expected?

 > let x = 5 in x + 1
This is how you define local variables. (The interpretter unfortunately 
doesn't let you define things and then use them later. Everything has to 
go into a single giant let-expression.)

 > let x = 5; y = 8 in x + y
This is how you define multiple variables.

 > iterate (*2) 1
This starts with 1, and applies the "times two" function to it over and 
over again, creating an infinite list of results. (Again, the 
interpretter doesn't display the whole thing.)

 > let factorial n = product [1..n] in factorial 6
This is how you define a new function. (Normally it wouldn't be a local 
variable, but the interpretter unfortunately doesn't allow top-level 
definitions.)

 > :t (+1)
This is a function that adds one to a number.

 > :t (*2)
This is a function that multiplies a number by two.

 > :t (+1) . (*2)
The "." operator takes two functions and combines them together. Here 
we've made a function that takes a number, multiplies it by two and then 
adds one. (Note carefully the order - RIGHT TO LEFT!)

 > map ((+1) . (*2)) [1..10]
Apply this function to a list of numbers.

 > :t div
This is a 2-argument function that does integer division.

 > :t div 10
This is not a syntax error. It's a 1-argument function. WTF?

 > :t div 10 5
This is a calculation that produces a number.

 > div 10 5
This divides 10 by 5.

 > map (div 10) [1..10]
See if you can work out what's going on.



OK, I'm going to stop now...


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.