TU Wien:Funktionale Programmierung VU (Knoop)/Allgemeinen Typ eines nicht-Lambda-Ausdrucks bestimmen

Aus VoWi
Zur Navigation springen Zur Suche springen

Diese Seite soll die Funktionen, die in Beispiel 1 typischerweise vorkommen, durch Beispiele kurz veranschaulichen. Wenn wer Erklärungen hinzufügen will, ist das gerne gesehen. Nicht abgedeckt werden Lambda-Ausdrücke TU Wien:Funktionale Programmierung VU (Knoop)/Allgemeinen Typ eines Lambda-Ausdrucks bestimmen (Howto t2-Beispiele) , list comprehensions https://wiki.haskell.org/List_comprehension und der . - Operator.

Standard Funktionen[Bearbeiten | Quelltext bearbeiten]

Hugs> :t [0,1,2,3,4,5,6,7,8,9] :: [Integer]
[0,1,2,3,4,5,6,7,8,9] :: [Integer]
Hugs> :t "Another String":: String
"Another String" :: String
Hugs> :t  "815":: String
"815" :: String

Hugs> :t drop 
drop :: Int -> [a] -> [a]
Hugs> drop 3 [1,2,3,4,5,6]
[4,5,6]
Hugs> :t drop 3 [1,2,3,4,5,6]
drop 3 [1,2,3,4,5,6] :: Num a => [a]
Hugs> drop 3 "Halloechen"
"loechen"
Hugs> :t drop 3 "Halloechen"
drop 3 "Halloechen" :: [Char]
Hugs> 
Hugs> :t head
head :: [a] -> a
Hugs> head [0,1,2,3,4,5,6,7,8,9]
0
Hugs> :t head [0,1,2,3,4,5,6,7,8,9]
head [0,1,2,3,4,5,6,7,8,9] :: Num a => a
Hugs> head "thisisastring"
't'
Hugs> :t head "thisisastring"
head "thisisastring" :: Char
Hugs> 
Hugs> :t length
length :: [a] -> Int
Hugs> length [0,1,2,3,4,5,6,7,8,9]
10
Hugs> :t [0,1,2,3,4,5,6,7,8,9]
[0,1,2,3,4,5,6,7,8,9] :: Num a => [a]
Hugs> length "kurz"
4
Hugs> :t length "kurz"
length "kurz" :: Int
Hugs> 
Hugs> :t mod
mod :: Integral a => a -> a -> a
Hugs> mod 21 10
1
Hugs> 21 `mod` 5
1
Hugs> :t mod 21 5
21 `mod` 5 :: Integral a => a
Hugs> :t 21 `mod` 5
21 `mod` 5 :: Integral a => a
Hugs> 
Hugs> :t product
product :: Num a => [a] -> a
Hugs> product [0,1,2,3,4,5,6,7,8,9]
0
Hugs> :t product [0,1,2,3,4,5,6,7,8,9]
product [0,1,2,3,4,5,6,7,8,9] :: Num a => a
Hugs> 
Hugs> :t reverse
reverse :: [a] -> [a]
Hugs> reverse [0,1,2,3,4,5,6,7,8,9]
[9,8,7,6,5,4,3,2,1,0]
Hugs> :t reverse [0,1,2,3,4,5,6,7,8,9]
reverse [0,1,2,3,4,5,6,7,8,9] :: Num a => [a]
Hugs> reverse "yo!"
"!oy"
Hugs> :t reverse "yo!"
reverse "yo!" :: [Char]
Hugs> 
Hugs> :t show
show :: Show a => a -> String
Hugs> show [1,2,3]
"[1,2,3]"
Hugs> :t show [1,2,3]
show [1,2,3] :: Num a => [Char]
Hugs> show "show"
"\"show\""
Hugs> :t show "show"
show "show" :: [Char]
Hugs> 
Hugs> :t sum
sum :: Num a => [a] -> a
Hugs> sum [1,2,3,4,5]
15
Hugs> :t sum [1,2,3,4,5]
sum [1,2,3,4,5] :: Num a => a
Hugs> 
Hugs> :t tail
tail :: [a] -> [a]
Hugs> tail [0,1,2,3,4,5,6,7,8,9]
[1,2,3,4,5,6,7,8,9]
Hugs> :t tail [0,1,2,3,4,5,6,7,8,9]
tail [0,1,2,3,4,5,6,7,8,9] :: Num a => [a]
Hugs> tail "astring"
"string"
Hugs> :t tail "astring"
tail "astring" :: [Char]
Hugs> 
Hugs> :t take
take :: Int -> [a] -> [a]
Hugs> take 3 [0,1,2,3,4,5,6,7,8,9]
[0,1,2]
Hugs> take 0 [0,1,2,3,4,5,6,7,8,9]
[]
Hugs> take 15 [0,1,2,3,4,5,6,7,8,9]
[0,1,2,3,4,5,6,7,8,9]
Hugs> :t take 3 [0,1,2,3,4,5,6,7,8,9]
take 3 [0,1,2,3,4,5,6,7,8,9] :: Num a => [a]
Hugs> take 3 "string"
"str"
Hugs> :t take 3 "string"
take 3 "string" :: [Char]
Hugs> 
Hugs> :t unlines
unlines :: [String] -> String
Hugs> unlines ["hallo", "welt", ['1', 's', 't', 'r', 'i', 'n', 'g']]
"hallo\nwelt\n1string\n"
Hugs> :t unlines ["hallo", "welt", ['1', 's', 't', 'r', 'i', 'n', 'g']]
unlines ["hallo","welt",['1','s','t','r','i','n','g']] :: [Char]
Hugs> 
Hugs> :t words
words :: String -> [String]
Hugs> words "hallo haskell string welt"
["hallo","haskell","string","welt"]
Hugs> :t words "hallo haskell string welt"
words "hallo haskell string welt" :: [String]

Let, in[Bearbeiten | Quelltext bearbeiten]

"in" steht für "invoke". Illustrierendes Beispiel:

Main> let x n = n `mod` 2 in x 5
1
Main> let x n = n `mod` 2 in x 6
0
Main> (\n -> n `mod` 2) 5 -- Zum Vergleich das Gleiche als Lambda-Audruck
1

Doku, die danach vielleicht verständlich ist: https://wiki.haskell.org/Keywords

Hierfür müssen noch Beispiele gefunden werden[Bearbeiten | Quelltext bearbeiten]

Hugs> :t (:)
(:) :: a -> [a] -> [a]
Hugs> :t (.)
(.) :: (a -> b) -> (c -> a) -> c -> b
Hugs>

Case expressions[Bearbeiten | Quelltext bearbeiten]

http://learnyouahaskell.com/syntax-in-functions