Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Miranda (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Sample code== The following Miranda script determines the set of all subsets of a set of numbers <syntaxhighlight lang="haskell"> subsets [] = [[]] subsets (x:xs) = [[x] ++ y | y <- ys] ++ ys where ys = subsets xs </syntaxhighlight> and this is a literate script for a function <code>primes</code> which gives the list of all prime numbers <syntaxhighlight lang="text"> > || The infinite list of all prime numbers. The list of potential prime numbers starts as all integers from 2 onwards; as each prime is returned, all the following numbers that can exactly be divided by it are filtered out of the list of candidates. > primes = sieve [2..] > sieve (p:x) = p : sieve [n | n <- x; n mod p ~= 0] </syntaxhighlight> Here, we have some more examples <syntaxhighlight lang="haskell"> max2 :: num -> num -> num max2 a b = a, if a>b = b, otherwise max3 :: num -> num -> num -> num max3 a b c = max2 (max2 a b) (max2 a c) multiply :: num -> num -> num multiply 0 b = 0 multiply a b = b + (multiply (a-1) b) fak :: num -> num fak 0 = 1 fak n = n * fak (n-1) itemnumber::[*]->num itemnumber [] = 0 itemnumber (a:x) = 1 + itemnumber x weekday::= Mo|Tu|We|Th|Fr|Sa|Su isWorkDay :: weekday -> bool isWorkDay Sa = False isWorkDay Su = False isWorkDay anyday = True tree * ::= E| N (tree *) * (tree *) nodecount :: tree * -> num nodecount E = 0 nodecount (N l w r) = nodecount l + 1 + nodecount r emptycount :: tree * -> num emptycount E = 1 emptycount (N l w r) = emptycount l + emptycount r treeExample = N ( N (N E 1 E) 3 (N E 4 E)) 5 (N (N E 6 E) 8 (N E 9 E)) weekdayTree = N ( N (N E Mo E) Tu (N E We E)) Th (N (N E Fr E) Sa (N E Su)) insert :: * -> stree * -> stree * insert x E = N E x E insert x (N l w E) = N l w x insert x (N E w r) = N x w r insert x (N l w r) = insert x l , if x <w = insert x r , otherwise list2searchtree :: [*] -> tree * list2searchtree [] = E list2searchtree [x] = N E x E list2searchtree (x:xs) = insert x (list2searchtree xs) maxel :: tree * -> * maxel E = error "empty" maxel (N l w E) = w maxel (N l w r) = maxel r minel :: tree * -> * minel E = error "empty" minel (N E w r) = w minel (N l w r) = minel l ||Traversing: going through values of tree, putting them in list preorder,inorder,postorder :: tree * -> [*] inorder E = [] inorder N l w r = inorder l ++ [w] ++ inorder r preorder E = [] preorder N l w r = [w] ++ preorder l ++ preorder r postorder E = [] postorder N l w r = postorder l ++ postorder r ++ [w] height :: tree * -> num height E = 0 height (N l w r) = 1 + max2 (height l) (height r) amount :: num -> num amount x = x ,if x >= 0 amount x = x*(-1), otherwise and :: bool -> bool -> bool and True True = True and x y = False || A AVL-Tree is a tree where the difference between the child nodes is not higher than 1 || i still have to test this isAvl :: tree * -> bool isAvl E = True isAvl (N l w r) = and (isAvl l) (isAvl r), if amount ((nodecount l) - (nodecount r)) < 2 = False, otherwise delete :: * -> tree * -> tree * delete x E = E delete x (N E x E) = E delete x (N E x r) = N E (minel r) (delete (minel r) r) delete x (N l x r) = N (delete (maxel l) l) (maxel l) r delete x (N l w r) = N (delete x l) w (delete x r) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)